perf(mercurial): optimize utility functions

This commit is contained in:
Marc Cornellà 2021-12-02 11:12:18 +01:00
parent d058801a18
commit 4119f53004
No known key found for this signature in database
GPG Key ID: 0314585E776A9C1B
2 changed files with 91 additions and 68 deletions

View File

@ -21,55 +21,6 @@ alias hgo='hg outgoing'
alias hglg='hg log --stat -v' alias hglg='hg log --stat -v'
alias hglgp='hg log --stat -p -v' alias hglgp='hg log --stat -p -v'
function in_hg() {
if $(hg branch > /dev/null 2>&1); then
echo 1
fi
}
function hg_get_branch_name() {
branch=`hg branch 2>/dev/null`
if [ $? -eq 0 ]; then
echo $branch
fi
unset branch
}
function hg_prompt_info {
local info rev branch dirty
if ! info=$(hg id --id --branch 2>/dev/null); then
return
fi
rev="${info[(w)1]}"
branch="${${info[(w)2]}:gs/%/%%}"
if [[ "$rev" = *+ ]]; then
dirty="$ZSH_THEME_HG_PROMPT_DIRTY"
else
dirty="$ZSH_THEME_HG_PROMPT_CLEAN"
fi
echo "${ZSH_THEME_HG_PROMPT_PREFIX}${branch}${dirty}${ZSH_THEME_HG_PROMPT_SUFFIX}"
}
function hg_dirty_choose {
hg status -mar 2> /dev/null | command grep -Eq '^\s*[ACDIM!?L]'
if [ $? -eq 0 ]; then
if [ $pipestatus[-1] -eq 0 ]; then
# Grep exits with 0 when "One or more lines were selected", return "dirty".
echo $1
return
fi
fi
echo $2
}
function hg_dirty {
hg_dirty_choose $ZSH_THEME_HG_PROMPT_DIRTY $ZSH_THEME_HG_PROMPT_CLEAN
}
function hgic() { function hgic() {
hg incoming "$@" | grep "changeset" | wc -l hg incoming "$@" | grep "changeset" | wc -l
} }
@ -78,8 +29,78 @@ function hgoc() {
hg outgoing "$@" | grep "changeset" | wc -l hg outgoing "$@" | grep "changeset" | wc -l
} }
function hg_get_bookmark_name() { # functions
if [ $(in_hg) ]; then function hg_root() {
echo $(hg id -B) local dir="$PWD"
while [[ "$dir" != "/" ]]; do
if [[ -d "$dir/.hg" ]]; then
echo "$dir"
return 0
fi fi
dir="${dir:h}"
done
return 1
}
function in_hg() {
hg_root >/dev/null
}
function hg_get_branch_name() {
local dir
if ! dir=$(hg_root); then
return
fi
if [[ ! -f "$dir/.hg/branch" ]]; then
echo default
return
fi
echo "$(<"$dir/.hg/branch")"
}
function hg_get_bookmark_name() {
local dir
if ! dir=$(hg_root); then
return
fi
if [[ ! -f "$dir/.hg/bookmarks.current" ]]; then
return
fi
echo "$(<"$dir/.hg/bookmarks.current")"
}
function hg_prompt_info {
local dir branch dirty
if ! dir=$(hg_root); then
return
fi
if [[ ! -f "$dir/.hg/branch" ]]; then
branch=default
else
branch="$(<"$dir/.hg/branch")"
fi
dirty="$(hg_dirty)"
echo "${ZSH_THEME_HG_PROMPT_PREFIX}${branch:gs/%/%%}${dirty}${ZSH_THEME_HG_PROMPT_SUFFIX}"
}
function hg_dirty {
local hg_status
if ! hg_status="$(hg status -mar 2>/dev/null)"; then
return
fi
# grep exits with 0 when dirty
if command grep -Eq '^\s*[ACDIM!?L]' <<< "$hg_status"; then
echo $ZSH_THEME_HG_PROMPT_DIRTY
return
fi
echo $ZSH_THEME_HG_PROMPT_CLEAN
} }

View File

@ -2,19 +2,21 @@ ZSH_THEME_GIT_PROMPT_PREFIX="%{$reset_color%}%{$fg[white]%}["
ZSH_THEME_GIT_PROMPT_SUFFIX="" ZSH_THEME_GIT_PROMPT_SUFFIX=""
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}●%{$fg[white]%}]%{$reset_color%} " ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[red]%}●%{$fg[white]%}]%{$reset_color%} "
ZSH_THEME_GIT_PROMPT_CLEAN="]%{$reset_color%} " ZSH_THEME_GIT_PROMPT_CLEAN="]%{$reset_color%} "
ZSH_THEME_SVN_PROMPT_PREFIX=$ZSH_THEME_GIT_PROMPT_PREFIX
ZSH_THEME_SVN_PROMPT_SUFFIX=$ZSH_THEME_GIT_PROMPT_SUFFIX ZSH_THEME_SVN_PROMPT_PREFIX="$ZSH_THEME_GIT_PROMPT_PREFIX"
ZSH_THEME_SVN_PROMPT_DIRTY=$ZSH_THEME_GIT_PROMPT_DIRTY ZSH_THEME_SVN_PROMPT_SUFFIX="$ZSH_THEME_GIT_PROMPT_SUFFIX"
ZSH_THEME_SVN_PROMPT_CLEAN=$ZSH_THEME_GIT_PROMPT_CLEAN ZSH_THEME_SVN_PROMPT_DIRTY="$ZSH_THEME_GIT_PROMPT_DIRTY"
ZSH_THEME_HG_PROMPT_PREFIX=$ZSH_THEME_GIT_PROMPT_PREFIX ZSH_THEME_SVN_PROMPT_CLEAN="$ZSH_THEME_GIT_PROMPT_CLEAN"
ZSH_THEME_HG_PROMPT_SUFFIX=$ZSH_THEME_GIT_PROMPT_SUFFIX
ZSH_THEME_HG_PROMPT_DIRTY=$ZSH_THEME_GIT_PROMPT_DIRTY ZSH_THEME_HG_PROMPT_PREFIX="$ZSH_THEME_GIT_PROMPT_PREFIX"
ZSH_THEME_HG_PROMPT_CLEAN=$ZSH_THEME_GIT_PROMPT_CLEAN ZSH_THEME_HG_PROMPT_SUFFIX="$ZSH_THEME_GIT_PROMPT_SUFFIX"
ZSH_THEME_HG_PROMPT_DIRTY="$ZSH_THEME_GIT_PROMPT_DIRTY"
ZSH_THEME_HG_PROMPT_CLEAN="$ZSH_THEME_GIT_PROMPT_CLEAN"
vcs_status() { vcs_status() {
if [[ $(whence in_svn) != "" ]] && in_svn; then if (( ${+functions[in_svn]} )) && in_svn; then
svn_prompt_info svn_prompt_info
elif [[ $(whence in_hg) != "" ]] && in_hg; then elif (( ${+functions[in_hg]} )) && in_hg; then
hg_prompt_info hg_prompt_info
else else
git_prompt_info git_prompt_info