forked from zimfw/eriner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eriner.zsh-theme
153 lines (131 loc) · 4.08 KB
/
eriner.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
# vim:et sts=2 sw=2 ft=zsh
_prompt_eriner_main() {
# This runs in a subshell
RETVAL=${?}
BG_COLOR=''
_prompt_eriner_status
_prompt_eriner_pwd
_prompt_eriner_git
_prompt_eriner_end
}
_prompt_eriner_right() {
_prompt_eriner_exectime
}
### Segment drawing
# Utility functions to make it easy and re-usable to draw segmented prompts.
# Begin a segment. Takes two arguments, background color and contents of the
# new segment.
_prompt_eriner_segment() {
print -n "%K{${1}}"
if [[ -n ${BG_COLOR} ]] print -n "%F{${BG_COLOR}}"
print -n "${2}"
BG_COLOR=${1}
}
_prompt_eriner_standout_segment() {
print -n "%S%F{${1}}"
if [[ -n ${BG_COLOR} ]] print -n "%K{${BG_COLOR}}%k"
print -n "${2}%s"
BG_COLOR=${1}
}
_prompt_eriner_right_segment() {
if [[ -n "${2}" ]] print -n "%F{${1}}%f"
print -n "%K{${1}}${2}%k"
}
# End the prompt, closing last segment.
_prompt_eriner_end() {
print -n "%k%F{${BG_COLOR}}%f "
}
### Prompt components
# Each component will draw itself, or hide itself if no information needs to
# be shown.
# Status: Was there an error? Am I root? Are there background jobs? Ranger
# spawned shell? Python venv activated? Who and where am I (user@hostname)?
_prompt_eriner_status() {
local segment=''
if (( RETVAL )) segment+=' %F{red}✘'
if (( EUID == 0 )) segment+=' %F{yellow}⚡'
if (( $(jobs -l | wc -l) )) segment+=' %F{cyan}⚙'
if (( RANGER_LEVEL )) segment+=' %F{cyan}r'
if [[ -n ${VIRTUAL_ENV} ]] segment+=" %F{cyan}${VIRTUAL_ENV:t}"
if [[ -n ${SSH_TTY} || ${EUID} == 0 ]] segment+=" %F{%(!.yellow.default)}%n@%m"
if [[ -n ${segment} ]]; then
_prompt_eriner_segment ${STATUS_COLOR} "${segment} "
fi
}
# Pwd: current working directory.
_prompt_eriner_pwd() {
local current_dir=${(%):-%~}
if [[ ${current_dir} != '~' ]]; then
current_dir="${${(@j:/:M)${(@s:/:)current_dir:h}#?}%/}/${current_dir:t}"
fi
_prompt_eriner_standout_segment ${PWD_COLOR} " ${current_dir} "
}
# Git: branch/detached head, dirty status.
_prompt_eriner_git() {
if [[ -n ${git_info} ]]; then
local git_color
local git_dirty=${(e)git_info[dirty]}
if [[ -n ${git_dirty} ]]; then
git_color=${DIRTY_COLOR}
else
git_color=${CLEAN_COLOR}
fi
_prompt_eriner_standout_segment ${git_color} " ${(e)git_info[prompt]}${git_dirty} "
fi
}
# Time formatting utilities
# Most of the code comes from zimfw/asciiship
zmodload zsh/datetime
_prompt_eriner_exectime_preexec() {
prompt_eriner_preexec_s=${EPOCHSECONDS}
}
_prompt_eriner_exectime_precmd() {
if (( prompt_eriner_preexec_s )); then
local -ri elapsed_s=$(( EPOCHSECONDS - prompt_eriner_preexec_s ))
local -ri s=$(( elapsed_s%60))
local -ri m=$(( (elapsed_s/60)%60 ))
local -ri h=$(( elapsed_s/3600 ))
unset prompt_eriner_preexec_s
local elapsed_time
if (( h > 0 )); then
elapsed_time=${h}h${m}m
elif (( m > 0 )); then
elapsed_time=${m}m${s}s
elif (( s > 2 )); then
elapsed_time=${s}s
else
# Don't show elapsed time
unset prompt_eriner_elapsed_time
return
fi
prompt_eriner_elapsed_time=" %B%F{white}took %B%F{yellow}${elapsed_time}%b%f "
else
# Clear previous when hitting ENTER with no command to execute
unset prompt_eriner_elapsed_time
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook preexec _prompt_eriner_exectime_preexec
add-zsh-hook precmd _prompt_eriner_exectime_precmd
_prompt_eriner_exectime() {
_prompt_eriner_right_segment ${EXECTIME_COLOR} "${prompt_eriner_elapsed_time}"
}
: ${STATUS_COLOR=black}
: ${PWD_COLOR=cyan}
: ${CLEAN_COLOR=green}
: ${DIRTY_COLOR=yellow}
: ${EXECTIME_COLOR=black}
VIRTUAL_ENV_DISABLE_PROMPT=1
setopt nopromptbang prompt{cr,percent,sp,subst}
if (( ${+functions[git-info]} )); then
zstyle ':zim:git-info:branch' format ' %b'
zstyle ':zim:git-info:commit' format '➦ %c'
zstyle ':zim:git-info:action' format ' (%s)'
zstyle ':zim:git-info:dirty' format ' ±'
zstyle ':zim:git-info:keys' format \
'prompt' '%b%c%s' \
'dirty' '%D'
autoload -Uz add-zsh-hook && add-zsh-hook precmd git-info
fi
PS1='$(_prompt_eriner_main)'
RPS1='$(_prompt_eriner_right)'