-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathastral.zsh-theme
592 lines (525 loc) · 15.9 KB
/
astral.zsh-theme
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
#!/usr/bin/env zsh
###############################################################################
# `.. `..
# `.. `..
# `.. `.... `.`. `.`. `... `.. `..
# `.. `.. `.. `.. `.. `.. `.. `..
# `.. `.. `... `.. `.. `.. `.. `..
# `.. `.. `.. `.. `.. `.. `.. `..
# `.. `...`.. `.. `.. `... `.. `...`...
#
# Astral - A ZSH Theme with Zen Mode.
#
# https://github.com/xwmx/astral
#
# Prompt reference:
# http://www.nparikh.org/unix/prompt.php
# http://bolyai.cs.elte.hu/zsh-manual/zsh_15.html
###############################################################################
###############################################################################
# Hooks
###############################################################################
# $ASTRAL_COMMAND_START_TIME
#
# The start time value before each command is executed.
#
# NOTE: Use `preexec` as start time, and reset to '0' in `precmd` so it's
# reset every time the promp is drawn, including when nothing is entered in
# the prompt before return is pressed.
export ASTRAL_COMMAND_START_TIME
preexec() {
ASTRAL_COMMAND_START_TIME="$(date +%s)"
}
precmd() {
# NOTE: This ordering is important. `$_COMMAND_START_TIME` will be used after
# `precmd` executes and is likely set by `preexec`, and we need
# `$ASTRAL_COMMAND_START_TIME` to be unset between `precmd` and the next
# `preexec` for when the prompt is redrawn without calling `preexec`, such
# as when enter is pressed without a command.
_COMMAND_START_TIME="${ASTRAL_COMMAND_START_TIME:-}"
ASTRAL_COMMAND_START_TIME=""
}
###############################################################################
# Helpers
###############################################################################
# _astral_spaces()
#
# Usage:
# _astral_line [<length>]
#
# Description:
# Print a line of spaces <length> columns long, defauling to `$COLUMNS`.
_astral_spaces() {
local _length="${1:-${COLUMNS}}"
printf '%*s' "${_length}"
}
# _astral_visible_length()
#
# Usage:
# _astral_visible_length <string>
#
# Description:
# Print the visible length of a string.
#
# References:
# http://stackoverflow.com/a/10564427
_astral_visible_length() {
local _string="${1:-}"
local _zero='%([BSUbfksu]|([FBK]|){*})'
local _length=${#${(S%%)_string//$~_zero/}}
printf "%s\n" "${_length}"
}
###############################################################################
# Components
###############################################################################
# _command
###############################################################################
# _astral_command_prompt()
#
# Usage:
# _astral_command_prompt
#
# Description:
# Display a row of color '❯' characters. Use last return status to display
# green to blue gradient if the last command returned with a 0 and red to
# blue if it returned with a non-zero status.
_astral_command_prompt() {
local _prompt_0=""
for __color in green yellow cyan blue
do
_prompt_0="${_prompt_0}%{$fg_bold[${__color}]%}❯"
done
local _prompt_non_0=""
for __color in red magenta blue cyan
do
_prompt_non_0="${_prompt_non_0}%{$fg_bold[${__color}]%}❯"
done
printf "%s\n" "%(?:${_prompt_0}:${_prompt_non_0}%s)"
}
# _git
###############################################################################
# _astral_git_prompt()
#
# Usage:
# _astral_git_prompt
#
# Description:
# Generate the git prompt.
#
# Reimplements some functions here:
# https://github.com/robbyrussell/oh-my-zsh/blob/master/lib/git.zsh
#
# See also:
# https://news.ycombinator.com/item?id=10121997
# https://github.com/yonchu/zsh-vcs-prompt
# https://github.com/arialdomartini/oh-my-git
# https://github.com/michaeldfallen/git-radar
_astral_git_prompt() {
# `git_prompt_info` variables
local _prompt_prefix="git:%{$fg[cyan]%}"
local _prompt_suffix="%{${reset_color}%}"
local _prompt_dirty="%{$fg[blue]%} %{$fg[yellow]%}✕%{${reset_color}%}"
local _prompt_clean="%{$fg[blue]%}"
_parse_git_dirty() {
local STATUS=''
local FLAGS
FLAGS=('--porcelain')
if [[ "$(command git config --get oh-my-zsh.hide-dirty)" != "1" ]]
then
if [[ ${POST_1_7_2_GIT} -gt 0 ]]
then
FLAGS+='--ignore-submodules=dirty'
fi
if [[ "${DISABLE_UNTRACKED_FILES_DIRTY}" = "true" ]]
then
FLAGS+='--untracked-files=no'
fi
STATUS=$(command git status ${FLAGS} 2> /dev/null | tail -n1)
fi
if [[ -n ${STATUS} ]]
then
printf "%s\n" "${_prompt_dirty}"
else
printf "%s\n" "${_prompt_clean}"
fi
}
_git_prompt_info() {
local _ref
if [[ "$(command git config --get oh-my-zsh.hide-status 2>/dev/null)" != "1" ]]
then
_ref=$(command git symbolic-ref HEAD 2> /dev/null) \
|| _ref=$(command git rev-parse --short HEAD 2> /dev/null) \
|| return 0
printf "%s\n" "${_prompt_prefix}${_ref#refs/heads/}$(_parse_git_dirty)${_prompt_suffix}"
fi
}
# For now, just fall back to the `git_prompt_info`oh-my-zsh shell function.
local _raw_git_prompt
_raw_git_prompt="$(_git_prompt_info)"
if [[ -n "${_raw_git_prompt}" ]]
then
printf "%s\n" "%{$fg_bold[blue]%}${_raw_git_prompt}%{$fg_bold[blue]%}"
else
printf ""
fi
}
# _machine
###############################################################################
# _astral_machine()
#
# Usage:
# _astral_machine
#
# Description:
# Display alternate machine prompt for remote sessions.
#
# This makes it easier to distinguish between local and remote sessions.
_astral_machine() {
local _astral_machine_string
if [[ "${SESSION_TYPE}" == "remote/ssh" ]]
then
local _astral_ssh_prefix="%{$fg_bold[blue]%}%{$fg_bold[yellow]%}ssh❯"
_astral_machine_string="${_astral_ssh_prefix} %{$fg_bold[green]%}%n@%m"
else
_astral_machine_string="%{$fg_bold[blue]%}%m"
fi
printf "%s\n" "${_astral_machine_string}"
}
# _notebook
###############################################################################
# _astral_notebook()
#
# Usage:
# _astral_notebook
#
# Description:
# Display current `nb` notebook name if available. About `nb`:
# https://github.com/xwmx/nb
_astral_notebook() {
if hash "nb" &>/dev/null
then
local _maybe_notebook_name=
_maybe_notebook_name="$(
# Run command in code block to capture `time` output.
{
NB_BASH_UPDATE_PROMPT_ENABLED=0 nb notebooks current --skip-git
} 2>/dev/null | head -1 || :
)"
if [[ -n "${_maybe_notebook_name:-}" ]] &&
[[ ! "${_maybe_notebook_name}" =~ \-\-\-\-\- ]]
then
local _notebook_prefix="%{$fg_bold[blue]%}nb:"
local _notebook_value="%{$fg_bold[cyan]%}${_maybe_notebook_name}"
local _notebook_suffix="%{$fg_bold[blue]%}%{${reset_color}%} "
local _notebook_string="${_notebook_prefix}${_notebook_value}${_notebook_suffix}"
else
_notebook_string=""
fi
printf "%s\n" "${_notebook_string}"
fi
}
# _ruby
###############################################################################
# _astral_ruby_prompt()
#
# Usage:
# _astral_ruby_prompt
#
# Description:
# If _astral_ruby_version_status() returns a version, generate the prompt
# section displaying the Ruby version.
_astral_ruby_prompt() {
local _maybe_ruby_version=
local _ruby_version_string=
if hash "asdf" &> /dev/null || hash "rbenv" &> /dev/null
then
_maybe_ruby_version="$(_astral_ruby_version)"
fi
if [[ -n "${_maybe_ruby_version}" ]]
then
local _version_prefix="%{$fg_bold[blue]%}ruby:"
local _version_value="%{$fg_bold[cyan]%}${_maybe_ruby_version}"
local _version_suffix="%{$fg_bold[blue]%}%{${reset_color}%} "
_ruby_version_string="${_version_prefix}${_version_value}${_version_suffix}"
fi
printf "%s\n" "${_ruby_version_string}"
}
# _astral_ruby_version()
#
# Usage:
# _astral_ruby_version
#
# Description:
# Show current Ruby version if different from global version.
#
# via: https://gist.github.com/mislav/1712320
_astral_ruby_version() {
local _asdf_current_output=
local _local_version=
local _print_version=0
if hash "rbenv" &> /dev/null &&
[[ "$(rbenv global)" != "${_local_version}" ]] ||
rbenv local > /dev/null 2>&1
then
_local_version="$(rbenv version-name)"
_print_version=1
elif hash "asdf" &> /dev/null &&
_asdf_current_output="$(asdf current ruby)"
then
local _asdf_current_version="${_asdf_current_output% *}"
_asdf_current_version="${_asdf_current_version#* }"
_asdf_current_version="${_asdf_current_version% *}"
_asdf_current_version="$(
printf "%s\\n" "${_asdf_current_version}" | awk '$1=$1'
)"
_local_version="${_asdf_current_version:-}"
_print_version=1
fi
if ((_print_version)) && [[ -n "${_local_version:-}" ]]
then
printf "%s\n" "${_local_version}"
fi
}
###############################################################################
# Lines
###############################################################################
# _astral_context_line
###############################################################################
# _astral_context_line()
#
# Usage:
# _astral_context_line
#
# Description:
# Print the context line.
_astral_context_line() {
# $_path
#
# Show the first two current path segments, with a ~ for the home
# directory.
local _path
_path="%{$fg[cyan]%}%2~"
# $_context
#
# machine:~/path
# ssh:machine:~/path
local _context
_context="$(_astral_machine):${_path}"
# $_full_line
#
# Full prompt line.
local _full_line
_full_line="${_context} $(_astral_notebook)$(_astral_ruby_prompt)$(_astral_git_prompt)"
printf "%s\n" "${_full_line}%{${reset_color}%}"
}
# _astral_prompt_line
###############################################################################
# _astral_prompt_line()
#
# Usage:
# _astral_prompt_line
#
# Description:
# Print the prompt line.
_astral_prompt_line() {
printf "%s\n" "$(_astral_command_prompt) %{${reset_color}%}"
}
# _astral_return_line
###############################################################################
# _astral_return_line()
#
# Usage:
# _astral_return_line
#
# Description:
# Print the return line.
_astral_return_line() {
# $_current_timestamp
local _current_timestamp
_current_timestamp="$(date +%s)"
# $_duration
local _duration=""
if [[ -n "${_COMMAND_START_TIME}" ]]
then
_duration="$((_current_timestamp - _COMMAND_START_TIME))"
fi
# $_duration_string
#
# Display modes:
# - 0-59 seconds: "<duration>s"
# - 60+ seconds: "<minutes>m<seconds>s (<duration>s)"
local _duration_string=""
local _minutes
local _minute_string
local _hours
local _hour_string
if [[ -n "${_duration}" ]]
then
_minutes="$((_duration / 60))"
if [[ "${_minutes}" -eq 0 ]]
then
_duration_string="${_duration}s"
else
_hours="$((_minutes / 60))"
if [[ "${_hours}" -eq 0 ]]
then
_minute_string="${_minutes}m$((_duration % 60))s"
_duration_string="${_minute_string} (${_duration}s)"
else
_hour_string="${_hours}h$((_minutes % 60))m$((_duration % 60))s"
_duration_string="${_hour_string} (${_duration}s)"
fi
fi
fi
# $_time
#
# The current time in 24-hour format.
#
# More Information:
# http://zsh.sourceforge.net/Doc/Release/Prompt-Expansion.html#Date-and-time
local _time
_time='%* %D{%F}'
# $_return_status_0_format
#
# The formatting when the previous command returns with status 0.
#
# Color chart:
# https://upload.wikimedia.org/wikipedia/commons/1/15/Xterm_256color_chart.svg
local _return_status_0_format
_return_status_0_format="%{$reset_color%}%F{239}"
# $_return_status_0_inverse_format
#
# The inverse formatting when the previous command returns with status 0.
local _return_status_0_inverse_format
_return_status_0_inverse_format="%{$reset_color%}%{$fg_no_bold[black]%}%{$bg[green]%}"
# $_return_status_0_prefix
#
# The prefix when the previous command returns with status 0.
#
# NOTE: Character options:
# - ❤︎
# - •
# NOTE: Unicode characters cause tab completion to delete previous command's
# output lines.
local _return_status_0_prefix
_return_status_0_prefix="${_return_status_0_format}•"
# $_return_status_1_format
#
# The formatting when the previous command returns with status 1.
local _return_status_1_format
_return_status_1_format="%{$reset_color%}%{$fg_no_bold[red]%}"
# $_return_status_1_inverse_format
#
# The inverse formatting when the previous command returns with status 0.
local _return_status_1_inverse_format
_return_status_1_inverse_format="%{$reset_color%}%{$fg_no_bold[black]%}%{$bg[red]%}"
# $_return_status_1_prefix
#
# The prefix when the previous command returns with status 1.
#
# NOTE: Character options:
# - ✖︎
# - x
# NOTE: Unicode characters cause tab completion to delete previous command's
# output lines.
local _return_status_1_prefix
_return_status_1_prefix="${_return_status_1_format}x"
# $_return_status_format
#
# Prompt formattings with color indicating last return status:
# green for 0 and red for non-0.
local _return_status_format
_return_status_format="%(?:${_return_status_0_format}:${_return_status_1_format})"
# $_return_status_inverse_format
#
# Prompt formattings with color indicating last return status:
# green for 0 and red for non-0.
local _return_status_inverse_format
_return_status_inverse_format="%(?:${_return_status_0_inverse_format}:${_return_status_1_inverse_format})"
# $_return_status
#
# Prefix prompt with a symbol with color indicating last return status:
# green for 0 and red for non-0.
local _return_status_prefix
_return_status_prefix="%(?:${_return_status_0_prefix}:${_return_status_1_prefix})"
# $_prefix
local _prefix
_prefix="${_return_status_prefix} ${_duration_string}"
# $_prefix_visible_length
local _prefix_visible_length
_prefix_visible_length="$(_astral_visible_length "${_prefix}")"
# $_spacer_length
local _spacer_length
_spacer_length="$((COLUMNS - _prefix_visible_length - 24))"
# $_spacer
#
# A string of characters for spacing elements.
#
# NOTE: Using `sed` rather than `tr` because `tr` mangles the middle dot on
# some systems.
local _spacer
_spacer="$(_astral_spaces "${_spacer_length}" | sed 's/ /·/g')"
# $_full_line
local _full_line
_full_line="${_prefix} ${_spacer} ${_time} •"
# NOTE: use `_full_line="${_prefix} ${_spacer} ${_time} »"` for top line.
printf "%s\n" "${_full_line}%{${reset_color}%}"
}
###############################################################################
# Prompt
###############################################################################
export ASTRAL_ZEN_MODE=0
astral() {
export _NEWLINE=$'\n'
if [[ "${1:-}" =~ '^-h|--help|help$' ]]
then
cat <<HEREDOC
Usage:
${0} zen
${0} prompt
${0} -h | --help | help
Options:
-h --help Display this usage information.
Subcommands:
zen Toggle Zen Mode.
prompt Print the formatted prompt string to assign to \$PROMPT.
Description:
A ZSH theme.
HEREDOC
return 0
elif [[ "${1:-}" =~ '^zen|on|off|show|hide|enable|disable|normal|simple$' ]]
then
if ((ASTRAL_ZEN_MODE))
then
ASTRAL_ZEN_MODE=0
printf "Zen Mode off.\n"
else
ASTRAL_ZEN_MODE=1
printf "Zen Mode on.\n"
fi
elif [[ "${1:-}" =~ '^prompt$' ]]
then
# $_top_section
#
# Full top section.
local _top_section
_top_section="$(_astral_return_line)${_NEWLINE}$(_astral_context_line)"
# $_bottom_line
#
# Full bottom prompt line.
local _bottom_line
_bottom_line="$(_astral_prompt_line)"
if ((ASTRAL_ZEN_MODE))
then
printf "%s\n" "${_bottom_line}"
else
printf "%s\n" "${_top_section}${_NEWLINE}${_bottom_line}"
fi
else
"${0}" -h
fi
}
# $PROMPT
#
# Primary prompt variable. Use $RPROMPT to put a prompt on the right side.
PROMPT=$'$(astral prompt)'