1
0
Fork 0

a better way to deal with git prompt

This commit is contained in:
Von Random 2019-02-01 14:52:49 +03:00
parent 8ffaf95b9d
commit 366200d3e8

31
zshrc
View file

@ -72,12 +72,12 @@ bindkey '^x^e' edit-command-line
# }}} # }}}
# {{{ prompt # {{{ prompt
prompt_ln1='[ %(!.%F{red}.%F{black})%n%f %m:%F{black}%d%f ]' prompt_ln1='[ %(!.%F{red}.%F{black})%n%f %m:%F{black}%d%f ]'
prompt_ln2=$'\n''%(!.%F{red}.%F{black})>%f ' prompt_ln2=$'\n%(!.%F{red}.%F{black})>%f '
prompt_state_file="/tmp/zsh_gitstatus_$$.tmp" prompt_state_file="/tmp/zsh_gitstatus_$$.tmp"
PROMPT="$prompt_ln1$prompt_ln2" PROMPT="$prompt_ln1$prompt_ln2"
PROMPT2='%b%f%_%(!.%F{red}.%F{black})>%f%b ' PROMPT2='%_%(!.%F{red}.%F{black})>%f '
PROMPT3='%b%f?%(!.%F{red}.%F{black})#%f%b ' PROMPT3='?%(!.%F{red}.%F{black})#%f '
PROMPT4='%b%f+%N:%i%(!.%F{red}.%F{black})>%f%b ' PROMPT4='+%N:%i%(!.%F{red}.%F{black})>%f '
precmd.title() { precmd.title() {
case $TERM in case $TERM in
(screen*) printf '\033k%s\033\' ${HOST%%.*};; (screen*) printf '\033k%s\033\' ${HOST%%.*};;
@ -96,8 +96,6 @@ precmd.is_git_repo() {
return 1 return 1
} }
precmd.git() { precmd.git() {
precmd.is_git_repo || return 0
typeset raw_status typeset raw_status
raw_status="$(flock -w 0 $prompt_state_file git --no-optional-locks status --porcelain -bu 2>/dev/null)" raw_status="$(flock -w 0 $prompt_state_file git --no-optional-locks status --porcelain -bu 2>/dev/null)"
(($?)) && return 0 (($?)) && return 0
@ -119,31 +117,30 @@ precmd.git() {
(( unmerged_count )) && git_status+="%F{magenta}*$unmerged_count" (( unmerged_count )) && git_status+="%F{magenta}*$unmerged_count"
[[ -z $git_status ]] && git_status="%F{green}ok" [[ -z $git_status ]] && git_status="%F{green}ok"
printf ' { %s \ue0a0 %s%%f }' $branch_info $git_status printf ' { %s \ue0a0 %s%%f }' $branch_info $git_status > $prompt_state_file
} }
precmd.prompt() { precmd.prompt() {
if (($#)); then PROMPT="$prompt_ln1$1$prompt_ln2"
PROMPT="$prompt_ln1$prompt_git_data$prompt_ln2"
else
PROMPT="$prompt_ln1$prompt_ln2"
fi
} }
precmd.git_update() { precmd.git_update() {
precmd.git > $prompt_state_file precmd.git
kill -s USR1 $$ kill -s USR1 $$
} }
precmd() { precmd() {
precmd.title precmd.title
precmd.prompt if precmd.is_git_repo; then
precmd.prompt $' { \ue0a0 }'
precmd.git_update &! precmd.git_update &!
else
precmd.prompt
fi
} }
TRAPUSR1() { TRAPUSR1() {
prompt_git_data="$(<$prompt_state_file)" precmd.prompt "$(<$prompt_state_file)"
precmd.prompt 1
zle && zle reset-prompt zle && zle reset-prompt
} }
TRAPEXIT() { TRAPEXIT() {
[[ -r $prompt_state_file ]] && rm $prompt_state_file [[ -f $prompt_state_file ]] && rm $prompt_state_file
} }
# }}} # }}}
# {{{ aliases # {{{ aliases