From 85d7ca48a6ccaf95b0fe243716d12d0b97bd4fe3 Mon Sep 17 00:00:00 2001 From: Peter Aronoff Date: Sat, 27 Jan 2024 15:59:49 -0500 Subject: [PATCH 1/2] Add options to disable tracking and local counts --- README.md | 11 +++++++++-- git-prompt.zsh | 43 ++++++++++++++++++++++++++++++++++++------- 2 files changed, 45 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index dc0ec0a..9ca27d2 100644 --- a/README.md +++ b/README.md @@ -144,8 +144,8 @@ The `symbol` option prints only `ZSH_THEME_GIT_PROMPT_UPSTREAM_SYMBOL`. Furthermore, a warning symbol can be configured through `ZSH_THEME_GIT_PROMPT_UPSTREAM_NO_TRACKING` for the case where no remote is available. `ZSH_THEME_GIT_PROMPT_UPSTREAM_NO_TRACKING` can be set independently of `ZSH_GIT_PROMPT_SHOW_UPSTREAM`. -### Show number of stash entries -The number of stash entries will be shown if `ZSH_GIT_PROMPT_SHOW_STASH` is set. +### Show whether there are stash entries +If `ZSH_GIT_PROMPT_SHOW_STASH` is set, the prompt will display a symbol and the number of stash entries when there are entries in the stash. On Git versions older than 2.35.0 this will execute another Git command every time a new prompt is shown! To enable stash entries add the following line to your `.zshrc`: @@ -153,6 +153,13 @@ To enable stash entries add the following line to your `.zshrc`: ZSH_GIT_PROMPT_SHOW_STASH=1 ``` +### Disable display of numbers +By default, the prompt will show counts for each item in the tracking status and local status sections. +(See [Prompt Structure](#prompt-structure) for details about these sections.) +However, you can disable the display of counts for either or both sections of the prompt using `ZSH_GIT_PROMPT_SHOW_TRACKING_COUNTS` and `ZSH_GIT_PROMPT_SHOW_LOCAL_COUNTS`. +If you set these variables to anything other than `1`, then the symbols will be shown but not the counts. +For example, a prompt such as `[master|✚2]` will become `[master|✚]` instead. + ### Force blank Since the prompt is asynchronous by default, the Git status updates slightly delayed. This has the benefit that the prompt will always be responsive even if the repository is huge and/or your disk is slow. diff --git a/git-prompt.zsh b/git-prompt.zsh index 5c4f798..5bd12bb 100644 --- a/git-prompt.zsh +++ b/git-prompt.zsh @@ -24,6 +24,8 @@ autoload -U colors && colors # Settings : "${ZSH_GIT_PROMPT_SHOW_UPSTREAM=""}" : "${ZSH_GIT_PROMPT_SHOW_STASH=""}" +: "${ZSH_GIT_PROMPT_SHOW_TRACKING_COUNTS="1"}" +: "${ZSH_GIT_PROMPT_SHOW_LOCAL_COUNTS="1"}" : "${ZSH_GIT_PROMPT_ENABLE_SECONDARY=""}" : "${ZSH_GIT_PROMPT_NO_ASYNC=""}" : "${ZSH_GIT_PROMPT_FORCE_BLANK=""}" @@ -100,6 +102,8 @@ function _zsh_git_prompt_git_status() { -v DETACHED="$ZSH_THEME_GIT_PROMPT_DETACHED" \ -v BRANCH="$ZSH_THEME_GIT_PROMPT_BRANCH" \ -v UPSTREAM_TYPE="$ZSH_GIT_PROMPT_SHOW_UPSTREAM" \ + -v SHOW_TRACKING_COUNTS="$ZSH_GIT_PROMPT_SHOW_TRACKING_COUNTS" \ + -v SHOW_LOCAL_COUNTS="$ZSH_GIT_PROMPT_SHOW_LOCAL_COUNTS" \ -v UPSTREAM_SYMBOL="$ZSH_THEME_GIT_PROMPT_UPSTREAM_SYMBOL" \ -v UPSTREAM_NO_TRACKING="$ZSH_THEME_GIT_PROMPT_UPSTREAM_NO_TRACKING" \ -v UPSTREAM_PREFIX="$ZSH_THEME_GIT_PROMPT_UPSTREAM_PREFIX" \ @@ -204,33 +208,58 @@ function _zsh_git_prompt_git_status() { } if (behind < 0) { - prompt_element(BEHIND, behind * -1); + if (SHOW_TRACKING_COUNTS == "1") { + count = behind * -1; + } else { + count = "" + } + prompt_element(BEHIND, count); } if (ahead > 0) { - prompt_element(AHEAD, ahead * 1); + if (SHOW_TRACKING_COUNTS == "1") { + count = ahead * 1; + } + prompt_element(AHEAD, count); } prompt_element(SEPARATOR); if (unmerged > 0) { - prompt_element(UNMERGED, unmerged); + if (SHOW_LOCAL_COUNTS == "1") { + count = unmerged; + } else { + count = "" + } + prompt_element(UNMERGED, count); } if (staged > 0) { - prompt_element(STAGED, staged); + if (SHOW_LOCAL_COUNTS == "1") { + count = staged; + } + prompt_element(STAGED, count); } if (unstaged > 0) { - prompt_element(UNSTAGED, unstaged); + if (SHOW_LOCAL_COUNTS == "1") { + count = unstaged; + } + prompt_element(UNSTAGED, count); } if (untracked > 0) { - prompt_element(UNTRACKED, untracked); + if (SHOW_LOCAL_COUNTS == "1") { + count = untracked; + } + prompt_element(UNTRACKED, count); } if (stashed > 0 && SHOW_STASH != "") { - prompt_element(STASHED, stashed); + if (SHOW_LOCAL_COUNTS == "1") { + count = stashed; + } + prompt_element(STASHED, count); } if (unmerged == 0 && staged == 0 && unstaged == 0 && untracked == 0) { From e2da06d1fe6ab11213b70378a58f817556f4d016 Mon Sep 17 00:00:00 2001 From: Wolfgang Popp Date: Sat, 24 Feb 2024 11:29:08 +0100 Subject: [PATCH 2/2] Move counter check to separate function --- README.md | 7 +++-- git-prompt.zsh | 71 ++++++++++++++++++-------------------------------- 2 files changed, 29 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 9ca27d2..67bf1a6 100644 --- a/README.md +++ b/README.md @@ -144,7 +144,7 @@ The `symbol` option prints only `ZSH_THEME_GIT_PROMPT_UPSTREAM_SYMBOL`. Furthermore, a warning symbol can be configured through `ZSH_THEME_GIT_PROMPT_UPSTREAM_NO_TRACKING` for the case where no remote is available. `ZSH_THEME_GIT_PROMPT_UPSTREAM_NO_TRACKING` can be set independently of `ZSH_GIT_PROMPT_SHOW_UPSTREAM`. -### Show whether there are stash entries +### Show stash entries If `ZSH_GIT_PROMPT_SHOW_STASH` is set, the prompt will display a symbol and the number of stash entries when there are entries in the stash. On Git versions older than 2.35.0 this will execute another Git command every time a new prompt is shown! To enable stash entries add the following line to your `.zshrc`: @@ -154,10 +154,9 @@ ZSH_GIT_PROMPT_SHOW_STASH=1 ``` ### Disable display of numbers -By default, the prompt will show counts for each item in the tracking status and local status sections. -(See [Prompt Structure](#prompt-structure) for details about these sections.) +By default, the prompt will show counts for each item in the tracking status and local status sections (see [Prompt Structure](#prompt-structure) for details about these sections). However, you can disable the display of counts for either or both sections of the prompt using `ZSH_GIT_PROMPT_SHOW_TRACKING_COUNTS` and `ZSH_GIT_PROMPT_SHOW_LOCAL_COUNTS`. -If you set these variables to anything other than `1`, then the symbols will be shown but not the counts. +If you unset these variables or set them to `0`, then only the symbols will be shown but not the counts. For example, a prompt such as `[master|✚2]` will become `[master|✚]` instead. ### Force blank diff --git a/git-prompt.zsh b/git-prompt.zsh index 5bd12bb..bad6524 100644 --- a/git-prompt.zsh +++ b/git-prompt.zsh @@ -143,6 +143,24 @@ function _zsh_git_prompt_git_status() { print(RC); } + function count_element(prefix, count, show_count) { + content = ""; + if (show_count) { + content = count; + } + if (count > 0) { + prompt_element(prefix, content); + } + } + + function local_element(prefix, count) { + count_element(prefix, count, SHOW_LOCAL_COUNTS) + } + + function tracking_element(prefix, count) { + count_element(prefix, count, SHOW_TRACKING_COUNTS) + } + $1 == "fatal:" { fatal = 1; } @@ -207,59 +225,22 @@ function _zsh_git_prompt_git_status() { prompt_element(UPSTREAM_PREFIX, upstream, UPSTREAM_SUFFIX); } - if (behind < 0) { - if (SHOW_TRACKING_COUNTS == "1") { - count = behind * -1; - } else { - count = "" - } - prompt_element(BEHIND, count); - } + tracking_element(BEHIND, behind * -1); - if (ahead > 0) { - if (SHOW_TRACKING_COUNTS == "1") { - count = ahead * 1; - } - prompt_element(AHEAD, count); - } + tracking_element(AHEAD, ahead * 1); prompt_element(SEPARATOR); - if (unmerged > 0) { - if (SHOW_LOCAL_COUNTS == "1") { - count = unmerged; - } else { - count = "" - } - prompt_element(UNMERGED, count); - } + local_element(UNMERGED, unmerged); - if (staged > 0) { - if (SHOW_LOCAL_COUNTS == "1") { - count = staged; - } - prompt_element(STAGED, count); - } + local_element(STAGED, staged); - if (unstaged > 0) { - if (SHOW_LOCAL_COUNTS == "1") { - count = unstaged; - } - prompt_element(UNSTAGED, count); - } + local_element(UNSTAGED, unstaged); - if (untracked > 0) { - if (SHOW_LOCAL_COUNTS == "1") { - count = untracked; - } - prompt_element(UNTRACKED, count); - } + local_element(UNTRACKED, untracked); - if (stashed > 0 && SHOW_STASH != "") { - if (SHOW_LOCAL_COUNTS == "1") { - count = stashed; - } - prompt_element(STASHED, count); + if (SHOW_STASH) { + local_element(STASHED, stashed); } if (unmerged == 0 && staged == 0 && unstaged == 0 && untracked == 0) {