1
0
Fork 0
mirror of https://github.com/hcartiaux/dotfiles.git synced 2024-10-18 17:25:23 +02:00
dotfiles/zsh/prompt_hyacinthe_setup

134 lines
2.8 KiB
Text
Raw Normal View History

2013-05-02 23:38:51 +02:00
# hyacinthe prompt theme with git support
# Derivated from <http://sebastiancelis.com/2009/11/16/zsh-prompt-git-users/>
2018-02-25 13:13:38 +01:00
prompt_hyacinthe_help() {
2013-05-02 23:38:51 +02:00
cat <<'EOF'
This prompt is color-scheme-able. You can invoke it thus:
prompt hyacinthe [<color1> [<color2> [<color3> [<color4> [<color5>]]]]]
The defaults colors are red, cyan, green, white, and yellow, respectively.
EOF
}
2018-02-25 13:13:38 +01:00
prompt_hyacinthe_setup() {
2013-05-02 23:38:51 +02:00
local p_date p_userpwd p_end p_win
local -A pc
setopt prompt_subst
pcc[1]=${1:-${${SSH_CLIENT+'yellow'}:-'red'}}
pcc[2]=${2:-'cyan'}
2018-02-25 13:13:38 +01:00
if [[ $USERNAME == "root" ]]; then
2013-05-02 23:38:51 +02:00
pcc[3]=${3:-'red'}
else
pcc[3]=${3:-'green'}
fi
pcc[4]=${4:-'white'}
pcc[5]=${5:-'yellow'}
pc['\[']="%F{$pcc[1]}["
pc['\]']="%F{$pcc[1]}]"
pc['<']="%F{$pcc[1]}<"
pc['>']="%F{$pcc[1]}>"
pc['\(']="%F{$pcc[1]}("
pc['\)']="%F{$pcc[1]})"
p_date="$pc['\[']%F{$pcc[2]}%D{%R}$pc['\]']"
2018-02-25 13:13:38 +01:00
[[ -n $WINDOW ]] && p_win="$pc['\(']%F{$pcc[4]}$WINDOW$pc['\)']"
2013-05-02 23:38:51 +02:00
p_userpwd="$pc['<']%F{$pcc[3]}%n@%m$p_win%F{$pcc[5]}:%F{$pcc[4]}%~$pc['>']"
p_end="%f%B%#%b "
zle_highlight[(r)default:*]=default:$pcc[2]
prompt="$p_date $p_userpwd $p_end"
PS2='%(4_.\.)%3_> %E'
2018-02-25 13:13:38 +01:00
add-zsh-hook precmd update_git_vars
2013-05-02 23:38:51 +02:00
add-zsh-hook preexec preexec_update_git_vars
2018-02-25 13:13:38 +01:00
add-zsh-hook chpwd update_git_vars
2013-05-02 23:38:51 +02:00
}
2018-02-25 13:13:38 +01:00
update_current_git_vars() {
2013-05-02 23:38:51 +02:00
unset __CURRENT_GIT_BRANCH
unset __CURRENT_GIT_BRANCH_STATUS
unset __CURRENT_GIT_BRANCH_IS_DIRTY
local st="$(git status 2>/dev/null)"
2018-02-25 13:13:38 +01:00
if [[ -n $st ]]; then
local -a arr
arr=(${(f)st})
if [[ $arr[1] =~ 'Not currently on any branch.' ]]; then
__CURRENT_GIT_BRANCH='no-branch'
else
__CURRENT_GIT_BRANCH="${arr[1][(w)4]}";
fi
if [[ $arr[2] =~ 'Your branch is' ]]; then
if [[ $arr[2] =~ 'ahead' ]]; then
__CURRENT_GIT_BRANCH_STATUS='ahead'
elif [[ $arr[2] =~ 'diverged' ]]; then
__CURRENT_GIT_BRANCH_STATUS='diverged'
elif [[ $arr[2] =~ 'up-to-date' || $arr[2] =~ 'up to date' ]]; then
__CURRENT_GIT_BRANCH_STATUS=''
else
__CURRENT_GIT_BRANCH_STATUS='behind'
fi
fi
if [[ ! $st =~ 'nothing to commit' ]]; then
__CURRENT_GIT_BRANCH_IS_DIRTY='1'
fi
2013-05-02 23:38:51 +02:00
fi
}
2018-02-25 13:13:38 +01:00
prompt_git_info() {
2013-05-02 23:38:51 +02:00
if [ -n "$__CURRENT_GIT_BRANCH" ]; then
2018-02-25 13:13:38 +01:00
local s="("
case "$__CURRENT_GIT_BRANCH_STATUS" in
ahead)
s+="↑"
;;
diverged)
s+="↕"
;;
behind)
s+="↓"
;;
esac
if [ -n "$__CURRENT_GIT_BRANCH_IS_DIRTY" ]; then
s+="✗"
fi
s+="$__CURRENT_GIT_BRANCH"
s+=")"
printf " %s%s" "%{${fg[yellow]}%}" $s
2013-05-02 23:38:51 +02:00
fi
}
2018-02-25 13:13:38 +01:00
preexec_update_git_vars() {
2013-05-02 23:38:51 +02:00
case "$1" in
2018-02-25 13:13:38 +01:00
git*)
__EXECUTED_GIT_COMMAND=1
;;
2013-05-02 23:38:51 +02:00
esac
}
update_git_vars() {
update_current_git_vars
2018-02-25 13:13:38 +01:00
if [ "$__CURRENT_GIT_BRANCH" != "no-branch" ]; then
2013-05-02 23:38:51 +02:00
RPROMPT="%F{$pcc[3]}$(prompt_git_info)"
fi
}
prompt_hyacinthe_setup "$@"