-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzunder-prompt.plugin.zsh
115 lines (93 loc) · 4.21 KB
/
zunder-prompt.plugin.zsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# Check if gitstatus is available.
if ! typeset -f gitstatus_query > /dev/null; then
echo "The gitstatus plugin is not installed and is required" \
"to use zunder-prompt."
return 0
fi
function gitstatus_prompt_update() {
emulate -L zsh
typeset -g GITSTATUS_PROMPT=''
typeset -gi GITSTATUS_PROMPT_LEN=0
# Call gitstatus_query synchronously.
gitstatus_query 'MY' || return 1 # error
[[ $VCS_STATUS_RESULT == 'ok-sync' ]] || return 0 # not a git repo
local clean='%5F' # magenta foreground
local modified='%3F' # yellow foreground
local untracked='%4F' # blue foreground
local conflicted='%1F' # red foreground
[[ "$TERM" != "linux" ]] && local git_icon=" " # set git_icon if not in tty
local p="on %B${clean}${git_icon}"
local where # branch name, tag or commit
if [[ -n $VCS_STATUS_LOCAL_BRANCH ]]; then
where=$VCS_STATUS_LOCAL_BRANCH
elif [[ -n $VCS_STATUS_TAG ]]; then
p+='#'
where=$VCS_STATUS_TAG
else
p+='@'
where=${VCS_STATUS_COMMIT[1,8]}
fi
(( $#where > 32 )) && where[13,-13]="…" # truncate long branch names and tags
p+="${where//\%/%%}%b" # escape %
# ⇣42 if behind the remote.
(( VCS_STATUS_COMMITS_BEHIND )) && p+=" ${clean}⇣${VCS_STATUS_COMMITS_BEHIND}"
# ⇡42 if ahead of the remote; no leading space if also behind the remote: ⇣42⇡42.
(( VCS_STATUS_COMMITS_AHEAD && !VCS_STATUS_COMMITS_BEHIND )) && p+=" "
(( VCS_STATUS_COMMITS_AHEAD )) && p+="${clean}⇡${VCS_STATUS_COMMITS_AHEAD}"
# ⇠42 if behind the push remote.
(( VCS_STATUS_PUSH_COMMITS_BEHIND )) && p+=" ${clean}⇠${VCS_STATUS_PUSH_COMMITS_BEHIND}"
(( VCS_STATUS_PUSH_COMMITS_AHEAD && !VCS_STATUS_PUSH_COMMITS_BEHIND )) && p+=" "
# ⇢42 if ahead of the push remote; no leading space if also behind: ⇠42⇢42.
(( VCS_STATUS_PUSH_COMMITS_AHEAD )) && p+="${clean}⇢${VCS_STATUS_PUSH_COMMITS_AHEAD}"
# *42 if have stashes.
(( VCS_STATUS_STASHES )) && p+=" ${clean}*${VCS_STATUS_STASHES}"
# 'merge' if the repo is in an unusual state.
[[ -n $VCS_STATUS_ACTION ]] && p+=" ${conflicted}${VCS_STATUS_ACTION}"
# ~42 if have merge conflicts.
(( VCS_STATUS_NUM_CONFLICTED )) && p+=" ${conflicted}~${VCS_STATUS_NUM_CONFLICTED}"
# +42 if have staged changes.
(( VCS_STATUS_NUM_STAGED )) && p+=" ${modified}+${VCS_STATUS_NUM_STAGED}"
# !42 if have unstaged changes.
(( VCS_STATUS_NUM_UNSTAGED )) && p+=" ${modified}!${VCS_STATUS_NUM_UNSTAGED}"
# ?42 if have untracked files. It's really a question mark, your font isn't broken.
(( VCS_STATUS_NUM_UNTRACKED )) && p+=" ${untracked}?${VCS_STATUS_NUM_UNTRACKED}"
GITSTATUS_PROMPT="${p}%f"
# The length of GITSTATUS_PROMPT after removing %f, %b, %F and %B.
GITSTATUS_PROMPT_LEN="${(m)#${${${GITSTATUS_PROMPT//\%\%/x}//\%(f|<->F)}//\%[Bb]}}"
}
# Start gitstatusd instance with name "MY". The same name is passed to
# gitstatus_query in gitstatus_prompt_update. The flags with -1 as values
# enable staged, unstaged, conflicted and untracked counters.
gitstatus_stop 'MY' && gitstatus_start -s -1 -u -1 -c -1 -d -1 'MY'
function check_first_prompt() {
if [[ $FIRST_PROMPT == false ]]; then
printf "\n"
else
FIRST_PROMPT=false
fi
}
autoload -Uz add-zsh-hook
# On every prompt, fetch git status and set GITSTATUS_PROMPT.
add-zsh-hook precmd gitstatus_prompt_update
# Adds a new line if it's not the first prompt.
add-zsh-hook precmd check_first_prompt
# Enable/disable the right prompt options.
setopt no_prompt_bang prompt_percent prompt_subst
# Default prompt char
if [[ -z "$ZUNDER_PROMPT_CHAR" ]]; then
ZUNDER_PROMPT_CHAR='%B❯%b'
[[ "$TERM" == "linux" ]] && ZUNDER_PROMPT_CHAR='>' # switch to > in tty mode
fi
# Default prompt char color (uses terminal foreground color)
ZUNDER_PROMPT_CHAR_COLOR="fg"
# Prompt used in multiline commands
PROMPT2="%8F·%f "
# Cyan current working directory. Gets truncated from the left if the whole
# prompt doesn't fit on the line
PROMPT='%B%6F%$((-GITSTATUS_PROMPT_LEN-1))<…<%~%<<%f%b'
# Git status
PROMPT+='${GITSTATUS_PROMPT:+ $GITSTATUS_PROMPT}'
# New line
PROMPT+=$'\n'
# $ZUNDER_PROMPT_CHAR $ZUNDER_PROMPT_CHAR_COLOR/red (ok/error)
PROMPT+='%F{%(?.${ZUNDER_PROMPT_CHAR_COLOR}.red)}${ZUNDER_PROMPT_CHAR}%f '