-
Notifications
You must be signed in to change notification settings - Fork 111
/
Copy pathprompt2
354 lines (285 loc) · 9.82 KB
/
prompt2
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#!/usr/bin/env zsh
# For my own and others sanity
# git:
# %b => current branch
# %a => current action (rebase/merge)
# prompt:
# %F => color dict
# %f => reset color
# %~ => current path
# %* => time
# %n => username
# %m => shortname host
# %(?..) => prompt conditional - %(condition.true.false)
# terminal codes:
# \e7 => save cursor position
# \e[2A => move cursor 2 lines up
# \e[1G => go to position 1 in terminal
# \e8 => restore cursor position
# \e[K => clears everything after the cursor on the current line
# \e[2K => clear everything on the current line
local lsep=""
local rsep=""
local alt_sep=""
# local alt_sep="/"
local alt_rsep=""
local gray2=#343D46
__format_cwd() {
local dir_limit="3"
local truncation="⋯"
local first_char
local part_count=0
local formatted_cwd=""
local tilde="~"
local dir_sep=""
local cwd="${PWD/#$HOME/$tilde}"
# get first char of the path, i.e. tilde or slash
[[ -n ${ZSH_VERSION-} ]] && first_char=$cwd[1,1] || first_char=${cwd::1}
# remove leading tilde
cwd="${cwd#\~}"
while [[ "$cwd" == */* && "$cwd" != "/" ]]; do
# pop off last part of cwd
local part="${cwd##*/}"
cwd="${cwd%/*}"
formatted_cwd="$dir_sep $part $formatted_cwd"
part_count=$((part_count+1))
[[ $part_count -eq $dir_limit ]] && first_char="$truncation" && break
done
printf "%s" "$first_char$formatted_cwd"
}
prompt_preprompt_render() {
setopt localoptions noshwordsplit
# Initialize the preprompt array.
local -a preprompt_parts
# Set the path.
preprompt_parts+="%F{white}%K{blue} $(__format_cwd) %f%k"
# Add Git branch and dirty status info.
typeset -gA prompt_vcs_info
typeset -g prompt_git_status
if [[ $prompt_vcs_info[branch] = '' ]];
then
preprompt_parts+="%F{blue}$lsep%f"
else
preprompt_parts+="%F{blue}%K{8}$lsep%f%k%F{white}%K{8} ${prompt_vcs_info[branch]} %f%k"
if [[ -n $prompt_git_status ]];
then
preprompt_parts+="%F{8}%K{$gray2}$lsep%f%k%F{white}%K{$gray2} ${prompt_git_status} %f%k%F{$gray2}$lsep%f"
else
preprompt_parts+="%F{8}$lsep%f%F{white} %f"
fi
fi
local cleaned_ps1=$PROMPT
local -H MATCH MBEGIN MEND
if [[ $PROMPT = *$prompt_newline* ]]; then
cleaned_ps1=${PROMPT##*${prompt_newline}}
fi
unset MATCH MBEGIN MEND
# Construct the new prompt with a clean preprompt.
local -ah ps1
ps1=(
${(j..)preprompt_parts} # Join parts, space separated.
$prompt_newline # Separate preprompt and prompt.
$cleaned_ps1
)
PROMPT="${(j..)ps1}"
# Expand the prompt for future comparision.
local expanded_prompt
expanded_prompt="${(S%%)PROMPT}"
if [[ $1 == precmd ]]; then
# Initial newline, for spaciousness.
elif [[ $prompt_last_prompt != $expanded_prompt ]]; then
# Redraw the prompt.
prompt_reset_prompt
fi
typeset -g prompt_last_prompt=$expanded_prompt
}
prompt_precmd() {
prompt_async_tasks
prompt_preprompt_render "precmd"
}
prompt_async_vcs_info() {
setopt localoptions noshwordsplit
# Configure `vcs_info` inside an async task. This frees up `vcs_info`
# to be used or configured as the user pleases.
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' use-simple true
# Only export two message variables from `vcs_info`.
zstyle ':vcs_info:*' max-exports 2
# Export branch (%b) and Git toplevel (%R).
zstyle ':vcs_info:git*' formats '%b' '%R'
zstyle ':vcs_info:git*' actionformats '%b|%a' '%R'
vcs_info
local -A info
info[pwd]=$PWD
info[top]=$vcs_info_msg_1_
info[branch]=$vcs_info_msg_0_
print -r - ${(@kvq)info}
}
prompt_async_git_status() {
[[ $(git rev-parse --is-inside-work-tree 2>/dev/null) == true ]] || return 1
local added_symbol="•"
local unmerged_symbol="ⅹ"
local modified_symbol="+"
local clean_symbol=""
local has_untracked_files_symbol="…"
local ahead_symbol="↑"
local behind_symbol="↓"
local unmerged_count=0 modified_count=0 has_untracked_files=0 added_count=0 is_clean=""
set -- $(git rev-list --left-right --count @{upstream}...HEAD 2>/dev/null)
local behind_count=$1
local ahead_count=$2
# Added (A), Copied (C), Deleted (D), Modified (M), Renamed (R), changed (T), Unmerged (U), Unknown (X), Broken (B)
while read line; do
case "$line" in
M*) modified_count=$(( $modified_count + 1 )) ;;
U*) unmerged_count=$(( $unmerged_count + 1 )) ;;
esac
done < <(git diff --name-status)
while read line; do
case "$line" in
*) added_count=$(( $added_count + 1 )) ;;
esac
done < <(git diff --name-status --cached)
if [ -n "$(git ls-files --others --exclude-standard)" ]; then
has_untracked_files=1
fi
if [ $(( unmerged_count + modified_count + has_untracked_files + added_count )) -eq 0 ]; then
is_clean=1
fi
local leading_whitespace=""
[[ $ahead_count -gt 0 ]] && { printf "%s" "$leading_whitespace$ahead_symbol$ahead_count"; leading_whitespace=" "; }
[[ $behind_count -gt 0 ]] && { printf "%s" "$leading_whitespace$behind_symbol$behind_count"; leading_whitespace=" "; }
[[ $modified_count -gt 0 ]] && { printf "%s" "$leading_whitespace$modified_symbol$modified_count"; leading_whitespace=" "; }
[[ $unmerged_count -gt 0 ]] && { printf "%s" "$leading_whitespace$unmerged_symbol$unmerged_count"; leading_whitespace=" "; }
[[ $added_count -gt 0 ]] && { printf "%s" "$leading_whitespace$added_symbol$added_count"; leading_whitespace=" "; }
[[ $has_untracked_files -gt 0 ]] && { printf "%s" "$leading_whitespace$has_untracked_files_symbol"; leading_whitespace=" "; }
[[ $is_clean -gt 0 ]] && { printf "%s" "$leading_whitespace$clean_symbol"; leading_whitespace=" "; }
}
prompt_async_tasks() {
setopt localoptions noshwordsplit
# Initialize the async worker.
((!${prompt_async_init:-0})) && {
async_start_worker "mike_prompt" -u -n
async_register_callback "mike_prompt" prompt_async_callback
typeset -g prompt_async_init=1
}
# Update the current working directory of the async worker.
async_worker_eval "mike_prompt" builtin cd -q $PWD
typeset -gA prompt_vcs_info
typeset -g prompt_git_status
local -H MATCH MBEGIN MEND
if [[ $PWD != ${prompt_vcs_info[pwd]}* ]]; then
# Stop any running async jobs.
async_flush_jobs "mike_prompt"
# Reset Git preprompt variables, switching working tree.
unset prompt_git_status
# prompt_git_status=
prompt_vcs_info[branch]=
prompt_vcs_info[top]=
fi
unset MATCH MBEGIN MEND
async_job "mike_prompt" prompt_async_vcs_info
# Only perform tasks inside a Git working tree.
[[ -n $prompt_vcs_info[top] ]] || return
prompt_async_refresh
}
prompt_async_refresh() {
async_job "mike_prompt" prompt_async_git_status
}
prompt_async_callback() {
setopt localoptions noshwordsplit
local job=$1 code=$2 output=$3 exec_time=$4 next_pending=$6
local do_render=0
case $job in
\[async])
# Code is 1 for corrupted worker output and 2 for dead worker.
if [[ $code -eq 2 ]]; then
# Our worker died unexpectedly.
typeset -g prompt_async_init=0
fi
;;
prompt_async_vcs_info)
local -A info
typeset -gA prompt_vcs_info
# Parse output (z) and unquote as array (Q@).
info=("${(Q@)${(z)output}}")
local -H MATCH MBEGIN MEND
if [[ $info[pwd] != $PWD ]]; then
# The path has changed since the check started, abort.
return
fi
# Check if Git top-level has changed.
if [[ $info[top] = $prompt_vcs_info[top] ]]; then
# If the stored pwd is part of $PWD, $PWD is shorter and likelier
# to be top-level, so we update pwd.
if [[ $prompt_vcs_info[pwd] = ${PWD}* ]]; then
prompt_vcs_info[pwd]=$PWD
fi
else
# Store $PWD to detect if we (maybe) left the Git path.
prompt_vcs_info[pwd]=$PWD
fi
unset MATCH MBEGIN MEND
# The update has a Git top-level set, which means we just entered a new
# Git directory. Run the async refresh tasks.
[[ -n $info[top] ]] && [[ -z $prompt_vcs_info[top] ]] && prompt_async_refresh
# Always update branch and top-level.
prompt_vcs_info[branch]=$info[branch]
prompt_vcs_info[top]=$info[top]
do_render=1
;;
prompt_async_git_status)
typeset -g prompt_git_status
local -A status_info
if [[ -n $output ]];
then
status_info=$output
fi
if [[ $status_info != $prompt_git_status ]];
then
prompt_git_status=$status_info
do_render=1
fi
;;
esac
if (( next_pending )); then
(( do_render )) && typeset -g prompt_async_render_requested=1
return
fi
[[ ${prompt_async_render_requested:-$do_render} = 1 ]] && prompt_preprompt_render
unset prompt_async_render_requested
}
prompt_reset_prompt() {
if [[ $CONTEXT == cont ]]; then
# When the context is "cont", PS2 is active and calling
# reset-prompt will have no effect on PS1, but it will
# reset the execution context (%_) of PS2 which we don't
# want. Unfortunately, we can't save the output of "%_"
# either because it is only ever rendered as part of the
# prompt, expanding in-place won't work.
return
fi
zle && zle .reset-prompt
}
prompt_setup() {
# Prevent percentage showing up if output doesn't end with a newline.
export PROMPT_EOL_MARK=''
prompt_opts=(subst percent)
# Borrowed from `promptinit`. Sets the prompt options in case Pure was not
# initialized via `promptinit`.
setopt noprompt{bang,cr,percent,subst} "prompt${^prompt_opts[@]}"
if [[ -z $prompt_newline ]]; then
# This variable needs to be set, usually set by promptinit.
typeset -g prompt_newline=$'\n%{\r%}'
fi
zmodload zsh/datetime
zmodload zsh/zle
zmodload zsh/parameter
zmodload zsh/zutil
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
autoload -Uz async && async
add-zsh-hook precmd prompt_precmd
PROMPT='$ '
}
prompt_setup "$@"