-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt_adam2_setup
353 lines (278 loc) · 10.5 KB
/
prompt_adam2_setup
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
# adam2 prompt theme
prompt_adam2_help () {
cat <<'EOF'
This prompt is color-scheme-able. You can invoke it thus:
prompt adam2 [ 8bit ] [<color1> [<color2> [<color3>] [<color4>]]
where the colors are for the hyphens, current directory, user@host,
and user input bits respectively. The default colors are cyan, green,
cyan, and white. This theme works best with a dark background.
If you have either UTF-8 or the `nexus' or `vga' console fonts or similar,
you can specify the `8bit' option to use 8-bit replacements for the
7-bit characters.
And you probably thought adam1 was overkill ...
EOF
}
function git_root_for_cwd {
echo $( git rev-parse --show-toplevel 2>/dev/null )
}
function prompt_build_git_branch {
prompt_git_branch=
local branch=$( git symbolic-ref HEAD 2>/dev/null )
if [[ -n "$branch" ]]; then
prompt_git_branch="${branch/*heads\//}"
elif [[ -d "$prompt_git_root/.git" ]]; then
prompt_git_branch='no branch'
fi
}
function prompt_build_git_user {
prompt_git_user=
if [[ -z "$prompt_git_branch" ]]; then
return
fi
prompt_git_user=$( git config user.name 2>/dev/null )
}
function prompt_build_git_changes {
prompt_git_ahead=0
prompt_git_behind=0
if [[ -z "$prompt_git_branch" ]]; then
return
fi
local merge=$( git config branch.$prompt_git_branch.merge 2>/dev/null )
local remote=$( git config branch.$prompt_git_branch.remote 2>/dev/null )
if [[ -n "$merge" ]] && [[ -n "$remote" ]]; then
local ref=${merge/heads/remotes/$remote}
local changes=${$( git rev-list --left-right $ref...HEAD 2>/dev/null|tr '\n' '-' )//[^<>]/}
prompt_git_ahead=${#${changes//</}}
prompt_git_behind=${#${changes//>/}}
fi
}
function prompt_build_git_operation {
prompt_git_operation=
local root="$prompt_git_root/.git"
if [[ ! -d "$root" ]]; then
return
elif [ -f "$root/rebase-merge/interactive" ]; then
prompt_git_operation='rebase -i'
elif [ -d "$root/rebase-merge" ]; then
prompt_git_operation='rebase -m'
elif [ -d "$root/rebase-apply" ]; then
if [ -f "$root/rebase-apply/rebasing" ]; then
prompt_git_operation='rebase'
elif [ -f "root/rebase-apply/applying" ]; then
prompt_git_operation='am'
else
prompt_git_operation='am/rebase'
fi
elif [ -f "$root/MERGE_HEAD" ]; then
prompt_git_operation='merge'
elif [ -f "$root/index.lock" ]; then
prompt_git_operation='locked'
elif [ -f "$root/BISECT_LOG" ]; then
prompt_git_operation='bisect'
fi
}
function prompt_build_git {
prompt_git=
prompt_git_width=0
prompt_git_root=$( git_root_for_cwd )
prompt_build_git_branch
prompt_build_git_user
prompt_build_git_changes
prompt_build_git_operation
prompt_git_desc=
if [[ -n "$prompt_git_branch" ]]; then
prompt_git_desc="$prompt_git_user | $prompt_git_branch"
if [ $prompt_git_behind -gt 0 ]; then
prompt_git_desc="${prompt_git_desc} ↓${prompt_git_behind}"
fi
if [ $prompt_git_ahead -gt 0 ]; then
prompt_git_desc="${prompt_git_desc} ↑${prompt_git_ahead}"
fi
fi
if [[ -n "$prompt_git_desc" ]] || [[ -n "$prompt_git_operation" ]]; then
prompt_git=" %B%F{$prompt_adam2_color1}[ "
prompt_git_width=$(( prompt_git_width + 3 ))
if [[ -n "$prompt_git_desc" ]]; then
prompt_git="${prompt_git}${prompt_git_desc}"
prompt_git_width=$(( prompt_git_width + ${#prompt_git_desc} ))
fi
if [[ -n "$prompt_git_desc" ]] && [[ -n "$prompt_git_operation" ]]; then
prompt_git="${prompt_git} "
prompt_git_width=$(( prompt_git_width + 1 ))
fi
if [[ -n "$prompt_git_operation" ]]; then
prompt_git="${prompt_git}%F{magenta}${prompt_git_operation}%f"
prompt_git_width=$(( prompt_git_width + ${#prompt_git_operation} ))
fi
prompt_git="${prompt_git} ]"
prompt_git_width=$(( prompt_git_width + 2 ))
fi
}
function prompt_build_ruby {
prompt_ruby=
prompt_ruby_width=0
if [[ ! -f Gemfile ]] || [[ "$PWD" == "$HOME" ]]; then
return
fi
prompt_ruby_path="$(command -v ruby 2>/dev/null)"
if [[ $? != 0 ]]; then
return
fi
if [[ "$prompt_ruby_path" == */.asdf/* ]]; then
if test -e "$ASDF_RUBY_VERSION"; then
prompt_ruby_version="$ASDF_RUBY_VERSION"
else
test -f .tool-versions && asdf_ruby_version="$(cat .tool-versions|grep ruby 2>/dev/null)"
test -z "$asdf_ruby_version" && asdf_ruby_version="$(cat ~/.tool-versions|grep ruby 2>/dev/null)"
test -n "$asdf_ruby_version" && prompt_ruby_version="${asdf_ruby_version#"ruby "}"
fi
elif [[ "$prompt_ruby_path" == */.rbenv/* ]]; then
if test -e "$RBENV_VERSION"; then
prompt_ruby_version="$RBENV_VERSION"
elif test -f .ruby-version; then
prompt_ruby_version="$(head -n 1 .ruby-version)"
fi
else
prompt_ruby_version="$(ruby -e 'print RUBY_VERSION')"
fi
if [ -z "$prompt_ruby_version" ]; then
return
fi
[[ "$prompt_ruby_version" == ruby* ]] || prompt_ruby_version="ruby-${prompt_ruby_version}"
prompt_ruby="%b%F{blue}${prompt_ruby_version}%f%B%F{black}|%b%f"
prompt_ruby_width=$(( ${#prompt_ruby_version} + 6 ))
}
function prompt_build_node {
prompt_node=
prompt_node_width=0
if [[ ! -f package.json ]] || [[ "$PWD" == "$HOME" ]]; then
return
fi
prompt_node_path="$(command -v node 2>/dev/null)"
if [[ $? != 0 ]]; then
return
fi
if [[ "$prompt_node_path" == */.asdf/* ]]; then
if test -e "$ASDF_NODE_VERSION"; then
prompt_node_version="$ASDF_NODE_VERSION"
else
test -f .tool-versions && asdf_nodejs_version="$(cat .tool-versions|grep nodejs 2>/dev/null)"
test -z "$asdf_nodejs_version" && asdf_nodejs_version="$(cat ~/.tool-versions|grep node 2>/dev/null)"
test -n "$asdf_nodejs_version" && prompt_node_version="${asdf_nodejs_version#"nodejs "}"
fi
elif [[ "$prompt_node_path" == */.nodenv/* ]]; then
if test -e "$NODENV_VERSION"; then
prompt_node_version="$NODENV_VERSION"
elif test -f .node-version; then
prompt_node_version="$(head -n 1 .node-version)"
fi
else
prompt_node_version="$(node -e 'console.log(process.version)')"
prompt_node_version="${prompt_node_version#v}"
fi
if [ -z "$prompt_node_version" ]; then
return
fi
prompt_node="%b%F{blue}node-${prompt_node_version}%f%B%F{black}|%b%f"
prompt_node_width=$(( ${#prompt_node_version} + 6 ))
}
function prompt_build_user_host {
prompt_user_host=
prompt_user_host_width=0
if [[ $EUID -eq 0 ]]; then
local notice='YOU ARE ROOT @ '
local host=${(%):-%m}
prompt_user_host="%B%F{red}YOU ARE ROOT @ %f%F{$prompt_adam2_color3}${host}%B%F{black}|%f%b"
prompt_user_host_width=$(( ${#notice} + ${#host} + 1 ))
else
local raw=${(%):-%n@%m}
prompt_user_host="%b%F{$prompt_adam2_color3}${raw}%B%F{black}|%b"
prompt_user_host_width=$(( ${#raw} + 1 ))
fi
}
prompt_adam2_setup () {
# Some can't be local
local prompt_gfx_tlc prompt_gfx_mlc prompt_gfx_blc
if [[ $1 == '8bit' ]]; then
shift
if [[ ${LC_ALL:-${LC_CTYPE:-$LANG}} = *UTF-8* ]]; then
prompt_gfx_tlc=$'\xe2\x94\x8c'
prompt_gfx_mlc=$'\xe2\x94\x9c'
prompt_gfx_blc=$'\xe2\x94\x94'
prompt_gfx_hyphen=$'\xe2\x94\x80'
else
prompt_gfx_tlc=$'\xda'
prompt_gfx_mlc=$'\xc3'
prompt_gfx_blc=$'\xc0'
prompt_gfx_hyphen=$'\xc4'
fi
else
prompt_gfx_tlc='.'
prompt_gfx_mlc='|'
prompt_gfx_blc='\`'
prompt_gfx_hyphen='-'
fi
# Colour scheme
prompt_adam2_color1=${1:-'cyan'} # hyphens & git & time
prompt_adam2_color2=${2:-'green'} # current directory
prompt_adam2_color3=${3:-'cyan'} # user@host
prompt_adam2_color4=${4:-'white'} # user input
local prompt_gfx_bbox
prompt_gfx_tbox="%B%F{$prompt_adam2_color1}${prompt_gfx_tlc}%b%F{$prompt_adam2_color1}${prompt_gfx_hyphen}"
prompt_gfx_bbox="%B%F{$prompt_adam2_color1}${prompt_gfx_blc}${prompt_gfx_hyphen}%b%F{$prompt_adam2_color1}"
# This is a cute hack. Well I like it, anyway.
prompt_gfx_bbox_to_mbox=$'%{\e[A\r'"%}%B%F{$prompt_adam2_color1}${prompt_gfx_mlc}%b%F{$prompt_adam2_color1}${prompt_gfx_hyphen}%{"$'\e[B%}'
prompt_l_paren="%B%F{black}("
prompt_r_paren="%B%F{black})"
prompt_time="%*"
prompt_cwd="%~"
prompt_line_1a="$prompt_gfx_tbox$prompt_l_paren%{\$prompt_user_host%}%B%F{$prompt_adam2_color2}$prompt_cwd%{\$prompt_git%}$prompt_r_paren%b%F{$prompt_adam2_color1}"
prompt_line_1b="$prompt_l_paren%{\$prompt_ruby%}%{\$prompt_node%}%F{$prompt_adam2_color1}%b$prompt_time$prompt_r_paren%b%F{$prompt_adam2_color1}${prompt_gfx_hyphen}"
prompt_line_2="$prompt_gfx_bbox${prompt_gfx_hyphen}%B%F{white}"
prompt_char="%(!.#.>)"
prompt_opts=(cr subst percent)
add-zsh-hook precmd prompt_adam2_precmd
}
prompt_adam2_precmd() {
prompt_build_git
prompt_build_ruby
prompt_build_node
prompt_build_user_host
setopt noxtrace localoptions extendedglob
local prompt_line_1
prompt_adam2_choose_prompt
PS1="$prompt_line_1$prompt_newline$prompt_line_2%B%F{white}$prompt_char %b%f%k"
PS2="$prompt_line_2$prompt_gfx_bbox_to_mbox%B%F{white}%_> %b%f%k"
PS3="$prompt_line_2$prompt_gfx_bbox_to_mbox%B%F{white}?# %b%f%k"
zle_highlight[(r)default:*]="default:fg=$prompt_adam2_color4,bold"
}
prompt_adam2_choose_prompt () {
local prompt_line_1a_width=${#${(S%%)prompt_line_1a//(\%([KF1]|)\{*\}|\%[Bbkf])}}
local prompt_line_1b_width=${#${(S%%)prompt_line_1b//(\%([KF1]|)\{*\}|\%[Bbkf])}}
local prompt_padding_size=$(( COLUMNS
- prompt_line_1a_width
- prompt_line_1b_width
- prompt_git_width
- prompt_ruby_width
- prompt_node_width
- prompt_user_host_width ))
# Try to fit in long path and user@host.
if (( prompt_padding_size > 0 )); then
local prompt_padding
eval "prompt_padding=\${(l:${prompt_padding_size}::${prompt_gfx_hyphen}:)_empty_zz}"
prompt_line_1="$prompt_line_1a$prompt_padding$prompt_line_1b"
return
fi
prompt_padding_size=$(( COLUMNS - prompt_line_1a_width ))
# Didn't fit; try to fit in just long path.
if (( prompt_padding_size > 0 )); then
local prompt_padding
eval "prompt_padding=\${(l:${prompt_padding_size}::${prompt_gfx_hyphen}:)_empty_zz}"
prompt_line_1="$prompt_line_1a$prompt_padding"
return
fi
# Still didn't fit; truncate
local prompt_pwd_size=$(( COLUMNS - 5 ))
prompt_line_1="$prompt_gfx_tbox$prompt_l_paren%B%F{$prompt_adam2_color2}%$prompt_pwd_size<...<%~%<<$prompt_r_paren%b%F{$prompt_adam2_color1}$prompt_gfx_hyphen"
}
prompt_adam2_setup "$@"