Skip to content

Instantly share code, notes, and snippets.

@mhoyer
Created February 16, 2011 18:58
Show Gist options
  • Save mhoyer/829926 to your computer and use it in GitHub Desktop.
Save mhoyer/829926 to your computer and use it in GitHub Desktop.
Cached hg prompt
HG_PROMPT="%{$fg[cyan]%}ʜɢ%{$reset_color%}>"
function prompt_char {
[ $HG_CURRENT_ROOT ] && echo $HG_PROMPT && return
#hg root >/dev/null 2>/dev/null && echo "$HG_PROMPT>" && return
#git branch >/dev/null 2>/dev/null && echo '±' && return
echo '>'
}
function battery_charge {
echo `$BAT_CHARGE` 2>/dev/null
}
function virtualenv_info {
[ $VIRTUAL_ENV ] && echo '('`basename $VIRTUAL_ENV`') '
}
function find_hg_root {
[ ! -d "$1" ] && find_hg_root "$PWD" && return
CURRENT_WD=$(readlink -f "$1")
# DEBUG echo " ? $CWD" >&2
# check if current $1 is matching existing HG_CURRENT_ROOT
[ "$CURRENT_WD" = "/" ] && export HG_CURRENT_ROOT= && return
[ -d "$1/.hg" ] && export HG_CURRENT_ROOT="$CURRENT_WD" && return
find_hg_root $1/..
}
function update_hg_prompt_info {
[ ! $HG_CURRENT_ROOT ] && export HG_PROMPT_INFO= && return
if [ -d "$HG_CURRENT_ROOT/.hg" ]; then
local HG_ROOT="$HG_CURRENT_ROOT/.hg"
if [ ! -e "$HG_ROOT/prompt_cache" ]; then
echo "Creating $HG_ROOT/prompt_cache" >&2
touch "$HG_ROOT/prompt_cache"
HG_PROMPT_INFO=$(hg_prompt_info)
else
LAST_UPDATE_TIME=`stat -c %Y $HG_ROOT/prompt_cache`
DIRSTATE_TIME=`stat -c %Y $HG_ROOT/dirstate`
if [[ $DIRSTATE_TIME -gt $LAST_UPDATE_TIME || "$1" == "force" ]]; then
touch "$HG_ROOT/prompt_cache"
HG_PROMPT_INFO=$(hg_prompt_info)
fi
fi
HG_PROMPT_INFO=${HG_PROMPT_INFO:-$(hg_prompt_info)}
export HG_PROMPT_INFO
else
export HG_PROMPT_INFO=
fi
}
function hg_prompt_info {
echo " >> Updating HG prompt" >&2
hg prompt --angle-brackets "\
< on $(_xterm_color 166 "<branch>")>\
< at $(_xterm_color 203 "<tags|, >")>\
$(_xterm_color 226 "<status|modified|unknown><update>")<
patches: <patches|join( → )|pre_applied(%{$fg[yellow]%})|post_applied(%{$reset_color%})|pre_unapplied(%{$fg_bold[black]%})|post_unapplied(%{$reset_color%})>>" 2>/dev/null
}
HG_PROMPT_INFO=
# see http://www.calmar.ws/vim/256-xterm-24bit-rgb-color-chart.html for color codes
PROMPT_PREFIX="$(_xterm_color 243 '%n')$(_xterm_color 240 '@')$(_xterm_color 238 '%m')"
PROMPT='$PROMPT_PREFIX $(_xterm_color 76 "${PWD/#$HOME/~}") $HG_PROMPT_INFO
$(prompt_char) '
RPROMPT='$(battery_charge)$RPROMPT_SUFFIX'
ZSH_THEME_GIT_PROMPT_PREFIX=" on %{$fg[magenta]%}"
ZSH_THEME_GIT_PROMPT_SUFFIX="%{$reset_color%}"
ZSH_THEME_GIT_PROMPT_DIRTY="%{$fg[green]%}!"
ZSH_THEME_GIT_PROMPT_UNTRACKED="%{$fg[green]%}?"
ZSH_THEME_GIT_PROMPT_CLEAN=""
# hook up the functions to ZSH events
typeset -ga chpwd_functions
typeset -ga precmd_functions
chpwd_functions+='find_hg_root'
precmd_functions+='update_hg_prompt_info'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment