-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt_garrett_setup
454 lines (368 loc) · 14.6 KB
/
prompt_garrett_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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#!/usr/bin/env zsh
#
# Garrett Zsh Theme for Prezto
# Created with modified code by Chauncey Garrett
#
# A prompt with the information you need the moment you need it.
#
# This prompt has the following features:
# * change prompt color when uid is root
# * change host color when on ssh
# * display full or truncated hostname on ssh
# * determine the number of background jobs
# * report the truncated present working directory
# * report return codes other than 0
# * report local time
# * report the terminal line number
# * report git status, git remote status, git prompt info and git SHA information
# * indicate vi-mode
# * notifications for commands taking longer than x time
#
# Features may be disabled and rearranged as desired by using the corresponding tokens.
#
#
# Load dependencies
#
pmodload 'helper'
#
# PROMPT | prompt_garrett_help
# - prompt configuration help
# TODO not even remotely finished...
#
# function prompt_garrett_help()
# {
# cat <<'EOF'
# This prompt is configurable via styles:
# Context: :prompt:garrett
# Colors (red green yellow blue magenta cyan white grey):
# prompt_garrett_color_user | The color for user@host. Defaults to 'green'
# root_color | The color for the hostname for root. Defaults to 'red'
# prompt_garrett_color_prompt | The color for everything else. Defaults to ''
# Path (path type - possible values):
# ratio - use COLUMNS/ratio to clip the path. Default.
# fixed - use a fixed maximum length.
# subdir - clip by number of subdirectories.
# full - show the full path
# Path length styles:
# ratio | the ratio for the 'ratio' path style, funnily enough. Defaults to 6.
# length | the maximum length for the 'fixed' path style. Defaults to 20.
# subdir | the number of subdirectories to show for the 'subdir' path style. Defaults to 3.
# You can set styles in the current terminal to test things out, values will be updated.
# EOF
# }
#
# PROMPT | Preview
# - display a preview of the prompt
# TODO not ready
#
# function prompt_garrett_preview
# {
# if (( $# > 0 ))
# then
# prompt_preview_theme 'garrett' "$@"
# else
# prompt_preview_theme 'garrett' # red green blue
# fi
# }
#
# PROMPT | prompt_garrett_pwd
# - truncate the pwd display, if needed
#
function prompt_garrett_pwd
{
local pwd="${PWD/#$HOME/~}"
if [[ "$pwd" == (#m)[/~] ]]
then # pwd will fit
prompt_garrett_current_dir="${prompt_garrett_color_pwd}$MATCH"
unset MATCH
else # pwd needs truncation
prompt_garrett_current_dir="${prompt_garrett_color_pwd}${${${(@j:/:M)${(@s:/:)pwd}##.#?}:h}%/}/${pwd:t}"
fi
}
#
# Notifications
# - notify of command completion after n seconds has passed
#
function prompt_garrett_precmd_notification
{
# trigger a notification after n seconds have elapsed
prompt_garrett_delay_after_notification=1
# determine time elapsed
prompt_garrett_precmd_time_elapsed_start=${prompt_garrett_preexec_time:-$(date +'%s')}
prompt_garrett_precmd_time_elapsed_stop=$(date +'%s')
let prompt_garrett_precmd_time_elapsed=$prompt_garrett_precmd_time_elapsed_stop-$prompt_garrett_precmd_time_elapsed_start
# notify!
if [ $prompt_garrett_precmd_time_elapsed -gt $prompt_garrett_delay_after_notification ]
then # n seconds have passed, so notify!
if [[ "$OSTYPE" == darwin* ]] && (( $+commands[terminal-notifier] ))
then
terminal-notifier -title "${prompt_garrett_preexec_cmd:-Unknown command}" -message "Required $prompt_garrett_precmd_time_elapsed s" -sound Tink
elif [[ "$OSTYPE" == darwin* ]] && (( $+commands[growlnotify] ))
then
growlnotify -n "Terminal" -t "${prompt_garrett_preexec_cmd:-Unknown command}" -m "Required $prompt_garrett_precmd_time_elapsed s"
else
tput bel
fi
fi
}
#
# PROMPT | prompt_garrett_chpwd
# - a function which is executed whenever the directory is changed
#
function prompt_garrett_chpwd
{
# list the contents of the new directory
if is-callable 'dircolors';
then # GNU flavor ls
if zstyle -t ':prezto:module:utility:ls' color
then
ls --group-directories-first --color=auto
else
ls -F
fi
else # BSD flavor ls
if zstyle -t ':prezto:module:utility:ls' color
then
ls -G
else
ls -F
fi
fi
}
#
# PROMPT | prompt_garrett_preexec
# - functions that are called before command execution
#
function prompt_garrett_preexec
{
# Define timer and cmd for notification
export prompt_garrett_preexec_time=$(date +'%s')
export prompt_garrett_preexec_cmd="\$ $1"
# Ensure terminal code isn't colored from prompt
print -n "$reset_color"
}
#
# PROMPT | prompt_garrett_precmd
# - functions that are called before each prompt is displayed
#
function prompt_garrett_precmd
{
setopt LOCAL_OPTIONS
unsetopt XTRACE KSH_ARRAYS
# Show number of background jobs
prompt_garrett_number_jobs="%(1j.${prompt_garrett_color_prompt}J:${cyan}%j .)"
# Format PWD
prompt_garrett_pwd
# Trigger a notification after x time has elapsed
eval prompt_garrett_precmd_notification
# Get ruby info
if (( $+functions[ruby-info] ))
then
ruby-info
fi
# Get git repository info
if (( $+functions[git-info] ))
then
git-info
fi
#
# Add a line to prompt for visibility
#
# Determine the width
local prompt_garrett_width_terminal
(( prompt_garrett_width_terminal= ${COLUMNS} - 1 ))
# Determine the length needed for prompt_garrett_space
# NOTE: Be sure not to include the ${(e)prompt_garrett_space} portion or it won't work
local prompt_line1="${prompt_garrett_upper_left_corner}( ${prompt_garrett_current_dir}${git_info[remote_status]}${git_info[prompt_info]}${git_info[local_status]}${git_info[sha]} )( ${ruby_info[version]}${prompt_garrett_location} )${prompt_garrett_upper_right_corner}"
local zero='%([BSUbfksu]|([FB]|){*})'
local prompt_garrett_width_line1=${#${(S%%)prompt_line1//$~zero/}}
# Calculate the padding
local prompt_garrett_space_padding
(( prompt_garrett_space_padding= ${prompt_garrett_width_terminal} - ${prompt_garrett_width_line1} ))
# Add the correct number of characters
local prompt_space_character="${prompt_garrett_altchar_padding}"
eval prompt_garrett_space="${prompt_garrett_color_prompt}\${(l.${prompt_garrett_space_padding}..${prompt_space_character}.)}"
# Prompt line 1
print
print -P '${prompt_garrett_altchar_enable}${prompt_garrett_color_prompt}${prompt_garrett_upper_left_corner}( ${prompt_garrett_current_dir}${git_info[remote_status]}${git_info[prompt_info]}${git_info[local_status]}${git_info[sha]} ${prompt_garrett_color_prompt})${prompt_garrett_altchar_enter}${(e)prompt_garrett_space}${prompt_garrett_altchar_leave}( ${ruby_info[version]}${prompt_garrett_location}${prompt_garrett_color_prompt} )${prompt_garrett_upper_right_corner}'
}
#
# PROMPT | prompt_garrett_setup
# - finally, configure the prompt
#
function prompt_garrett_setup
{
# Load necessary modules
setopt LOCAL_OPTIONS
unsetopt XTRACE KSH_ARRAYS
prompt_opts=(cr percent subst)
# Add hooks for calling preexec, precmd & chpwd
autoload -Uz add-zsh-hook
add-zsh-hook preexec prompt_garrett_preexec
add-zsh-hook precmd prompt_garrett_precmd
add-zsh-hook chpwd prompt_garrett_chpwd
#
# Colors
#
# Alias the colors
[[ -z $(functions colors) ]] && autoload -U colors && colors
for color in black red green yellow blue magenta cyan white grey
do
eval $color='%F{${(L)color}}'
eval ${color}_bold='%B{${(L)color}}'
done
# Color scheme
eval prompt_garrett_color_pwd=\$\{${2:-'${blue}'}\}
# eval prompt_garrett_color_pwd=${2:-'${blue}'}
eval prompt_garrett_color_line_number=${5:-'${magenta}'}
eval prompt_garrett_color_time=${6:-'${green}'}
eval prompt_garrett_color_git_branch=${7:-'${green}'}
eval prompt_garrett_color_git_sha=${8:-'${yellow}'}
eval prompt_garrett_color_ruby_version=${8:-'${yellow}'}
# Determine prompt, user and host colors
if [[ "$EUID" = "0" ]] || [[ "$USER" = 'root' ]]
then # root user
# set colors
eval prompt_garrett_color_user=${3:-'${red}'}
eval prompt_garrett_color_host=${3:-'${red}'}
eval prompt_garrett_color_prompt=${3:-'${red}'}
# set style
eval prompt_garrett_user='%S${prompt_garrett_color_user}%n%s'
eval prompt_garrett_host='${prompt_garrett_color_host}%m' # hostname up to first . (dot) (use %M for full hostname)
eval prompt_garrett_location='${prompt_garrett_user}${cyan}@${prompt_garrett_host}' # [email protected]
elif [[ -n "$SSH_CLIENT" || -n "$SSH2_CLIENT" ]]
then # on SSH
# set colors
eval prompt_garrett_color_user=${3:-'${green}'}
eval prompt_garrett_color_host=${3:-'${yellow}'}
eval prompt_garrett_color_prompt=${3:-'${yellow}'}
# set style
eval prompt_garrett_user='%S${prompt_garrett_color_user}%n%s'
eval prompt_garrett_host='${prompt_garrett_color_host}%m' # hostname up to first . (dot) (use %M for full hostname)
eval prompt_garrett_location='${prompt_garrett_user}${cyan}@${prompt_garrett_host}' # [email protected]
else # normal user
# set colors
eval prompt_garrett_color_user=${1:-'${green}'}
eval prompt_garrett_color_host=${1:-'${green}'}
eval prompt_garrett_color_prompt=${1:-'${grey}'}
# set style
eval prompt_garrett_user=''
eval prompt_garrett_host='${prompt_garrett_color_host}%m' # hostname up to first . (dot) (use %M for full hostname)
eval prompt_garrett_location='${prompt_garrett_user}${cyan}@${prompt_garrett_host}' # [email protected]
fi
#
# Report return code
#
eval prompt_garrett_return_code='%(?..${red}%? ⏎ ) '
#
# Report local time
#
eval prompt_garrett_current_time='${green}%T' # 24 hour time format
# eval prompt_garrett_current_time='${green}%*' # 24 hour time format, second precise
# eval prompt_garrett_current_time='${green}%t' # AM/PM time format
# Keep the time updated
# TODO: figure out a better way to do this
# schedprompt()
# {
# emulate -L zsh
# zmodload -i zsh/sched
# # Remove existing event, so that multiple calls to "schedprompt" work OK (you could put one in precmd to push the timer 30 seconds into the future, for example.)
# integer i=${"${(@)zsh_scheduled_events#*:*:}"[(i)schedprompt]}
# (( i )) && sched -$i
# # Test that zle is running before calling the widget (recommended to avoid error messages). Otherwise it updates on entry to zle, so there's no loss.
# zle && zle reset-prompt
# # This ensures we're not too far off the start of the minute
# sched +1 schedprompt
# }
# schedprompt
#
# Report terminal line number
#
eval prompt_garrett_line_number='${green}+${magenta}%!'
#
# Report git info
# NOTE: listed in order in which the information will appear in the prompt
#
# Git verbose data (commit counts, etc.)
# zstyle ':prezto:module:git:info' verbose 'yes'
# Git prompt info
zstyle ':prezto:module:git:info:branch' format "${cyan} λ${prompt_garrett_color_prompt}:${green}%b"
zstyle ':prezto:module:git:info:remote' format ""
zstyle ':prezto:module:git:info:action' format "${yellow} %s"
zstyle ':prezto:module:git:info:position' format "${red} %p"
# Git commit SHA
zstyle ':prezto:module:git:info:commit' format "${yellow} %.7c"
# Git remote status
zstyle ':prezto:module:git:info:behind' format "${magenta} ⬇ "
zstyle ':prezto:module:git:info:ahead' format "${magenta} ⬆ "
zstyle ':prezto:module:git:info:diverged' format "${magenta} ⥮"
zstyle ':prezto:module:git:info:stashed' format "${cyan} ✭"
# Git local status
zstyle ':prezto:module:git:info:clean' format ""
zstyle ':prezto:module:git:info:dirty' format "${prompt_garrett_color_prompt} |"
zstyle ':prezto:module:git:info:added' format "${green} ✚"
zstyle ':prezto:module:git:info:deleted' format "${red} ✗"
zstyle ':prezto:module:git:info:modified' format "${blue} ✱"
zstyle ':prezto:module:git:info:renamed' format "${magenta} ➜"
zstyle ':prezto:module:git:info:unmerged' format "${yellow} ═"
zstyle ':prezto:module:git:info:untracked' format "${white} ◼"
# Git prompt styles
zstyle ':prezto:module:git:info:keys' format \
'prompt_info' "%b" \
'rprompt' "%R" \
'local_status' "%C%D%a%d%m%r%U%u" \
'remote_status' "%B%A%S" \
'sha' "%c" \
#
# Report ruby version
# %v | ruby version
#
zstyle ':prezto:module:ruby:info:version' format "${yellow}ruby:%v "
#
# Vim mode indicator
#
zstyle ':prezto:module:editor:info:keymap:primary' format "${red}❱%(?.${prompt_garrett_color_prompt}.${red})❱❱ "
zstyle ':prezto:module:editor:info:keymap:alternate' format "${red}❰%(?.${prompt_garrett_color_prompt}.${red})❰❰ "
# zstyle ':prezto:module:editor:info:keymap:primary:insert' format "${red}I "
zstyle ':prezto:module:editor:info:keymap:primary:overwrite' format "${red}♺ "
zstyle ':prezto:module:editor:info:completing' format "${red}..."
#
# Use the extended character set, if available
#
typeset -A altchar
set -A altchar ${(s..)terminfo[acsc]}
prompt_garrett_altchar_enable="%{$terminfo[enacs]%}"
prompt_garrett_altchar_enter="%{$terminfo[smacs]%}"
prompt_garrett_altchar_leave="%{$terminfo[rmacs]%}"
prompt_garrett_altchar_padding=${altchar[q]:--}
prompt_garrett_upper_left_corner=${prompt_garrett_altchar_enter}${altchar[l]:--}${altchar[q]:--}${prompt_garrett_altchar_leave}
prompt_garrett_lower_left_corner=${prompt_garrett_altchar_enter}${altchar[m]:--}${altchar[q]:--}${prompt_garrett_altchar_leave}
prompt_garrett_upper_right_corner=${prompt_garrett_altchar_enter}${altchar[q]:--}${altchar[k]:--}${prompt_garrett_altchar_leave}
prompt_garrett_lower_right_corner=${prompt_garrett_altchar_enter}${altchar[q]:--}${altchar[j]:--}${prompt_garrett_altchar_leave}
#
# Finally!
#
# PROMPT | Left
# RPROMPT | Right
# PROMPT2 | Continuation
# PROMPT3 | Selection
# PROMPT4 | Execution trace
# SPROMPT | Autocorrection
# SUDO_PS1 | Backup root prompt
#
if (( $SHLVL == 1 ))
then
export PROMPT='${prompt_garrett_altchar_enable}${prompt_garrett_color_prompt}${prompt_garrett_lower_left_corner}${editor_info[keymap]}'
else
export PROMPT='${prompt_garrett_color_prompt}${prompt_garrett_lower_left_corner}( ${cyan}$SHLVL ${prompt_garrett_color_prompt}) ${editor_info[keymap]}'
fi
export RPROMPT='${editor_info[alternate]}${editor_info[overwrite]}${prompt_garrett_return_code}${prompt_garrett_number_jobs}${prompt_garrett_line_number} ${prompt_garrett_current_time} %(?.${prompt_garrett_color_prompt}.${red})❰${prompt_garrett_color_prompt}${prompt_garrett_lower_right_corner}'
export PROMPT2='(%_) ${editor_info[keymap]}'
export PROMPT3='
(?) ${editor_info[keymap]}'
export PROMPT4='${yellow}+${blue}%N ${green}@${magenta}%i ${reset_color}'
export SPROMPT='
${prompt_garrett_color_prompt}Correct ${red}%R${prompt_garrett_color_prompt} to ${green}%r${prompt_garrett_color_prompt} ? [nyae] '
export SUDO_PS1="\[\e[31;1;46m\][\u] \w \$\[\e[0m\] "
}
prompt_garrett_setup "$@"