-
Notifications
You must be signed in to change notification settings - Fork 2
/
simpalt.zsh-theme
287 lines (244 loc) · 6.74 KB
/
simpalt.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
# Based on Roby Russel's agnoster theme
# https://github.com/agnoster/agnoster-zsh-theme
# https://github.com/robbyrussell/oh-my-zsh/wiki/themes
#
# # Goals
# - Make a smaller footprint in the termial while maintaning the information
# provided by agnoster
# - Allow switching between full prompt and small prompt
# - Warn on aws-vault session being active
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
if [[ -z $SIMPALT_PROMPT_SEGMENTS ]]; then
typeset -aHg SIMPALT_PROMPT_SEGMENTS=(
prompt_aws
prompt_status
prompt_context
prompt_virtualenv
prompt_dir
prompt_git
)
fi
if [[ -z $SIMPALT_MAIN_BRANCHES ]]; then
typeset -aHg SIMPALT_MAIN_BRANCHES=(
master
main
development
)
fi
typeset -g SIMPALT_SMALL='ON'
if [[ -z "$PRIMARY_FG" ]]; then
PRIMARY_FG=black
fi
# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() {
local bg fg
[[ -n $1 ]] && bg="%K{$1}" || bg="%k"
[[ -n $2 ]] && fg="%F{$2}" || fg="%f"
if [[ $__SIMPALT_CURRENT_BG == 'NONE' ]]; then
print -n "%{$bg%}"
else
if [[ $1 != $__SIMPALT_CURRENT_BG ]]; then
if [[ -z $__SIMPALT_PENDING_FLAG ]]; then
print -n ' '
fi
print -n "%{$bg%F{$__SIMPALT_CURRENT_BG}%}"
elif [[ -n $__SIMPALT_PENDING_FLAG || -z $3 ]]; then
print -n "%{$fg%}"
fi
fi
print -n "%{$fg%}"
__SIMPALT_CURRENT_BG=$1
if [[ -n $3 ]]; then
print -n " $3"
unset __SIMPALT_PENDING_FLAG
else
__SIMPALT_PENDING_FLAG=1
fi
}
# End the prompt, closing any open segments
prompt_end() {
if [[ -z $__SIMPALT_PENDING_FLAG ]]; then
print -n ' '
fi
if [[ -n $__SIMPALT_CURRENT_BG ]]; then
print -n "%{%k%F{$__SIMPALT_CURRENT_BG}%}"
else
print -n "%{%k%}"
fi
print -n "%{%f%}"
}
### Prompt components
# Each component will draw itself, and hide itself if no information needs to be shown
# Context: user@hostname (who am I and where am I)
prompt_context() {
local context user=`whoami`
if [[ "$user" != "$DEFAULT_USER" || -n "$SSH_CONNECTION" ]]; then
[[ -n "$COMPUTER_SYMBOL" ]] && context="$COMPUTER_SYMBOL" || context="$user@%m"
prompt_segment $PRIMARY_FG default "%(!.%{%F{yellow}%}.)$context"
fi
}
# Git: branch/detached head, dirty status
prompt_git() {
local ref color git_state git_status
local is_wip() {
test -n "$(git log -n1 --format='%s' 2> /dev/null | grep -iw '^wip')"
}
ref="$(git rev-parse --abbrev-ref HEAD 2> /dev/null)"
if [ $SIMPALT_SMALL ]; then
if [ -z "$ref" ]; then
color=blue
elif prompt_simpalt_get_git_state; then
color=cyan
ref="$git_state"
elif ! $(git symbolic-ref HEAD &> /dev/null); then
color=red
ref=""
else
local branch_color=default
local behind=`git rev-list --count HEAD..@{upstream} 2> /dev/null`
if [ "$behind" ]; then
local ahead=`git rev-list --count @{upstream}..HEAD 2> /dev/null`
if (( behind > 0 )); then
if (( ahead > 0 )); then
branch_color=magenta
else
branch_color=red
fi
elif (( ahead > 0 )); then
branch_color=yellow
fi
else
branch_color=blue
fi
prompt_segment black $branch_color
__SIMPALT_PENDING_FLAG=1
if is_wip; then
ref="↺"
else
ref=""
fi
git_status="$(git status --porcelain --ignore-submodules 2> /dev/null)"
if [ $? -ne 0 ]; then
color=magenta
elif [ -n "${git_status}" ]; then
color=yellow
else
color=green
fi
fi
prompt_segment $color $PRIMARY_FG "$ref"
else
if [[ -n "$ref" ]]; then
if [ -n "$(git status --porcelain 2> /dev/null)" ]; then
color=yellow
if prompt_simpalt_get_git_state; then
ref="$ref ($git_state)"
else
ref="$ref ±"
fi
else
color=green
ref="$ref"
fi
if ! $(git symbolic-ref HEAD &> /dev/null); then
ref="➦ $(git rev-parse --short HEAD 2> /dev/null)"
elif is_wip; then
ref="↺ $ref"
else
ref=" $ref"
fi
prompt_segment $color $PRIMARY_FG "$ref"
fi
fi
}
# AWS: current aws-vault session
prompt_aws() {
if [ $AWS_VAULT ]; then
[ $SIMPALT_SMALL ] && prompt_segment black magenta "" || prompt_segment magenta $PRIMARY_FG " $AWS_VAULT"
fi
}
# Dir: current working directory
prompt_dir() {
if [ $SIMPALT_SMALL ]; then
if [[ "$PWD" == "$HOME" ]]; then
prompt_segment black default '~'
else
prompt_segment black default "$(basename $PWD)"
fi
else
prompt_segment blue $PRIMARY_FG '%~'
fi
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="%{%F{red}%}✘"
[[ $UID -eq 0 ]] && symbols+="%{%F{yellow}%}☢"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="%{%F{blue}%}⚙"
[[ -n "$symbols" ]] && prompt_segment $PRIMARY_FG default "$symbols"
}
# Display current virtual environment
prompt_virtualenv() {
if [[ -n $VIRTUAL_ENV ]]; then
prompt_segment cyan $PRIMARY_FG "$(basename $VIRTUAL_ENV)"
fi
}
prompt_simpalt_get_git_state () {
local gitdir="$(git rev-parse --absolute-git-dir)"
local tmp
for tmp in "${gitdir}/rebase-apply" \
"${gitdir}/rebase" \
"${gitdir}/../.dotest" \
"${gitdir}/rebase-merge/interactive" \
"${gitdir}/.dotest-merge/interactive" \
"${gitdir}/rebase-merge" \
"${gitdir}/.dotest-merge"; do
if [[ -d ${tmp} ]]; then
[ $SIMPALT_SMALL ] && git_state="" || git_state="rebase"
return 0
fi
done
if [[ -f "${gitdir}/MERGE_HEAD" ]]; then
[ $SIMPALT_SMALL ] && git_state="" || git_state="merge"
return 0
fi
if [[ -f "${gitdir}/BISECT_LOG" ]]; then
[ $SIMPALT_SMALL ] && git_state="" || git_state="bisect"
return 0
fi
if [[ -f "${gitdir}/CHERRY_PICK_HEAD" ]] || [[ -d "${gitdir}/sequencer" ]] ; then
[ $SIMPALT_SMALL ] && git_state="" || git_state="cherry"
return 0
fi
return 1
}
## Main prompt
prompt_simpalt_main() {
RETVAL=$?
__SIMPALT_CURRENT_BG='NONE'
for prompt_segment in "${SIMPALT_PROMPT_SEGMENTS[@]}"; do
[[ -n $prompt_segment ]] && $prompt_segment
done
prompt_end
}
prompt_simpalt_precmd() {
PROMPT='%{%f%b%k%}$(prompt_simpalt_main) '
if (( COLUMNS > 120 )); then
RPROMPT="%{%F{23}%}$(date '+%H:%M:%S')"
else
unset RPROMPT
fi
}
prompt_simpalt_setup() {
prompt_opts=(cr subst percent)
autoload -Uz add-zsh-hook
add-zsh-hook precmd prompt_simpalt_precmd
}
prompt_simpalt_setup "$@"