-
Notifications
You must be signed in to change notification settings - Fork 0
/
zsh-simple-prompt.plugin.zsh
executable file
·145 lines (131 loc) · 2.98 KB
/
zsh-simple-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
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
autoload -Uz add-zsh-hook
NOTIFIER=$(command -v terminal-notifier)
CURRENT_CMD=""
_zsh_simple_prompt__time() {
if date +%N | grep -v N >/dev/null; then
date +%s%3N
else
date +%s000
fi
}
_zsh_simple_prompt__signal_name() {
local sigs
sigs=$(
cat <<EOF
01 SIGHUP
02 SIGINT
03 SIGQUIT
04 SIGILL
05 SIGTRAP
06 SIGABRT
07 SIGEMT
08 SIGFPE
09 SIGKILL
10 SIGBUS
11 SIGSEGV
12 SIGSYS
13 SIGPIPE
14 SIGALRM
15 SIGTERM
16 SIGURG
17 SIGSTOP
18 SIGTSTP
19 SIGCONT
20 SIGCHLD
21 SIGTTIN
22 SIGTTOU
23 SIGIO
24 SIGXCPU
25 SIGXFSZ
26 SIGVTALRM
27 SIGPROF
28 SIGWINCH
29 SIGINFO
30 SIGUSR1
31 SIGUSR2
EOF
)
local query
query=$(printf %02d "${1}")
echo "${sigs}" | grep "${query}" | awk '{print $2}'
}
_zsh_simple_prompt__exitcode2signal() {
local code=$1
if [[ $code -gt 128 && $code -le $((128 + 31)) ]]; then
local sigid signame
sigid=$((code - 128))
signame=$(_zsh_simple_prompt__signal_name $sigid)
echo "${signame}"
return
fi
}
_zsh_simple_prompt__start_timer() {
local cmd
cmd="$1"
if [[ -n "$cmd" ]]; then
timer=$(_zsh_simple_prompt__time)
fi
}
add-zsh-hook preexec _zsh_simple_prompt__start_timer
_zsh_simple_prompt__register_command_name() {
CURRENT_CMD="$1"
}
add-zsh-hook preexec _zsh_simple_prompt__register_command_name
_zsh_simple_prompt__configure_prompt() {
local code=$?
psvar=()
local st sig
st=""
sig=$(_zsh_simple_prompt__exitcode2signal $code)
if [[ -n "${sig}" ]]; then
st="${sig}($((code - 128)))"
elif [[ "${code}" -ne 0 ]]; then
st="${code}"
fi
local elapsed now t
elapsed=""
if [[ -n "$timer" ]]; then
now=$(_zsh_simple_prompt__time)
t=$((now - timer))
elapsed="$(_zsh_simple_prompt__human_readable_elapsed_time $t)"
unset timer
fi
if [[ -n "$st" && -n "${CURRENT_CMD}" ]]; then
psvar[1]="$st "
[[ -n "${psvar[*]}" ]] && psvar[10]=1
fi
if [[ "${SPL_PROMPT_CMD_TIME_MIN}" -lt "$t" ]]; then
[[ -n "$elapsed" ]] && psvar[2]="$elapsed "
[[ -n "${psvar[*]}" ]] && psvar[10]=1
fi
if [[ ${t} -ge ${SPL_PROMPT_NOTIFY_TIME_MIN} && -n "${NOTIFIER}" ]]; then
local result
[[ ${code} -eq 0 ]] &&
result="DONE 👍" ||
result="FAILED ‼️"
${NOTIFIER} \
-title "${result}" \
-subtitle "${CURRENT_CMD}" \
-message "${psvar[1]}${psvar[2]}"
fi
unset CURRENT_CMD
}
add-zsh-hook precmd _zsh_simple_prompt__configure_prompt
_zsh_simple_prompt__human_readable_elapsed_time() {
local o=''
local t=$(($1 / 1000))
local ms=$(($1 % 1000)) # 1.11 → 11, 1.1 → 10, 1.01 → 01
t=$(cut -d. -f 1 <<<$t)
h=$(($t / 3600))
((h > 0)) && o="${h}h"
t=$((t % 3600))
m=$(($t / 60))
((m > 0)) && o="${o}${m}m"
s=$((t % 60))
((h == 0)) && ((m == 0)) && s="${s}.${ms}"
[[ "$(bc <<<"$s > 0")" -eq 1 ]] && o="${o}${s}s"
echo "${o}"
}
SPL_PROMPT_CMD_TIME_MIN=${SPL_PROMPT_CMD_TIME_MIN:-50}
SPL_PROMPT_NOTIFY_TIME_MIN=${SPL_PROMPT_NOTIFY_TIME_MIN:-60000}
PROMPT="%10(v|⇢ |)%F{yellow}%1v%f%F{blue}%2v%f%10(v|"$'\n'"|)""%(?,%F{green},%F{red})%B$%b%f "