-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt_zen_setup
96 lines (80 loc) · 3.46 KB
/
prompt_zen_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
#!/usr/bin/env zsh
# Zen Zsh Prompt
#
# author: Sheikh Saad (https://github.com/cybardev)
# promptinit initialization function
prompt_zen_setup() {
# ------------------------ User Config --------------------------- #
zen_mode="split" # split, group
zen_prompt_symbol="(λ) " # prompt characters
zen_min_cmd_duration="5" # minimum duration of last command in seconds
# ----------------------- Colors --------------------------------- #
zen_color_prompt="#F7C" # pink
zen_color_err="#E77" # red
zen_color_dir="#4CF" # blue
zen_color_green="#AC8" # green
zen_color_yellow="#FB4" # yellow
# ---------------------------------------------------------------- #
# zsh options
prompt_opts=(cr percent sp)
autoload -Uz colors vcs_info
colors
# add zsh hooks for theme
add-zsh-hook preexec prompt_zen_preexec
add-zsh-hook precmd prompt_zen_precmd
# git status style
zstyle ":vcs_info:*" enable git
zstyle ":vcs_info:*" check-for-changes true
zstyle ":vcs_info:*" stagedstr "%F{$zen_color_yellow}"
zstyle ":vcs_info:*" unstagedstr "%F{$zen_color_err}"
zstyle ":vcs_info:*" formats "%c%u(%b)"
}
# function executed before each command
prompt_zen_preexec() {
timer=${timer:-$SECONDS}
}
# function executed before displaying each prompt line
prompt_zen_precmd() {
# save the duration of last command
if [[ -n $timer ]]; then
timer_show=$(($SECONDS - $timer))
[[ "$timer_show" -ge "$zen_min_cmd_duration" ]] && timer_str="$(convert_sec timer_show)" || timer_str=""
unset timer
fi
# prompt style
vcs_info # initialize git status function
zen_dir="%F{$zen_color_dir}%1~" # directory
[[ -z "$vcs_info_msg_0_" ]] && zen_gitstatus="" || zen_gitstatus="%F{$zen_color_green}$vcs_info_msg_0_ " # git status (only show in git repos)
[[ -z "$VIRTUAL_ENV" ]] && zen_venv="" || zen_venv="%F{$zen_color_err}[$(basename $VIRTUAL_ENV)] " # venv status (only show when venv active)
zen_timer="%F{$zen_color_yellow}$timer_str" # last command duration
zen_prompt_style="%(?,%F{$zen_color_prompt},%F{$zen_color_err})%(?,,%F{$zen_color_err}%? )$zen_prompt_symbol%f" # shell prompt
# mode-dependent configuration
if [[ "$zen_mode" = "group" ]]; then
# print newline before prompt, unless first prompt in process
case "$NEW_LINE_BEFORE_PROMPT" in
"") NEW_LINE_BEFORE_PROMPT=1 ;;
*) echo "" ;;
esac
PS1="$zen_dir $zen_gitstatus$zen_venv$zen_timer%f"$'\n'"$zen_prompt_style" # left prompt group
elif [[ "$zen_mode" = "split" ]]; then
PS1="$zen_prompt_style" # left prompt
RPS1="$zen_timer $zen_venv$zen_gitstatus$zen_dir%f" # right prompt
else
echo "Unknown mode: $zen_mode"
exit 1
fi
}
# convert seconds to the format: 1h 2m 3s
convert_sec() {
((h = ${1} / 3600))
((m = (${1} % 3600) / 60))
((s = ${1} % 60))
# only print if value is greater than zero
hms=""
[[ $h -gt 0 ]] && hms+="$h""h "
[[ $m -gt 0 ]] && hms+="$m""m "
[[ $s -gt 0 ]] && hms+="$s""s "
echo -n "${hms% }" # strip trailing whitespace
}
# initiallize prompt theme
prompt_zen_setup "$@"