From 46e63340eef243f25890e2178931919125c58aae Mon Sep 17 00:00:00 2001 From: whoami Date: Wed, 1 Dec 2021 19:49:42 +0300 Subject: [PATCH] feat(branch): show mercurial bookmarks if used (#9948) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marc Cornellà --- plugins/branch/README.md | 48 +++++++++++++++++++++----------- plugins/branch/branch.plugin.zsh | 44 ++++++++++++++++------------- 2 files changed, 56 insertions(+), 36 deletions(-) diff --git a/plugins/branch/README.md b/plugins/branch/README.md index 56ab8da4b..a15dd22df 100644 --- a/plugins/branch/README.md +++ b/plugins/branch/README.md @@ -1,31 +1,47 @@ -# Branch +# Branch plugin -Displays the current Git or Mercurial branch fast. +This plugin displays the current Git or Mercurial branch, fast. If in a Mercurial repository, +also display the current bookmark, if present. + +To use it, add `branch` to the plugins array in your zshrc file: + +```zsh +plugins=(... branch) +``` ## Speed test -### Mercurial +- `hg branch`: -```shell -$ time hg branch -0.11s user 0.14s system 70% cpu 0.355 total -``` + ```console + $ time hg branch + 0.11s user 0.14s system 70% cpu 0.355 total + ``` -### Branch plugin +- branch plugin: -```shell -$ time zsh /tmp/branch_prompt_info_test.zsh -0.00s user 0.01s system 78% cpu 0.014 total -``` + ```console + $ time zsh /tmp/branch_prompt_info_test.zsh + 0.00s user 0.01s system 78% cpu 0.014 total + ``` ## Usage -Edit your theme file (eg.: `~/.oh-my-zsh/theme/robbyrussell.zsh-theme`) -adding `$(branch_prompt_info)` in your prompt like this: +Copy your theme to `$ZSH_CUSTOM/themes/` and modify it to add `$(branch_prompt_info)` in your prompt. +This example is for the `robbyrussell` theme: ```diff -- PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}' -+ PROMPT='${ret_status}%{$fg_bold[green]%}%p %{$fg[cyan]%}%c %{$fg_bold[blue]%}$(git_prompt_info)$(branch_prompt_info)%{$fg_bold[blue]%} % %{$reset_color%}' +diff --git a/themes/robbyrussell.zsh-theme b/themes/robbyrussell.zsh-theme +index 2fd5f2cd..9d89a464 100644 +--- a/themes/robbyrussell.zsh-theme ++++ b/themes/robbyrussell.zsh-theme +@@ -1,5 +1,5 @@ + PROMPT="%(?:%{$fg_bold[green]%}➜ :%{$fg_bold[red]%}➜ )" +-PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(git_prompt_info)' ++PROMPT+=' %{$fg[cyan]%}%c%{$reset_color%} $(branch_prompt_info)' + + ZSH_THEME_GIT_PROMPT_PREFIX="%{$fg_bold[blue]%}git:(%{$fg[red]%}" + ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%} " ``` ## Maintainer diff --git a/plugins/branch/branch.plugin.zsh b/plugins/branch/branch.plugin.zsh index 2e5659bdf..dd5871fdc 100644 --- a/plugins/branch/branch.plugin.zsh +++ b/plugins/branch/branch.plugin.zsh @@ -3,29 +3,33 @@ # Oct 2, 2015 function branch_prompt_info() { - # Defines path as current directory - local current_dir=$PWD - # While current path is not root path - while [[ $current_dir != '/' ]] - do - # Git repository - if [[ -d "${current_dir}/.git" ]] - then - echo '±' ${"$(<"$current_dir/.git/HEAD")"##*/} - return; + # Start checking in current working directory + local branch="" dir="$PWD" + while [[ "$dir" != '/' ]]; do + # Found .git directory + if [[ -d "${dir}/.git" ]]; then + branch="${"$(<"${dir}/.git/HEAD")"##*/}" + echo '±' "${branch:gs/%/%%}" + return fi - # Mercurial repository - if [[ -d "${current_dir}/.hg" ]] - then - if [[ -f "$current_dir/.hg/branch" ]] - then - echo '☿' $(<"$current_dir/.hg/branch") + + # Found .hg directory + if [[ -d "${dir}/.hg" ]]; then + if [[ -f "${dir}/.hg/branch" ]]; then + branch="$(<"${dir}/.hg/branch")" else - echo '☿ default' + branch="default" fi - return; + + if [[ -f "${dir}/.hg/bookmarks.current" ]]; then + branch="${branch}/$(<"${dir}/.hg/bookmarks.current")" + fi + + echo '☿' "${branch:gs/%/%%}" + return fi - # Defines path as parent directory and keeps looking for :) - current_dir="${current_dir:h}" + + # Check parent directory + dir="${dir:h}" done }