-
Notifications
You must be signed in to change notification settings - Fork 0
/
danielparks-zsh-theme.plugin.zsh
216 lines (175 loc) · 5.37 KB
/
danielparks-zsh-theme.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Convert seconds into human readable time: 165392.3129 => 1d 21h 56m 32s
# Based on https://github.com/sindresorhus/pretty-time-zsh
_danielparks_theme_humanize_interval () {
float total_seconds=$1
integer days=$(( total_seconds / 60 / 60 / 24 ))
integer hours=$(( total_seconds / 60 / 60 % 24 ))
integer minutes=$(( total_seconds / 60 % 60 ))
float seconds=$(( total_seconds % 60 ))
(( days > 0 )) && print -n "${days}d "
(( hours > 0 )) && print -n "${hours}h "
(( minutes > 0 )) && print -n "${minutes}m "
if (( total_seconds > 10 )) ; then
printf "%0.0fs" $seconds
else
printf "%0.1fs" $seconds
fi
}
_danielparks_theme_git_info_fallback () {
local gitstatus icons='' fg_color=green
gitstatus=$(git status --porcelain=1 2>/dev/null) || return 0
if echo $gitstatus | grep --quiet '^.[^ ?!]' ; then
# Unstaged changes
if echo $gitstatus | grep --quiet '^[^ ?!]' ; then
# Staged changes
icons+=' %1{⦿%}'
else
icons+=' %1{○%}'
fi
fg_color=red
elif echo $gitstatus | grep --quiet '^[^ ?!]' ; then
# Staged changes
icons+=' %1{●%}'
fg_color=yellow
fi
if echo $gitstatus | fgrep --quiet '??' ; then
# Untracked changes
fg_color=red
fi
ref=$(git symbolic-ref --short HEAD 2>/dev/null) ||
ref=$(git show-ref --head -s --abbrev HEAD 2>/dev/null | head -n1)
print -n " %F{$fg_color}${ref}${icons}%f"
}
_danielparks_theme_git_info () {
eval $(git-status-vars 2>/dev/null)
if [[ -z $repo_state ]] ; then
# git-status-vars should always output repo_state
_danielparks_theme_git_info_fallback
return 0
fi
if [[ $repo_state == "NotFound" ]] ; then
return 0
fi
local icons='' fg_color=green
if [[ $unstaged_count > 0 ]] ; then
if [[ $staged_count > 0 ]] ; then
icons+=' %1{⦿%}'
else
icons+=' %1{○%}'
fi
fg_color=red
elif [[ $staged_count > 0 ]] ; then
icons+=' %1{●%}'
fg_color=yellow
fi
if [[ $conflicted_count > 0 ]] ; then
icons+=' %1{⚠️%} '
fi
if [[ $untracked_count > 0 ]] ; then
fg_color=red
fi
local ref=$head_ref1_short
if [[ -z $ref ]] ; then
ref=${head_hash:0:8}
fi
print -n " %F{$fg_color}${ref}${icons}%f"
if [[ $repo_state != "Clean" ]] ; then
# Convert "CamelCase" string to "camel case" (space separated).
local words=$(sed -e 's/\([a-z]\)\([A-Z]\)/\1 \2/g' <<<$repo_state)
print -n " %F{red}(${words:l})%f"
fi
fg_color=yellow
if [[ $head_ahead > 0 && $head_behind > 0 ]] ; then
fg_color=red
fi
if [[ $head_ahead > 0 ]] ; then
print -n " %F{${fg_color}}%1{↑%}${head_ahead}%f"
fi
if [[ $head_behind > 0 ]] ; then
print -n " %F{${fg_color}}%1{↓%}${head_behind}%f"
fi
}
_danielparks_theme_virtualenv_info () {
[ $VIRTUAL_ENV ] && print -n " (${VIRTUAL_ENV:t})" || true
}
_danielparks_theme_precmd () {
local last_status=$?
local prompt_char
local startseconds=${_danielparks_theme_preexec_timestamp:-$EPOCHREALTIME}
float elapsed
(( elapsed = EPOCHREALTIME - startseconds ))
_danielparks_theme_preexec_timestamp=
# Output invisible information for terminal title.
if [[ $SSH_CONNECTION ]] ; then
print -Pn '\e]2;%n@%m %~\a'
else
print -Pn '\e]2;%~\a'
fi
# Build up $PROMPT (just printing the prompt won’t work if it doesn’t end with
# a newline — zsh clears the line when it prints $PROMPT).
PROMPT=
if [[ $danielparks_theme != minimal ]] ; then
if [[ $danielparks_theme != compact ]] ; then
if [[ $danielparks_theme != full && $danielparks_theme != ''
&& $danielparks_theme != $_danielparks_theme_last ]] ; then
print -Pn $'\n%B%F{red}Invalid setting for $danielparks_theme ('
print -n $danielparks_theme
print -P $'), expected one of "full", "compact", "minimal", or "".%f%b'
fi
# Blank line before prompt.
PROMPT+=$'\n'
fi
if [[ $last_status == 0 ]] ; then
PROMPT+='%f%k%B%F{green}%1{✔%}%f'
else
PROMPT+="%f%k%B%F{red}=${last_status}%f"
fi
if [[ $SSH_CONNECTION ]] ; then
PROMPT+=' %F{yellow}%n@%m%f' # user@host
fi
if [[ $danielparks_theme != compact ]] ; then
PROMPT+=$(_danielparks_theme_git_info)
fi
PROMPT+=' %F{white}%~%f' # directory
if [[ $danielparks_theme != compact ]] ; then
PROMPT+=' %F{blue}%D{%L:%M:%S %p}%f' # time
PROMPT+=$(_danielparks_theme_virtualenv_info)
if (( elapsed > 0.05 )) ; then
PROMPT+=" %F{yellow}$(_danielparks_theme_humanize_interval $elapsed)%f"
fi
PROMPT+=$'\n'
fi
prompt_char='%1{❯%}'
PROMPT+='%(!.%F{yellow}root.)'
else
# minimal
PROMPT+='%f%k%B%(!.%F{yellow}root.)'
if [[ $last_status == 0 ]] ; then
PROMPT+='%F{green}'
prompt_char='%1{✔%}'
else
PROMPT+='%F{red}'
prompt_char='%1{✘%}'
fi
fi
# 'root' if running as root. As many ❯ as $SHLVL.
PROMPT+="$(repeat $SHLVL print -n "$prompt_char")%f%k%b "
_danielparks_theme_last=$danielparks_theme
}
_danielparks_theme_preexec () {
_danielparks_theme_preexec_timestamp=$EPOCHREALTIME
}
() {
zmodload zsh/datetime
autoload -Uz add-zsh-hook
add-zsh-hook precmd _danielparks_theme_precmd
add-zsh-hook preexec _danielparks_theme_preexec
if [[ -z $IGNORE_GIT_SUMMARY && -z $IGNORE_GIT_STATUS_VARS ]] \
&& ! command -v git-status-vars &>/dev/null ; then
print -Pn '%B%F{red}git-status-vars not installed.%f%b Run' >&2
print -Pn ' `cargo install git-status-vars` or visit' >&2
print -Pn ' https://github.com/danielparks/git-status-vars#installation' >&2
print -Pn ' for instructions. Set IGNORE_GIT_STATUS_VARS=1 to suppress' >&2
print -P ' this message.' >&2
fi
}