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=(
settings.zsh
functions.zsh
prompt-powerline-native.zsh
prompt-powerline.zsh
)
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 PROMPT3 $prompt_fmtn '?#'
printf -v PROMPT4 $prompt_fmtn '+%N:%i'
prompt_state_file=/tmp/zsh_gitstatus_$$.tmp
PROMPT=
prev_color=
prompt_fifo=~/.zsh_gitstatus_$$
typeset -A prompt_symbols=(
sep_a $'\ue0b0'
sep_b $'\ue0b1'
@ -36,34 +34,30 @@ typeset -A prompt_colors=(
git_unmerged 30
)
precmd.prompt.clear() {
PROMPT=
}
precmd.prompt.add() {
(( $# < 2 )) && return 1
typeset data=$1 color=$2
if [[ $color == same ]]; then
PROMPT+="$prompt_symbols[sep_b] $data "
prompt_string+="$prompt_symbols[sep_b] $data "
else
if ((${#PROMPT})); then
PROMPT+="%F{$prev_color}%K{$color}$prompt_symbols[sep_a]%F{$prompt_colors[fg]} $data "
if (( ${#prompt_string} )); then
prompt_string+="%F{$prev_color}%K{$color}$prompt_symbols[sep_a]%F{$prompt_colors[fg]} $data "
else
PROMPT="%K{$color}%F{$prompt_colors[fg]} $data "
prompt_string="%K{$color}%F{$prompt_colors[fg]} $data "
fi
prev_color=$color
fi
}
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() {
typeset user_color
((UID)) && user_color=$prompt_colors[user] || user_color=$prompt_colors[root]
typeset c=user
(( UID )) || c=root
precmd.prompt.add %n $user_color
precmd.prompt.add %n $prompt_colors[$c]
}
precmd.prompt.ssh() {
@ -105,27 +99,35 @@ precmd.prompt.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.add "$prompt_symbols[git] $prompt_symbols[ellipsis]" $prompt_colors[git_branch]
}
precmd.prompt.git() {
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 branch_status git_status_string IFS=
while read line; do
if [[ $line[1,2] == '##' ]]; then
case $line[1,2] in
('##')
branch_status=${line[4,-1]%%...*}
[[ $line =~ behind ]] && branch_status+=?
[[ $line =~ ahead ]] && branch_status+=!
precmd.prompt.add "$prompt_symbols[git] $branch_status" $prompt_colors[git_branch]
fi
[[ $line[1,2] == '??' ]] && (( count[git_untracked]++ ))
[[ $line[1,2] =~ .[MD] ]] && (( count[git_unstaged]++ ))
[[ $line[1,2] =~ [MDARC]. ]] && (( count[git_staged]++ ))
[[ $line[1,2] =~ [ADU]{2} ]] && (( count[git_unmerged]++ ))
;;
(?[MD]) (( count[git_unstaged]++ )) ;|
([MDARC]?) (( count[git_staged]++ )) ;|
('??') (( count[git_untracked]++ )) ;|
([ADU][ADU]) (( count[git_unmerged]++ ))
esac
done <<< $raw_status
for i in git_unstaged git_staged git_untracked git_unmerged; do
@ -133,13 +135,8 @@ precmd.prompt.git() {
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.clear
typeset -g prompt_string= prev_color=
precmd.prompt.user
precmd.prompt.ssh
precmd.prompt.host
@ -148,15 +145,16 @@ precmd.prompt() {
}
precmd.git_update() {
umask 077
precmd.prompt
precmd.prompt.git
precmd.prompt.bang
> $prompt_state_file <<< $PROMPT
[[ ! -p $prompt_fifo ]] && mkfifo -m 0600 $prompt_fifo
echo -n $prompt_string > $prompt_fifo &!
kill -s USR1 $$
}
precmd() {
PROMPT=
if precmd.is_git_repo; then
precmd.prompt
precmd.prompt.pre_git
@ -166,15 +164,16 @@ precmd() {
precmd.prompt
precmd.prompt.bang
fi
PROMPT=$prompt_string
}
TRAPUSR1() {
PROMPT=$(<$prompt_state_file)
PROMPT=$(<$prompt_fifo)
zle && zle reset-prompt
}
TRAPEXIT() {
[[ -f $prompt_state_file ]] && rm $prompt_state_file
[[ -p $prompt_fifo ]] && rm $prompt_fifo
}
function zle-line-init zle-keymap-select {