-
Notifications
You must be signed in to change notification settings - Fork 0
/
horizontal.zsh
280 lines (237 loc) · 9.06 KB
/
horizontal.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
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
# Horizontal by Nui Narongwet
# MIT License
# Naming convension
# functions
# - avoid function keyword
# - snake case
# - prefix function name with _horizontal_
# - store function result in FUNCNAME_result variable
# Set a plaintext of $1 without formatting
_horizontal_plaintext() {
readonly zero_length='%([BSUbfksu]|([FB]|){*})'
typeset -g _horizontal_plaintext_result=${(S%%)1//$~zero_length/}
}
_horizontal_reset_prompt() {
# face color
readonly happy='green'
readonly sad='red'
if ((${horizontal[color]})); then
# prompt face turn green if the previous command did exit with 0,
# otherwise turn yellow
PROMPT="%F{${horizontal[base_color]}} '--%f%B>%(1j. %F{red}%j!%f.) %(?.%F{$happy}:%).%F{$sad}:()%b%f "
# restore prompt highlighting if needed
if ((${#ZSH_HIGHLIGHT_HIGHLIGHTERS} == 0)); then
ZSH_HIGHLIGHT_HIGHLIGHTERS=($_horizontal_orig_zsh_highlight_highlighters)
fi
unset _horizontal_orig_zsh_highlight_highlighters
else
PROMPT=" '-->%(1j. %j!.) %(?.:%).:() "
# backup prompt highlighting
if ((${+_horizontal_orig_zsh_highlight_highlighters} == 0)); then
_horizontal_orig_zsh_highlight_highlighters=($ZSH_HIGHLIGHT_HIGHLIGHTERS)
fi
# and disable it
ZSH_HIGHLIGHT_HIGHLIGHTERS=()
fi
}
# Set a string that when combine with $1
# its length is equal to
# - $COLUMNS if length of $1 <= $COLUMNS
# - $COLUMNS * 2 if length of $1 > $COLUMNS
_horizontal_gen_padding() {
_horizontal_plaintext "${(j::)@}"
integer prompt_length=$#_horizontal_plaintext_result
integer n=$((COLUMNS - prompt_length - 1))
((n < 0)) && n=$((COLUMNS * 2 - prompt_length))
local IFS=${horizontal[fill_character]}
if ((n > 0)); then
typeset -g _horizontal_gen_padding_result=${(l:$n:::)}
else
typeset -g _horizontal_gen_padding_result=
fi
}
_horizontal_join_status() {
local separator=${horizontal_status_separator:-"%F{${horizontal[base_color]}} | %f"}
local string
local item
for item in $@; do string+=$separator$item; done
string=${string:${#separator}} # remove leading separator
typeset -g _horizontal_join_status_result=$string
}
# Turn number of seconds into human readable format
# 78555 => 21h 49m 15s
# 2781 => 46m 21s
_horizontal_human_time() {
local result=""
local total_seconds=$1
local days=$((total_seconds / 60 / 60 / 24))
local hours=$((total_seconds / 60 / 60 % 24))
local minutes=$((total_seconds / 60 % 60))
local seconds=$((total_seconds % 60))
((days > 0)) && result+="${days}d "
((hours > 0)) && result+="${hours}h "
((minutes > 0)) && result+="${minutes}m "
result+="${seconds}s"
typeset -g _horizontal_human_time_result=$result
}
_horizontal_exec_seconds() {
integer stop=$EPOCHSECONDS
integer start=${_horizontal_cmd_timestamp:-$stop}
typeset -g _horizontal_exec_seconds_result=$((stop-start))
}
_horizontal_idle_seconds() {
integer stop=$EPOCHSECONDS
integer start=${_horizontal_idle_timestamp:-$stop}
typeset -g _horizontal_idle_seconds_result=$((stop-start))
}
_horizontal_git_dirty() {
if ((${horizontal[git_untracked_dirty]})); then
test -z "$(command git status --porcelain --ignore-submodules -unormal)"
else
command git diff --no-ext-diff --quiet --exit-code
fi
if (($? == 0)); then
typeset -g _horizontal_git_dirty_result=
else
typeset -g _horizontal_git_dirty_result='*'
fi
}
_horizontal_userhost() {
if [[ ${horizontal[userhost]} == 1 ]]; then
typeset -g _horizontal_userhost_result="%b%f%n|${horizontal_hostname:-%m}%f: "
else
typeset -g _horizontal_userhost_result=
fi
}
prompt_horizontal_preexec() {
typeset -g _horizontal_cmd_timestamp=$EPOCHSECONDS
# shows the executed command in the title when a process is active
print -n -P -- "\e]0;"
print -n -r -- "$2"
print -n -P -- "\a"
}
prompt_horizontal_precmd() {
_horizontal_reset_prompt
# shows the hostname
print -Pn -- '\e]0;%M\a'
local preprompt
local rpreprompt
_horizontal_userhost
preprompt="%b%F{${horizontal[base_color]}}.-%B(${_horizontal_userhost_result}%B%F{yellow}%~%F{${horizontal[base_color]}})%b%F{${horizontal[base_color]}}-%f"
((${horizontal[status]})) && {
local -a prompt_status
local -a rprompt_status
local git_info
local timestamp
integer displayed_exec_time
((${horizontal[git]})) && vcs_info
# git branch and dirty status
((${horizontal[git]})) && [[ -n $vcs_info_msg_0_ ]] && {
((${horizontal[git_dirty]})) && _horizontal_git_dirty
git_info="${vcs_info_msg_0_}${_horizontal_git_dirty_result}"
[[ -n $git_info ]] && prompt_status+=$git_info
}
# python virtual environment
((${horizontal[virtualenv]})) && [[ -n $VIRTUAL_ENV ]] && {
prompt_status+="(${VIRTUAL_ENV:t}%)"
}
# last command execute time
((${horizontal[exec_time]})) && {
displayed_exec_time=0
_horizontal_exec_seconds
(($_horizontal_exec_seconds_result >= ${horizontal[show_exec_time_threshold]})) && {
displayed_exec_time=1
_horizontal_human_time $_horizontal_exec_seconds_result
prompt_status+="%F{yellow}$_horizontal_human_time_result%f"
}
((! $displayed_exec_time)) && {
_horizontal_idle_seconds
(($_horizontal_idle_seconds_result >= ${horizontal[show_idle_time_threshold]})) && {
_horizontal_human_time $_horizontal_idle_seconds_result
prompt_status+="%F{blue}$_horizontal_human_time_result%f"
}
}
}
((${horizontal[timestamp]})) && {
_horizontal_exec_seconds
(($_horizontal_exec_seconds_result >= ${horizontal[show_timestamp_threshold]})) && {
strftime -s timestamp '%D %R' $EPOCHSECONDS
rprompt_status+=$timestamp
}
}
# put status to preprompt line
((${#prompt_status} > 0)) && {
_horizontal_join_status $prompt_status
preprompt+=" $_horizontal_join_status_result "
}
# put rstatus to right of preprompt line
((${#rprompt_status} > 0)) && {
_horizontal_join_status $rprompt_status
rpreprompt+=" $_horizontal_join_status_result %F{${horizontal[base_color]}}-%f"
}
}
# make a horizontal line
((${horizontal[hr]})) && {
_horizontal_gen_padding $preprompt $rpreprompt
preprompt+="%F{${horizontal[base_color]}}${_horizontal_gen_padding_result}%f$rpreprompt"
}
# blank line before preprompt line
((${horizontal[cozy]})) && preprompt="\n$preprompt"
((${horizontal[color]} == 0)) && {
_horizontal_plaintext $preprompt
preprompt=$_horizontal_plaintext_result
}
# print preprompt line
print -P -- $preprompt
# reset value since `preexec` isn't always triggered
unset _horizontal_cmd_timestamp
typeset -g _horizontal_idle_timestamp=$EPOCHSECONDS
}
prompt_horizontal_setup() {
typeset -gA horizontal
# Enable/Disable horizontal features
: ${horizontal[base_color]:=cyan}
: ${horizontal[color]:=1}
: ${horizontal[cozy]:=0}
: ${horizontal[exec_time]:=1}
: ${horizontal[git]:=1}
: ${horizontal[git_dirty]:=1}
: ${horizontal[git_untracked_dirty]:=1}
: ${horizontal[hr]:=1}
: ${horizontal[status]:=1}
: ${horizontal[timestamp]:=1}
: ${horizontal[userhost]:=1}
: ${horizontal[virtualenv]:=1}
: ${horizontal[show_exec_time_threshold]:=5}
: ${horizontal[show_idle_time_threshold]:=300}
: ${horizontal[show_timestamp_threshold]:=180}
: ${horizontal[fill_character]:=-}
# horizontal_branch_symbol=''
# horizontal_hostname=
# horizontal_status_separator="%F{${horizontal[base_color]}} | %f"
# prevent percentage showing up
# if output doesn't end with a newline
export PROMPT_EOL_MARK=''
prompt_opts=(cr percent)
zmodload zsh/datetime
autoload -Uz add-zsh-hook
autoload -Uz vcs_info
add-zsh-hook precmd prompt_horizontal_precmd
add-zsh-hook preexec prompt_horizontal_preexec
local branch_symbol=${horizontal_branch_symbol-''}
zstyle ':vcs_info:*' enable git
zstyle ':vcs_info:*' use-simple true
# only export two msg variables from vcs_info
zstyle ':vcs_info:*' max-exports 2
# vcs_info_msg_0_ = ' %b' (for branch)
# vcs_info_msg_1_ = 'x%R' git top level (%R), x-prefix prevents creation of a named path (AUTO_NAME_DIRS)
zstyle ':vcs_info:git*' formats "$branch_symbol%b" 'x%R'
zstyle ':vcs_info:git*' actionformats "$branch_symbol%b|%a" 'x%R'
# if vcs_info is not load properly, disable git integration
vcs_info 2>/dev/null || horizontal[git]=0
# disable auto updating PS1 by virtualenv
VIRTUAL_ENV_DISABLE_PROMPT=1
export PYENV_VIRTUALENV_DISABLE_PROMPT=1
}
prompt_horizontal_setup "$@"
# vim: ft=zsh sw=4 sts=4 ts=4