2022-08-20 15:33:45 +03:00
|
|
|
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'
|
|
|
|
|
2022-08-21 04:04:14 +03:00
|
|
|
prompt_fifo=~/.zsh_gitstatus_$$
|
2022-08-20 19:07:49 +03:00
|
|
|
typeset -A prompt_symbols=(
|
|
|
|
sep_a $'\ue0b0'
|
|
|
|
sep_b $'\ue0b1'
|
|
|
|
ellipsis $'\u2026'
|
|
|
|
ro $'\u2717'
|
|
|
|
ssh $'\u2191'
|
|
|
|
git $'\ue0a0'
|
|
|
|
git_unstaged '±'
|
|
|
|
git_staged $'\u2713'
|
|
|
|
git_untracked '!'
|
|
|
|
git_unmerged '*'
|
|
|
|
bang $'\n\U01f525'
|
|
|
|
)
|
2022-09-03 19:30:19 +03:00
|
|
|
# gruvbox
|
|
|
|
#typeset -A prompt_colors=(
|
|
|
|
# fg '#ebdbb2'
|
|
|
|
# user '#458588'
|
|
|
|
# root '#cc241d'
|
|
|
|
# ssh '#d65d0e'
|
|
|
|
# host '#504945'
|
|
|
|
# cwd '#3c3836'
|
|
|
|
# ro '#d65d0e'
|
|
|
|
# git_branch '#504945'
|
|
|
|
# git_unstaged '#d65d0e'
|
|
|
|
# git_staged '#458588'
|
|
|
|
# git_untracked '#cc241d'
|
|
|
|
# git_unmerged '#689d6a'
|
|
|
|
#)
|
|
|
|
# 256 color
|
2022-08-20 19:07:49 +03:00
|
|
|
typeset -A prompt_colors=(
|
2022-09-03 19:30:19 +03:00
|
|
|
fg 253
|
|
|
|
user 24
|
|
|
|
root 124
|
|
|
|
ssh 66
|
|
|
|
host 238
|
|
|
|
cwd 236
|
|
|
|
ro 88
|
|
|
|
git_branch 238
|
|
|
|
git_unstaged 166
|
|
|
|
git_untracked 124
|
|
|
|
git_unmerged 171
|
|
|
|
git_staged 54
|
2022-08-20 19:07:49 +03:00
|
|
|
)
|
|
|
|
|
2022-08-21 04:17:56 +03:00
|
|
|
precmd.prompt.init() {
|
|
|
|
typeset -g prompt_string= prev_color=
|
|
|
|
}
|
|
|
|
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.add() {
|
2022-08-21 04:04:14 +03:00
|
|
|
(( $# < 2 )) && return 1
|
2022-08-20 19:07:49 +03:00
|
|
|
typeset data=$1 color=$2
|
2022-08-21 15:26:30 +03:00
|
|
|
if [[ -z $prompt_string ]]; then
|
|
|
|
prompt_string+="%K{$color}%F{$prompt_colors[fg]} $data "
|
2022-08-20 19:07:49 +03:00
|
|
|
else
|
2022-08-21 15:26:30 +03:00
|
|
|
prompt_string+="%F{$prev_color}%K{$color}$prompt_symbols[sep_a]%F{$prompt_colors[fg]} $data "
|
2022-08-20 19:07:49 +03:00
|
|
|
fi
|
2022-08-21 15:26:30 +03:00
|
|
|
prev_color=$color
|
|
|
|
}
|
|
|
|
|
|
|
|
precmd.prompt.add_same() {
|
|
|
|
prompt_string+="$prompt_symbols[sep_b] $* "
|
2022-08-20 19:07:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
precmd.prompt.bang() {
|
2022-08-21 04:04:14 +03:00
|
|
|
prompt_string+="%F{$prev_color}%k$prompt_symbols[sep_a]%f$prompt_symbols[bang] "
|
2022-08-20 19:07:49 +03:00
|
|
|
}
|
|
|
|
|
2022-08-21 04:17:56 +03:00
|
|
|
precmd.prompt.apply() {
|
|
|
|
PROMPT=$prompt_string
|
|
|
|
unset prompt_string
|
|
|
|
}
|
|
|
|
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.user() {
|
2022-08-21 04:04:14 +03:00
|
|
|
typeset c=user
|
|
|
|
(( UID )) || c=root
|
2022-08-20 19:07:49 +03:00
|
|
|
|
2022-08-21 04:04:14 +03:00
|
|
|
precmd.prompt.add %n $prompt_colors[$c]
|
2022-08-20 19:07:49 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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]
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.cwd() {
|
2022-08-21 15:26:30 +03:00
|
|
|
precmd.prompt.add %~ $prompt_colors[cwd]
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.ro() {
|
|
|
|
[[ -w . ]] || precmd.prompt.add $prompt_symbols[ro] $prompt_colors[ro]
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
2022-08-21 04:04:14 +03:00
|
|
|
precmd.is_git_repo() {
|
2022-08-21 04:43:39 +03:00
|
|
|
typeset prompt_git_dir
|
2022-08-21 04:04:14 +03:00
|
|
|
prompt_git_dir=$(git rev-parse --git-dir 2>/dev/null) || return 1
|
|
|
|
[[ ! -e $prompt_git_dir/nozsh ]]
|
|
|
|
}
|
|
|
|
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.pre_git() {
|
|
|
|
precmd.prompt.add "$prompt_symbols[git] $prompt_symbols[ellipsis]" $prompt_colors[git_branch]
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.git() {
|
2022-08-20 15:33:45 +03:00
|
|
|
typeset raw_status
|
2022-08-21 04:43:39 +03:00
|
|
|
raw_status=$(git status --porcelain -bu 2>/dev/null) || return 0
|
2022-08-20 15:33:45 +03:00
|
|
|
|
2022-08-20 19:07:49 +03:00
|
|
|
typeset -A count
|
|
|
|
typeset branch_status git_status_string IFS=
|
2022-08-20 15:33:45 +03:00
|
|
|
while read line; do
|
2022-08-21 04:04:14 +03:00
|
|
|
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]
|
|
|
|
;;
|
|
|
|
(?[MD]) (( count[git_unstaged]++ )) ;|
|
|
|
|
([MDARC]?) (( count[git_staged]++ )) ;|
|
|
|
|
('??') (( count[git_untracked]++ )) ;|
|
|
|
|
([ADU][ADU]) (( count[git_unmerged]++ ))
|
|
|
|
esac
|
2022-08-20 15:33:45 +03:00
|
|
|
done <<< $raw_status
|
|
|
|
|
2022-08-23 16:13:04 +03:00
|
|
|
for i in git_unstaged git_untracked git_unmerged git_staged; do
|
2022-08-20 19:07:49 +03:00
|
|
|
(( count[$i] )) && precmd.prompt.add "$count[$i]$prompt_symbols[$i]" $prompt_colors[$i]
|
|
|
|
done
|
|
|
|
}
|
|
|
|
|
|
|
|
precmd.prompt() {
|
2022-08-21 04:17:56 +03:00
|
|
|
precmd.prompt.init
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.user
|
|
|
|
precmd.prompt.ssh
|
|
|
|
precmd.prompt.host
|
2022-08-21 15:26:30 +03:00
|
|
|
precmd.prompt.cwd
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.ro
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
precmd.git_update() {
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt
|
|
|
|
precmd.prompt.git
|
|
|
|
precmd.prompt.bang
|
2022-08-21 04:04:14 +03:00
|
|
|
[[ ! -p $prompt_fifo ]] && mkfifo -m 0600 $prompt_fifo
|
|
|
|
echo -n $prompt_string > $prompt_fifo &!
|
2022-08-20 15:33:45 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-08-20 15:33:45 +03:00
|
|
|
precmd() {
|
2022-08-21 04:34:32 +03:00
|
|
|
precmd.prompt
|
2022-08-20 15:33:45 +03:00
|
|
|
if precmd.is_git_repo; then
|
2022-08-20 19:07:49 +03:00
|
|
|
precmd.prompt.pre_git
|
2022-08-20 15:33:45 +03:00
|
|
|
precmd.git_update &!
|
|
|
|
fi
|
2022-08-21 04:34:32 +03:00
|
|
|
precmd.prompt.bang
|
2022-08-21 04:17:56 +03:00
|
|
|
precmd.prompt.apply
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TRAPUSR1() {
|
2022-08-21 04:34:32 +03:00
|
|
|
precmd.prompt.update
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TRAPEXIT() {
|
2022-08-21 04:04:14 +03:00
|
|
|
[[ -p $prompt_fifo ]] && rm $prompt_fifo
|
2022-08-20 15:33:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|