-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.sh
369 lines (291 loc) · 8.49 KB
/
prompt.sh
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/bin/sh
_SPS_main() {
local hostname=$(hostname | sed -E 's/\..*//')
_sps_domain_or_localnet_host=$(hostname | sed -E '
/\..*\./{
s/[^.]+\.//
b
}
s/\..*//
')
if [ -f /proc/$$/exe ] && ls -l /proc/$$/exe 2>/dev/null | sed 's/.*-> //' | grep -Eq '(^|/)(busybox|bb|ginit|.?ash|ksh.*)$'; then
local is_ash_or_ksh=1
fi
if [ -z "$SPS_ESCAPE" ] && [ -n "${BASH_VERSION}${is_ash_or_ksh}" ]; then
SPS_ESCAPE=1
fi
if [ -z "$SPS_WINDOW_TITLE" ]; then
SPS_WINDOW_TITLE=1
fi
_SPS_detect_env
_sps_tmp="${TMP:-${TEMP:-${TMPDIR:-/tmp}}}/sh-prompt-simple/$$"
if [ "$_sps_env" = windows ] && [ -z "$_sps_tmp" ]; then
_sps_tmp=$(echo "$USERPROFILE/AppData/Local/Temp/sh-prompt-simple/$$" | tr '\\' '/')
fi
mkdir -p "$_sps_tmp"
: ${USER:=$(whoami)}
prompt_char='>'
[ "$(id -u)" = 0 ] && prompt_char='#'
_e=$(printf "\033")
if [ -z "$ZSH_VERSION" ]; then
if [ "$SPS_ESCAPE" = 1 ]; then
PS1="\
"'`_SPS_get_status`'"\
\["'`_SPS_window_title`'"\]\
\["'`_SPS_status_color`'"\]"'`_SPS_status`'" \
\[${_e}[0;95m\]${_sps_env} \
\[${_e}[33m\]"'`_SPS_cwd`'" \
\[${_e}[0;36m\]"'`_SPS_git_open_bracket`'"\
\[${_e}[35m\]"'`_SPS_git_branch`'"\
\[${_e}[0;97m\]"'`_SPS_git_sep`'"\
\["'`_SPS_git_status_color`'"\]"'`_SPS_git_status`'"\
\[${_e}[0;36m\]"'`_SPS_git_close_bracket`'"
\[${_e}[38;2;140;206;250m\]${USER}\
\[${_e}[1;97m\]@\
\[${_e}[0;38;2;140;206;250m\]${hostname} \
\[${_e}[38;2;220;20;60m\]${prompt_char}\
\[${_e}[0m\] "
else
PS1="\
"'`_SPS_get_status`'"\
"'`_SPS_window_title`'"\
"'`_SPS_status_color``_SPS_status`'" \
${_e}[0;95m${_sps_env} \
${_e}[33m"'`_SPS_cwd`'" \
${_e}[0;36m"'`_SPS_git_open_bracket`'"\
${_e}[35m"'`_SPS_git_branch`'"\
${_e}[0;97m"'`_SPS_git_sep`'"\
"'`_SPS_git_status_color``_SPS_git_status`'"\
${_e}[0;36m"'`_SPS_git_close_bracket`'"
${_e}[38;2;140;206;250m${USER}\
${_e}[1;97m@\
${_e}[0;38;2;140;206;250m${hostname} \
${_e}[38;2;220;20;60m${prompt_char}\
${_e}[0m "
fi
else # zsh
setopt PROMPT_SUBST
precmd() {
printf "\
$(_SPS_get_status)\
$(_SPS_window_title)\
$(_SPS_status_color)$(_SPS_status) \
\033[0;95m${_sps_env} \
\033[33m$(_SPS_cwd) \
\033[0;36m$(_SPS_git_open_bracket)\
\033[35m$(_SPS_git_branch)\
\033[0;97m$(_SPS_git_sep)\
$(_SPS_git_status_color)$(_SPS_git_status)\
\033[0;36m$(_SPS_git_close_bracket)
"
}
PS1="%{${_e}[38;2;140;206;250m%}${USER}%{${_e}[1;97m%}@%{${_e}[0m${_e}[38;2;140;206;250m%}${hostname} %{${_e}[38;2;220;20;60m%}${prompt_char}%{${_e}[0m%} "
fi
}
_SPS_quit() {
rm -rf "$_sps_tmp"
local tmp_root=${_sps_tmp%/*}
if [ -z "$(find "$tmp_root" -mindepth 1 -type d)" ]; then
rm -rf "$tmp_root"
fi
return 0
}
trap "_SPS_quit" EXIT
_SPS_uname_o() {
# macOS does not have `uname -o`.
uname -o 2>/dev/null || uname
}
_SPS_detect_non_linux_env() {
if [ -n "$TERMUX_VERSION" ]; then
echo termux
return
elif [ "$(_SPS_uname_o)" = Darwin ]; then
echo macOS
return
elif [ "$(_SPS_uname_o)" = Msys ] && [ -n "$MSYSTEM" ]; then
echo "$MSYSTEM" | tr '[:upper:]' '[:lower:]'
return
elif [ "$(_SPS_uname_o)" = Cygwin ]; then
echo cygwin
return
elif echo "$(_SPS_uname_o)$(uname 2>/dev/null)" | grep -qi windows || \
[ -d /Windows/System32 ]; then
SPS_ESCAPE=1 # Possibly a busybox for Windows build.
echo windows
return
fi
uname | sed -E 's/[[:space:][:punct:]]+/_/g'
}
_SPS_detect_distro() {
[ -f /etc/os-release ] || return
local distro=$(sed -nE '/^ID="/s/^ID="([^"]+)".*/\1/p; s/^ID=([^[:space:]]+)/\1/p; t match; d; :match; q' /etc/os-release)
local normalized=$(echo "$distro" | sed -E '
# Remove all buzzwords and extraneous words.
s/(GNU|Secure|open)//ig
:buzzwords
s/(^|[[:space:][:punct:]]+)(LTS|toolkit|operating|solutions|Security|Firewall|Cluster|Distribution|system|project|interim|enterprise|corporate|server|desktop|studio|edition|live|libre|industrial|incognito|remix|and|on|a|for|the|[0-9]+)($|[[:space:][:punct:]]+)/\1\3/i
t buzzwords
# Remove GNU or Linux not at the beginning of phrase, or X
# as a word by itself not at the beginning of phrase.
:gnulinux
s,([[:space:][:punct:]]+)(GNU|Linux|X([[:space:][:punct:]]|$)),\1,i
t gnulinux
# Trim space/punctuation from start/end.
s/[[:space:][:punct:]]+$//
s/^[[:space:][:punct:]]+//
# Normalize all suse products to suse.
s/.*(^|[[:space:][:punct:]])SUSE($|[[:space:][:punct:]]).*/suse/i
t
# Remove everyting before the first /, if what is after is
# longer than 3 characters.
s;.+/(.{3,});\1;
# Replace all space sequences with underscore.
s/[[:space:]]+/_/g
# Keep names with one hyphen, replace all other punctuation
# sequnces with underscore.
/^[^-]+-[^-]+$/!{
s/[[:punct:]]+/_/g
}
');
# If normalized name is longer than 15 characters, abbreviate
# instead.
if [ "$(printf %s "$normalized" | wc -c)" -gt 15 ]; then
normalized=$(echo "$distro" | sed -E '
:abbrev
s/(^|[[:space:][:punct:]]+)([[:alpha:]])[[:alpha:]]+/\1\2/
t abbrev
s/[[:space:][:punct:]]+//g
')
fi
echo "$normalized"
}
_SPS_detect_env() {
case "$(_SPS_uname_o)" in
*Linux)
_sps_env=$(_SPS_detect_distro)
: ${_sps_env:=linux}
;;
*)
_sps_env=$(_SPS_detect_non_linux_env)
;;
esac
}
_SPS_get_status() {
if [ "$?" -eq 0 ]; then
echo 0 > "$_sps_tmp/cmd_status"
else
echo 1 > "$_sps_tmp/cmd_status"
fi
}
_SPS_status_color() {
if [ "$(cat "$_sps_tmp/cmd_status")" -eq 0 ]; then
printf "\033[0;32m"
else
printf "\033[0;31m"
fi
}
_SPS_status() {
if [ "$(cat "$_sps_tmp/cmd_status")" -eq 0 ]; then
printf 'v'
else
printf 'x'
fi
}
_SPS_in_git_tree() {
! command -v git >/dev/null && return 1
if [ -f "$_sps_tmp/in_git_tree" ]; then
return "$(cat "$_sps_tmp/in_git_tree")"
fi
local OLDPWD=$PWD
local matched=
while ! (printf "$PWD" | grep -Eqi '^([[:alnum:]]+:)?[\/]$'); do
if [ -d .git ]; then
matched=1
break
fi
cd ..
done
cd "$OLDPWD"
if [ -n "$matched" ]; then
echo 0 > "$_sps_tmp/in_git_tree"
return 0
fi
echo 1 > "$_sps_tmp/in_git_tree"
return 1
}
_SPS_git_status_color() {
if [ -z "$SPS_STATUS" ] || ! _SPS_in_git_tree; then
return
fi
status=$(LANG=C LC_ALL=C git status 2>/dev/null)
clean=
if echo "$status" | grep -Eq 'working tree clean'; then
# For remote tracking branches, check that the branch is up-to-date with the remote branch.
if [ "$(echo "$status" | wc -l)" -le 2 ] || echo "$status" | grep -Eq '^Your branch is up to date with'; then
clean=1
fi
fi
if [ -n "$clean" ]; then
echo 0 > "$_sps_tmp/git_status"
printf "\033[0;32m"
else
echo 1 > "$_sps_tmp/git_status"
printf "\033[0;31m"
fi
}
_SPS_git_status() {
if [ -z "$SPS_STATUS" ] || ! _SPS_in_git_tree; then
return
fi
if [ "$(cat "$_sps_tmp/git_status")" = 0 ]; then
printf 'v'
else
printf '~~~'
fi
}
_SPS_git_sep() {
if [ -z "$SPS_STATUS" ] || ! _SPS_in_git_tree; then
return
fi
printf '|'
}
_SPS_git_open_bracket() {
_SPS_in_git_tree && printf '['
}
_SPS_git_close_bracket() {
_SPS_in_git_tree && printf ']'
rm "$_sps_tmp/"*git* 2>/dev/null
}
_SPS_git_branch() {
! _SPS_in_git_tree && return
git rev-parse --abbrev-ref HEAD 2>/dev/null
}
_SPS_cwd() {
case "$PWD" in
"$HOME")
printf '~'
;;
"$HOME"/*)
local pwd=${PWD#$HOME}
while :; do
case "$pwd" in
/*)
pwd=${pwd#/}
;;
*)
break
;;
esac
done
printf "~/${pwd}"
;;
*)
printf "${PWD}"
;;
esac
}
_SPS_window_title() {
[ "$SPS_WINDOW_TITLE" = 0 ] && return
printf "\033]0;${_sps_domain_or_localnet_host}\007"
}
_SPS_main