1
0
Fork 0
mirror of https://github.com/ohmyzsh/ohmyzsh.git synced 2024-11-24 06:40:07 +00:00

Improved mercurial prompt

This commit is contained in:
HimDek 2022-05-29 08:28:15 +05:30
parent e9e8c6b54d
commit c2ef915921
2 changed files with 71 additions and 43 deletions

View file

@ -52,10 +52,18 @@ plugins=(... mercurial)
You can also redefine additional vars used in the plugin (after Oh My Zsh is sourced): You can also redefine additional vars used in the plugin (after Oh My Zsh is sourced):
```zsh ```zsh
ZSH_THEME_HG_PROMPT_PREFIX="%{$fg_bold[magenta]%}hg:(%{$fg[red]%}" ZSH_THEME_HG_PROMPT_PREFIX="("
ZSH_THEME_HG_PROMPT_SUFFIX="%{$reset_color%}" ZSH_THEME_HG_PROMPT_SUFFIX=")"
ZSH_THEME_HG_PROMPT_DIRTY="%{$fg[magenta]%}) %{$fg[yellow]%}✗%{$reset_color%}" ZSH_THEME_HG_PROMPT_SEPARATOR="|"
ZSH_THEME_HG_PROMPT_CLEAN="%{$fg[magenta]%})" ZSH_THEME_HG_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
ZSH_THEME_HG_PROMPT_BEHIND="%{↓%G%}"
ZSH_THEME_HG_PROMPT_AHEAD="%{↑%G%}"
ZSH_THEME_HG_PROMPT_MODIFIED="%{$fg[red]%}%{●%G%}"
ZSH_THEME_HG_PROMPT_ADDED="%{$fg[blue]%}%{✚%G%}"
ZSH_THEME_HG_PROMPT_REMOVED="%{$fg[red]%}%{✖%G%}"
ZSH_THEME_HG_PROMPT_DELETED="%{$fg[red]%}%{🗑️%G%}"
ZSH_THEME_HG_PROMPT_UNKNOWN="%{$fg[cyan]%}%{…%G%}"
ZSH_THEME_HG_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
``` ```
### Display repo branch and directory status in prompt ### Display repo branch and directory status in prompt

View file

@ -73,46 +73,66 @@ function hg_get_bookmark_name() {
echo "$(<"$dir/.hg/bookmarks.current")" echo "$(<"$dir/.hg/bookmarks.current")"
} }
function update_hg_vars() {
if $(hg id >/dev/null 2>&1); then
local rev="$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')"
local branch="$(hg id -b 2>/dev/null)"
local hg_status=`hg st`
HG_REV_BRANCH="${rev:gs/%/%%}%{$FG[239]%}@%{$reset_color%}${branch:gs/%/%%}"
HG_UNKNOWN="$(echo $hg_status | grep "^\?" | wc -l)"
HG_MODIFIED="$(echo $hg_status | grep "^\M" | wc -l)"
HG_ADDED="$(echo $hg_status | grep "^\A" | wc -l)"
HG_REMOVED="$(echo $hg_status | grep "^\R" | wc -l)"
HG_DELETED="$(echo $hg_status | grep "^\!" | wc -l)"
HG_AHEAD="$(hg log -r "draft()" | grep "summary" | wc -l)"
# HG_BEHIND="$(hg incoming | grep "summary" | wc -l)"
fi
}
function hg_prompt_info { function hg_prompt_info {
local dir branch dirty update_hg_vars
if ! dir=$(hg_root); then if $(hg id >/dev/null 2>&1); then
return local STATUS="$ZSH_THEME_HG_PROMPT_PREFIX$ZSH_THEME_HG_PROMPT_BRANCH$HG_REV_BRANCH%{${reset_color}%}"
if [[ "$HG_BEHIND" -ne "0" ]]; then
STATUS="$STATUS$ZSH_THEME_HG_PROMPT_BEHIND$HG_BEHIND%{$reset_color%}"
fi fi
if [[ "$HG_AHEAD" -ne "0" ]]; then
if [[ ! -f "$dir/.hg/branch" ]]; then STATUS="$STATUS$ZSH_THEME_HG_PROMPT_AHEAD$HG_AHEAD%{$reset_color%}"
branch=default fi
else STATUS="$STATUS$ZSH_THEME_HG_PROMPT_SEPARATOR"
branch="$(<"$dir/.hg/branch")" if [[ "$HG_MODIFIED" -ne "0" ]]; then
STATUS="$STATUS$ZSH_THEME_HG_PROMPT_MODIFIED$HG_MODIFIED%{$reset_color%}"
fi
if [[ "$HG_ADDED" -ne "0" ]]; then
STATUS="$STATUS$ZSH_THEME_HG_PROMPT_ADDED$HG_ADDED%{$reset_color%}"
fi
if [[ "$HG_REMOVED" -ne "0" ]]; then
STATUS="$STATUS$ZSH_THEME_HG_PROMPT_REMOVED$HG_REMOVED%{$reset_color%}"
fi
if [[ "$HG_DELETED" -ne "0" ]]; then
STATUS="$STATUS$ZSH_THEME_HG_PROMPT_DELETED$HG_DELETED%{$reset_color%}"
fi
if [[ "$HG_UNKNOWN" -ne "0" ]]; then
STATUS="$STATUS$ZSH_THEME_HG_PROMPT_UNKNOWN$HG_UNKNOWN%{$reset_color%}"
fi
if [[ "$HG_MODIFIED" -eq "0" && "$HG_ADDED" -eq "0" && "$HG_REMOVED" -eq "0" && "$HG_DELETED" -eq "0" && "$HG_UNKNOWN" -eq "0" ]]; then
STATUS="$STATUS$ZSH_THEME_HG_PROMPT_CLEAN"
fi
STATUS="$STATUS%{${reset_color}%}$ZSH_THEME_HG_PROMPT_SUFFIX"
echo "$STATUS"
fi fi
dirty="$(hg_dirty)"
echo "${ZSH_THEME_HG_PROMPT_PREFIX}${branch:gs/%/%%}${dirty}${ZSH_THEME_HG_PROMPT_SUFFIX}"
} }
function hg_dirty { # Default values for the appearance of the prompt.
# Do nothing if clean / dirty settings aren't defined ZSH_THEME_HG_PROMPT_PREFIX="("
if [[ -z "$ZSH_THEME_HG_PROMPT_DIRTY" && -z "$ZSH_THEME_HG_PROMPT_CLEAN" ]]; then ZSH_THEME_HG_PROMPT_SUFFIX=")"
return ZSH_THEME_HG_PROMPT_SEPARATOR="|"
fi ZSH_THEME_HG_PROMPT_BRANCH="%{$fg_bold[magenta]%}"
ZSH_THEME_HG_PROMPT_BEHIND="%{↓%G%}"
# Check if there are modifications ZSH_THEME_HG_PROMPT_AHEAD="%{↑%G%}"
local hg_status ZSH_THEME_HG_PROMPT_MODIFIED="%{$fg[red]%}%{●%G%}"
if [[ "$DISABLE_UNTRACKED_FILES_DIRTY" = true ]]; then ZSH_THEME_HG_PROMPT_ADDED="%{$fg[blue]%}%{✚%G%}"
if ! hg_status="$(hg status -q 2>/dev/null)"; then ZSH_THEME_HG_PROMPT_REMOVED="%{$fg[red]%}%{✖%G%}"
return ZSH_THEME_HG_PROMPT_DELETED="%{$fg[red]%}%{🗑️%G%}"
fi ZSH_THEME_HG_PROMPT_UNKNOWN="%{$fg[cyan]%}%{…%G%}"
else ZSH_THEME_HG_PROMPT_CLEAN="%{$fg_bold[green]%}%{✔%G%}"
if ! hg_status="$(hg status 2>/dev/null)"; then
return
fi
fi
# grep exits with 0 when dirty
if command grep -Eq '^\s*[ACDIMR!?L].*$' <<< "$hg_status"; then
echo $ZSH_THEME_HG_PROMPT_DIRTY
return
fi
echo $ZSH_THEME_HG_PROMPT_CLEAN
}