diff options
author | June McEnroe <june@causal.agency> | 2013-01-26 17:46:57 -0500 |
---|---|---|
committer | June McEnroe <june@causal.agency> | 2013-01-26 17:46:57 -0500 |
commit | 7d9d17639e04d4e6adcb3a0289acffea40256474 (patch) | |
tree | 13f738341bebbf0f0f10c37614180a778b5097ba /.zsh/gitprompt.zsh | |
parent | Actually port awesome config to 3.5 (diff) | |
download | src-7d9d17639e04d4e6adcb3a0289acffea40256474.tar.gz src-7d9d17639e04d4e6adcb3a0289acffea40256474.zip |
Add git prompt
Diffstat (limited to '')
-rw-r--r-- | .zsh/gitprompt.zsh | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/.zsh/gitprompt.zsh b/.zsh/gitprompt.zsh new file mode 100644 index 00000000..0a420786 --- /dev/null +++ b/.zsh/gitprompt.zsh @@ -0,0 +1,86 @@ +# Ported from http://sebastiancelis.com/2009/11/16/zsh-prompt-git-users/ + +function gitprompt_update +{ + unset __CURRENT_GIT_BRANCH + unset __CURRENT_GIT_BRANCH_STATUS + unset __CURRENT_GIT_BRANCH_IS_DIRTY + + local st="$(git status 2>/dev/null)" + 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' + else + __CURRENT_GIT_BRANCH_STATUS='behind' + fi + fi + + if [[ ! $st =~ 'nothing' ]]; then # Untracked files count as clean + __CURRENT_GIT_BRANCH_IS_DIRTY='1' + fi + fi +} + +# Changed around the formatting here a bunch +function gitprompt +{ + if [ -n "$__CURRENT_GIT_BRANCH" ]; then + local s="%{${fg[yellow]}%}" + if [ -n "$__CURRENT_GIT_BRANCH_IS_DIRTY" ]; then + s+="⚡" + else + s+=":" + fi + s+="$__CURRENT_GIT_BRANCH" + case "$__CURRENT_GIT_BRANCH_STATUS" in + ahead) + s+="↑" + ;; + diverged) + s+="↕" + ;; + behind) + s+="↓" + ;; + esac + + echo $s + fi +} + +function gitprompt_preexec +{ + case "$1" in + g*) # Switched from git* to also detect my short aliases + __EXECUTED_GIT_COMMAND=1 + ;; + esac +} + +function gitprompt_precmd +{ + if [ -n "$__EXECUTED_GIT_COMMAND" ]; then + gitprompt_update + unset __EXECUTED_GIT_COMMAND + fi +} + +typeset -ga preexec_functions +typeset -ga precmd_functions +typeset -ga chpwd_functions + +preexec_functions+='gitprompt_preexec' +precmd_functions+='gitprompt_precmd' +chpwd_functions+='gitprompt_update' |