1
0
Fork 0

zsh: a proper powerline implementation, readable and extensible

This commit is contained in:
Von Random 2022-08-20 19:07:49 +03:00
parent 5d5127af49
commit 3e8ac63ace
2 changed files with 140 additions and 94 deletions

View file

@ -9,14 +9,9 @@ confdir=$(dirname $0)
conflist=( conflist=(
settings.zsh settings.zsh
functions.zsh functions.zsh
prompt-powerline-native.zsh
) )
if [[ -x $(whence powerline-go) ]]; then
conflist+=(prompt-powerline-go.zsh)
else
conflist+=(prompt-plain.zsh)
fi
for config in $conflist; do for config in $conflist; do
[[ -r $confdir/$config ]] && . $confdir/$config [[ -r $confdir/$config ]] && . $confdir/$config
done done

View file

@ -1,67 +1,135 @@
# Abandon all hope
#
# This is an implementation of powerline with git support.
# The main feature is it being asynchronous: git status is updated in parallel.
# Despite all the hours spent I have on this I am not going to use this atrocity.
# But it's sorta funny to keep this around.
#
# I see now why common powerline prompts are usually not implemented in shell.
prompt_fmtn='[ %%{\e[2;3m%%}%s%%{\e[0m%%} ] ' 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_state_file=/tmp/zsh_gitstatus_$$.tmp
precmd.home() { PROMPT=
[[ $PWD =~ ^$HOME ]] && printf '~' typeset -A prompt_symbols=(
} sep_a $'\ue0b0'
sep_b $'\ue0b1'
precmd.cwd() { ellipsis $'\u2026'
typeset shifted symbol=$' \ue0b1 ' cwd=${PWD#$HOME} ro $'\u2717'
[[ -z $cwd ]] && return ssh $'\u2191'
typeset -a cwd_array=(${(ps:/:)cwd}) git $'\ue0a0'
cwd= git_unstaged '±'
while ((${#cwd_array} > 3)); do git_staged $'\u2713'
shift cwd_array git_untracked '!'
typeset shifted=1 git_unmerged '*'
done bang $'\n\U01f525'
((shifted)) && cwd_array=(... $cwd_array)
while ((${#cwd_array})); do
cwd+="$cwd_array[1]$symbol"
shift cwd_array
done
printf ${cwd%$symbol}
}
precmd.vars() {
typeset user_color=blue
((UID)) || user_color=red
typeset -ga prompt_strings=(
%n::$user_color
%m::246
"$(precmd.home)::242"
"$(precmd.cwd)::245"
) )
typeset -A prompt_colors=(
fg 253
user 53
ssh 90
root 52
host 240
home 237
/ 237
dirs 234
ro 124
git_branch 237
git_unstaged 130
git_staged 25
git_untracked 88
git_unmerged 30
)
precmd.prompt.clear() {
PROMPT=
} }
precmd.prompt() { precmd.prompt.add() {
typeset prev_color symbol=$'\ue0b0' last=$'\n\U01f525 ' fg_color=black n=1 limit=${#prompt_strings} typeset string
typeset ps1 line val color typeset data=$1 color=$2
ps1= if [[ $color == same ]]; then
for ((i=1; i<=limit; i++)); do PROMPT+="$prompt_symbols[sep_b] $data "
line=$prompt_strings[$i] else
val=${line%%::*} if ((${#PROMPT})); then
color=${line##*::} PROMPT+="%F{$prev_color}%K{$color}$prompt_symbols[sep_a]%F{$prompt_colors[fg]} $data "
if [[ -n $val ]]; then else
[[ -z $prev_color ]] && prev_color=$color PROMPT="%K{$color}%F{$prompt_colors[fg]} $data "
ps1+="%F{$prev_color}%K{$color}$symbol%F{$fg_color} $val " fi
prev_color=$color prev_color=$color
fi fi
}
precmd.prompt.bang() {
PROMPT+="%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]
precmd.prompt.add %n $user_color
}
precmd.prompt.ssh() {
[[ -n $SSH_CONNECTION ]] && precmd.prompt.add $prompt_symbols[ssh] $prompt_colors[ssh]
}
precmd.prompt.host() {
precmd.prompt.add %m $prompt_colors[host]
}
precmd.prompt.cwd() {
typeset cwd limit=${1:-3}
if [[ $PWD =~ ^$HOME ]]; then
precmd.prompt.add \~ $prompt_colors[home]
cwd=${PWD#$HOME}
else
precmd.prompt.add / $prompt_colors[/]
cwd=${PWD:1}
fi
[[ -z $cwd ]] && return
typeset -a cwd_array=(${(ps:/:)cwd})
if ((${#cwd_array} > limit)); then
precmd.prompt.add $prompt_symbols[ellipsis] $prompt_colors[dirs]
while ((${#cwd_array} > limit)); do
shift cwd_array
done
else
precmd.prompt.add $cwd_array[1] $prompt_colors[dirs]
shift cwd_array
fi
while ((${#cwd_array})); do
precmd.prompt.add $cwd_array[1] same
shift cwd_array
done
}
precmd.prompt.ro() {
[[ -w . ]] || precmd.prompt.add $prompt_symbols[ro] $prompt_colors[ro]
}
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
typeset -A count
typeset branch_status git_status_string IFS=
while read line; do
if [[ $line[1,2] == '##' ]]; then
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]++ ))
done <<< $raw_status
for i in git_unstaged git_staged git_untracked git_unmerged; do
(( count[$i] )) && precmd.prompt.add "$count[$i]$prompt_symbols[$i]" $prompt_colors[$i]
done done
ps1+="%F{$prev_color}%k$symbol%f$last"
echo -n $ps1
} }
precmd.is_git_repo() { precmd.is_git_repo() {
@ -69,50 +137,33 @@ precmd.is_git_repo() {
[[ ! -e $git_dir/nozsh ]] [[ ! -e $git_dir/nozsh ]]
} }
precmd.git() { precmd.prompt() {
typeset raw_status precmd.prompt.clear
raw_status=$(flock -n $prompt_state_file git --no-optional-locks status --porcelain -bu 2>/dev/null) || return 0 precmd.prompt.user
precmd.prompt.ssh
typeset symbol=$'\ue0a0' branch_status git_status_string IFS= precmd.prompt.host
typeset staged_count unstaged_count untracked_count unmerged_coun precmd.prompt.cwd 2
while read line; do precmd.prompt.ro
if [[ $line[1,2] == '##' ]]; then
branch_status=${line[4,-1]%%...*}
[[ $line =~ behind ]] && branch_status+=?
[[ $line =~ ahead ]] && branch_status+=!
prompt_strings+=("$symbol $branch_status::249")
fi
[[ $line[1,2] == '??' ]] && (( untracked_count++ ))
[[ $line[1,2] =~ .[MD] ]] && (( unstaged_count++ ))
[[ $line[1,2] =~ [MDARC]. ]] && (( staged_count++ ))
[[ $line[1,2] =~ [ADU]{2} ]] && (( unmerged_count++ ))
done <<< $raw_status
if ! ((untracked_count || unstaged_count || staged_count || unmerged_count)); then
prompt_strings+=(ok::10)
fi
(( unstaged_count )) && prompt_strings+=( "~$unstaged_count::11" )
(( staged_count )) && prompt_strings+=( "+$staged_count::12" )
(( untracked_count )) && prompt_strings+=( "-$untracked_count::9" )
(( unmerged_count )) && prompt_strings+=( "*$unmerged_count::14" )
} }
precmd.git_update() { precmd.git_update() {
precmd.vars umask 077
precmd.git precmd.prompt
precmd.prompt > $prompt_state_file precmd.prompt.git
precmd.prompt.bang
> $prompt_state_file <<< $PROMPT
kill -s USR1 $$ kill -s USR1 $$
} }
precmd() { precmd() {
if precmd.is_git_repo; then if precmd.is_git_repo; then
precmd.vars precmd.prompt
prompt_strings+=($'\ue0a0 ...'::249) precmd.prompt.pre_git
PROMPT=$(precmd.prompt) precmd.prompt.bang
precmd.git_update &! precmd.git_update &!
else else
precmd.vars precmd.prompt
PROMPT=$(precmd.prompt) precmd.prompt.bang
fi fi
} }