1
0
Fork 0
mirror of https://github.com/hcartiaux/dotfiles.git synced 2024-10-18 09:15:24 +02:00

[zsh] git prompt file, shfmt run

This commit is contained in:
Hyacinthe Cartiaux 2018-02-25 13:13:38 +01:00
parent 535522dc91
commit 95e7bad149

View file

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