1
0
Fork 0
vdotfiles/cli/.config/zsh/prompt.zsh

161 lines
4 KiB
Bash
Raw Normal View History

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'
2023-01-04 20:54:18 +02:00
prompt_wt="$USERNAME@$HOST"
prompt_fifo=~/.zsh_gitstatus_$$
prompt_blimit=12
typeset -A prompt_symbols=(
2023-06-19 18:34:20 +03:00
sep_a $'\ue0b0'
ellipsis $'\u2026'
git $'\ue0a0'
2022-12-21 17:59:58 +02:00
git_unstaged '~'
git_staged $'\u2713'
git_untracked '!'
git_unmerged '*'
2023-08-03 19:51:22 +03:00
bang $'\u266a'
)
typeset -A prompt_colors=(
2023-08-03 19:51:22 +03:00
# fg '15'
2023-01-26 02:22:49 +02:00
root '1'
2023-08-03 19:51:22 +03:00
ssh '15'
2023-01-24 20:35:42 +02:00
cwd '4'
2023-08-03 19:51:22 +03:00
git_branch '241'
2023-01-24 20:35:42 +02:00
git_unstaged '3'
git_staged '6'
git_untracked '1'
git_unmerged '5'
2023-08-03 19:52:44 +03:00
bang '10'
)
precmd.is_git_repo() {
typeset prompt_git_dir
prompt_git_dir=$(git rev-parse --git-dir 2>/dev/null) || return 1
[[ ! -e $prompt_git_dir/nozsh ]]
}
2023-08-03 19:51:22 +03:00
precmd.prompt.add() {
(( $# < 1 )) && return 1
typeset data=$1 color=$2
[[ -n $prompt_string ]] && prompt_string+=" "
if [[ -n $color ]]; then
prompt_string+="%F{$color}$data%f"
else
prompt_string+="$data"
fi
2022-08-21 04:17:56 +03:00
}
2023-08-03 19:51:22 +03:00
precmd.prompt.add_pl() {
(( $# < 2 )) && return 1
typeset data=$1 color=$2
if [[ -z $prompt_string ]]; then
prompt_string+="%K{$color}%F{$prompt_colors[fg]} $data "
else
prompt_string+="%F{$prev_color}%K{$color}$prompt_symbols[sep_a]%F{$prompt_colors[fg]} $data "
fi
prev_color=$color
}
2022-08-21 04:17:56 +03:00
precmd.prompt.apply() {
PROMPT=$prompt_string
unset prompt_string
}
precmd.prompt.pre_git() {
precmd.prompt.add "$prompt_symbols[git] $prompt_symbols[ellipsis]" $prompt_colors[git_branch]
}
precmd.prompt.git() {
typeset raw_status IFS=
2022-08-21 04:43:39 +03:00
raw_status=$(git status --porcelain -bu 2>/dev/null) || return 0
typeset -A count
while read line; do
case $line[1,2] in
('##')
typeset branch_status=${line[4,-1]%%...*}
((${#branch_status}>prompt_blimit)) && \
branch_status=$branch_status[1,$prompt_blimit]$prompt_symbols[ellipsis]
[[ $line =~ behind ]] && branch_status+=?
[[ $line =~ ahead ]] && branch_status+=!
precmd.prompt.add "$prompt_symbols[git] $branch_status" $prompt_colors[git_branch]
;;
(?[MD]) (( ++count[git_unstaged] )) ;|
([MDARC]?) (( ++count[git_staged] )) ;|
('??') (( ++count[git_untracked] )) ;|
([ADU][ADU]) (( ++count[git_unmerged] ))
esac
done <<< $raw_status
2022-12-21 17:59:58 +02:00
for i in git_untracked git_unmerged git_unstaged git_staged; do
(( count[$i] )) && precmd.prompt.add "$count[$i]$prompt_symbols[$i]" $prompt_colors[$i]
done
}
precmd.prompt() {
typeset -g prompt_string= prev_color=
2023-08-03 19:51:22 +03:00
precmd.prompt.add '['
(( UID )) \
|| precmd.prompt.add '#' $prompt_colors[root]
[[ -n $SSH_CONNECTION ]]\
&& precmd.prompt.add %n@%m $prompt_colors[ssh]
precmd.prompt.add %~ $prompt_colors[cwd]
[[ $1 == pre_git ]]\
&& precmd.prompt.pre_git
[[ $1 == git ]]\
&& precmd.prompt.git
2023-08-03 19:51:22 +03:00
precmd.prompt.add $']\n'
prompt_string+="%F{$prompt_colors[bang]}$prompt_symbols[bang]%f "
}
precmd.git_update() {
precmd.prompt git
[[ ! -p $prompt_fifo ]] && mkfifo -m 0600 $prompt_fifo
echo -n $prompt_string > $prompt_fifo &!
kill -s USR1 $$
}
2022-08-21 04:34:32 +03:00
precmd.prompt.update() {
typeset -g prompt_string=$(<$prompt_fifo)
precmd.prompt.apply
zle && zle reset-prompt
}
2023-01-04 20:54:18 +02:00
precmd.window_title() {
printf '\033]0;%s\007' $prompt_wt
}
precmd() {
2023-01-04 20:54:18 +02:00
precmd.window_title
if precmd.is_git_repo; then
precmd.prompt pre_git
precmd.git_update &!
else
precmd.prompt
fi
2022-08-21 04:17:56 +03:00
precmd.prompt.apply
}
TRAPUSR1() {
2022-08-21 04:34:32 +03:00
precmd.prompt.update
}
TRAPEXIT() {
[[ -p $prompt_fifo ]] && rm $prompt_fifo
}
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