1
0
Fork 0

zsh: cleanup, replace temp file with a fifo

This commit is contained in:
Von Random 2022-08-21 04:04:14 +03:00
parent 16e0c4e6dc
commit ccfbfe8933
3 changed files with 40 additions and 73 deletions

View file

@ -9,7 +9,7 @@ confdir=$(dirname $0)
conflist=( conflist=(
settings.zsh settings.zsh
functions.zsh functions.zsh
prompt-powerline-native.zsh prompt-powerline.zsh
) )
for config in $conflist; do for config in $conflist; do

View file

@ -1,32 +0,0 @@
function powerline_precmd() {
#-modules venv,user,host,ssh,cwd,perms,git,hg,jobs,exit,root \
PS1=$(
powerline-go \
-modules user,host,ssh,cwd,perms,git \
)
#-newline \
#git
PS1=$PS1$'\n\U01f525 '
}
function install_powerline_precmd() {
for s in $precmd_functions[@]; do
if [[ $s = powerline_precmd ]]; then
return
fi
done
precmd_functions+=(powerline_precmd)
}
if [[ "$TERM" != "linux" ]] && [[ -x $(whence powerline-go) ]]; then
install_powerline_precmd
fi
function zle-line-init zle-keymap-select {
local seq=$'\e[2 q'
[[ $KEYMAP == vicmd ]] && seq=$'\e[4 q'
printf $seq
}
zle -N zle-line-init
zle -N zle-keymap-select

View file

@ -2,10 +2,8 @@ prompt_fmtn='[ %%{\e[2;3m%%}%s%%{\e[0m%%} ] '
printf -v PROMPT2 $prompt_fmtn '%_' printf -v PROMPT2 $prompt_fmtn '%_'
printf -v PROMPT3 $prompt_fmtn '?#' printf -v PROMPT3 $prompt_fmtn '?#'
printf -v PROMPT4 $prompt_fmtn '+%N:%i' printf -v PROMPT4 $prompt_fmtn '+%N:%i'
prompt_state_file=/tmp/zsh_gitstatus_$$.tmp
PROMPT= prompt_fifo=~/.zsh_gitstatus_$$
prev_color=
typeset -A prompt_symbols=( typeset -A prompt_symbols=(
sep_a $'\ue0b0' sep_a $'\ue0b0'
sep_b $'\ue0b1' sep_b $'\ue0b1'
@ -36,34 +34,30 @@ typeset -A prompt_colors=(
git_unmerged 30 git_unmerged 30
) )
precmd.prompt.clear() {
PROMPT=
}
precmd.prompt.add() { precmd.prompt.add() {
(($#<2)) && return 1 (( $# < 2 )) && return 1
typeset data=$1 color=$2 typeset data=$1 color=$2
if [[ $color == same ]]; then if [[ $color == same ]]; then
PROMPT+="$prompt_symbols[sep_b] $data " prompt_string+="$prompt_symbols[sep_b] $data "
else else
if ((${#PROMPT})); then if (( ${#prompt_string} )); then
PROMPT+="%F{$prev_color}%K{$color}$prompt_symbols[sep_a]%F{$prompt_colors[fg]} $data " prompt_string+="%F{$prev_color}%K{$color}$prompt_symbols[sep_a]%F{$prompt_colors[fg]} $data "
else else
PROMPT="%K{$color}%F{$prompt_colors[fg]} $data " prompt_string="%K{$color}%F{$prompt_colors[fg]} $data "
fi fi
prev_color=$color prev_color=$color
fi fi
} }
precmd.prompt.bang() { precmd.prompt.bang() {
PROMPT+="%F{$prev_color}%k$prompt_symbols[sep_a]%f$prompt_symbols[bang] " prompt_string+="%F{$prev_color}%k$prompt_symbols[sep_a]%f$prompt_symbols[bang] "
} }
precmd.prompt.user() { precmd.prompt.user() {
typeset user_color typeset c=user
((UID)) && user_color=$prompt_colors[user] || user_color=$prompt_colors[root] (( UID )) || c=root
precmd.prompt.add %n $user_color precmd.prompt.add %n $prompt_colors[$c]
} }
precmd.prompt.ssh() { precmd.prompt.ssh() {
@ -86,16 +80,16 @@ precmd.prompt.cwd() {
[[ -z $cwd ]] && return [[ -z $cwd ]] && return
typeset -a cwd_array=(${(ps:/:)cwd}) typeset -a cwd_array=(${(ps:/:)cwd})
if ((${#cwd_array} > limit)); then if (( ${#cwd_array} > limit )); then
precmd.prompt.add $prompt_symbols[ellipsis] $prompt_colors[dirs] precmd.prompt.add $prompt_symbols[ellipsis] $prompt_colors[dirs]
while ((${#cwd_array} > limit)); do while (( ${#cwd_array} > limit )); do
shift cwd_array shift cwd_array
done done
else else
precmd.prompt.add $cwd_array[1] $prompt_colors[dirs] precmd.prompt.add $cwd_array[1] $prompt_colors[dirs]
shift cwd_array shift cwd_array
fi fi
while ((${#cwd_array})); do while (( ${#cwd_array} )); do
precmd.prompt.add $cwd_array[1] same precmd.prompt.add $cwd_array[1] same
shift cwd_array shift cwd_array
done done
@ -105,27 +99,35 @@ precmd.prompt.ro() {
[[ -w . ]] || precmd.prompt.add $prompt_symbols[ro] $prompt_colors[ro] [[ -w . ]] || precmd.prompt.add $prompt_symbols[ro] $prompt_colors[ro]
} }
precmd.is_git_repo() {
typeset -g prompt_git_dir
prompt_git_dir=$(git rev-parse --git-dir 2>/dev/null) || return 1
[[ ! -e $prompt_git_dir/nozsh ]]
}
precmd.prompt.pre_git() { precmd.prompt.pre_git() {
precmd.prompt.add "$prompt_symbols[git] $prompt_symbols[ellipsis]" $prompt_colors[git_branch] precmd.prompt.add "$prompt_symbols[git] $prompt_symbols[ellipsis]" $prompt_colors[git_branch]
} }
precmd.prompt.git() { precmd.prompt.git() {
typeset raw_status typeset raw_status
raw_status=$(flock -n $prompt_state_file git --no-optional-locks status --porcelain -bu 2>/dev/null) || return 0 raw_status=$(flock -n $prompt_git_dir git --no-optional-locks status --porcelain -bu 2>/dev/null) || return 0
typeset -A count typeset -A count
typeset branch_status git_status_string IFS= typeset branch_status git_status_string IFS=
while read line; do while read line; do
if [[ $line[1,2] == '##' ]]; then case $line[1,2] in
branch_status=${line[4,-1]%%...*} ('##')
[[ $line =~ behind ]] && branch_status+=? branch_status=${line[4,-1]%%...*}
[[ $line =~ ahead ]] && branch_status+=! [[ $line =~ behind ]] && branch_status+=?
precmd.prompt.add "$prompt_symbols[git] $branch_status" $prompt_colors[git_branch] [[ $line =~ ahead ]] && branch_status+=!
fi precmd.prompt.add "$prompt_symbols[git] $branch_status" $prompt_colors[git_branch]
[[ $line[1,2] == '??' ]] && (( count[git_untracked]++ )) ;;
[[ $line[1,2] =~ .[MD] ]] && (( count[git_unstaged]++ )) (?[MD]) (( count[git_unstaged]++ )) ;|
[[ $line[1,2] =~ [MDARC]. ]] && (( count[git_staged]++ )) ([MDARC]?) (( count[git_staged]++ )) ;|
[[ $line[1,2] =~ [ADU]{2} ]] && (( count[git_unmerged]++ )) ('??') (( count[git_untracked]++ )) ;|
([ADU][ADU]) (( count[git_unmerged]++ ))
esac
done <<< $raw_status done <<< $raw_status
for i in git_unstaged git_staged git_untracked git_unmerged; do for i in git_unstaged git_staged git_untracked git_unmerged; do
@ -133,13 +135,8 @@ precmd.prompt.git() {
done done
} }
precmd.is_git_repo() {
read -r git_dir < <(git rev-parse --git-dir 2>/dev/null) || return 1
[[ ! -e $git_dir/nozsh ]]
}
precmd.prompt() { precmd.prompt() {
precmd.prompt.clear typeset -g prompt_string= prev_color=
precmd.prompt.user precmd.prompt.user
precmd.prompt.ssh precmd.prompt.ssh
precmd.prompt.host precmd.prompt.host
@ -148,15 +145,16 @@ precmd.prompt() {
} }
precmd.git_update() { precmd.git_update() {
umask 077
precmd.prompt precmd.prompt
precmd.prompt.git precmd.prompt.git
precmd.prompt.bang precmd.prompt.bang
> $prompt_state_file <<< $PROMPT [[ ! -p $prompt_fifo ]] && mkfifo -m 0600 $prompt_fifo
echo -n $prompt_string > $prompt_fifo &!
kill -s USR1 $$ kill -s USR1 $$
} }
precmd() { precmd() {
PROMPT=
if precmd.is_git_repo; then if precmd.is_git_repo; then
precmd.prompt precmd.prompt
precmd.prompt.pre_git precmd.prompt.pre_git
@ -166,15 +164,16 @@ precmd() {
precmd.prompt precmd.prompt
precmd.prompt.bang precmd.prompt.bang
fi fi
PROMPT=$prompt_string
} }
TRAPUSR1() { TRAPUSR1() {
PROMPT=$(<$prompt_state_file) PROMPT=$(<$prompt_fifo)
zle && zle reset-prompt zle && zle reset-prompt
} }
TRAPEXIT() { TRAPEXIT() {
[[ -f $prompt_state_file ]] && rm $prompt_state_file [[ -p $prompt_fifo ]] && rm $prompt_fifo
} }
function zle-line-init zle-keymap-select { function zle-line-init zle-keymap-select {