forked from speedenator/agnoster-bash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agnoster.bash
executable file
·500 lines (436 loc) · 14.6 KB
/
agnoster.bash
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
#!/usr/bin/env bash
# vim: ft=bash ts=2 sw=2 sts=2
#
# agnoster's Theme - https://gist.github.com/3712874
# A Powerline-inspired theme for BASH
#
# (Converted from ZSH theme by Kenny Root)
# https://gist.github.com/kruton/8345450
#
# Updated & fixed by Erik Selberg [email protected] 1/14/17
# Tested on MacOSX, Ubuntu, Amazon Linux
# Bash v3 and v4
#
# # README
#
# In order for this theme to render correctly, you will need a
# [Powerline-patched font](https://gist.github.com/1595572).
# I recommend: https://github.com/powerline/fonts.git
# > git clone https://github.com/powerline/fonts.git fonts
# > cd fonts
# > install.sh
# In addition, I recommend the
# [Solarized theme](https://github.com/altercation/solarized/) and, if you're
# using it on Mac OS X, [iTerm 2](http://www.iterm2.com/) over Terminal.app -
# it has significantly better color fidelity.
# Install:
# I recommend the following:
# $ cd home
# $ mkdir -p .bash/themes/agnoster-bash
# $ git clone https://github.com/speedenator/agnoster-bash.git .bash/themes/agnoster-bash
# then add the following to your .bashrc:
# export THEME=$HOME/.bash/themes/agnoster-bash/agnoster.bash
# if [[ -f $THEME ]]; then
# export DEFAULT_USER=`whoami`
# source $THEME
# fi
#
# # Goals
#
# The aim of this theme is to only show you *relevant* information. Like most
# prompts, it will only show git information when in a git working directory.
# However, it goes a step further: everything from the current user and
# hostname to whether the last call exited with an error to whether background
# jobs are running in this shell will all be displayed automatically when
# appropriate.
# Generally speaking, this script has limited support for right
# prompts (ala powerlevel9k on zsh), but it's pretty problematic in Bash.
# The general pattern is to write out the right prompt, hit \r, then
# write the left. This is problematic for the following reasons:
# - Doesn't properly resize dynamically when you resize the terminal
# - Changes to the prompt (like clearing and re-typing, super common) deletes the prompt
# - Getting the right alignment via columns / tput cols is pretty problematic (and is a bug in this version)
# - Bash prompt escapes (like \h or \w) don't get interpolated
#
# all in all, if you really, really want right-side prompts without a
# ton of work, recommend going to zsh for now. If you know how to fix this,
# would appreciate it!
# note: requires bash v4+... Mac users - you often have bash3.
# 'brew install bash' will set you free
PROMPT_DIRTRIM=2 # bash4 and above
######################################################################
DEBUG=0
debug() {
if [[ ${DEBUG} -ne 0 ]]; then
>&2 echo -e $*
fi
}
######################################################################
### Segment drawing
# A few utility functions to make it easy and re-usable to draw segmented prompts
CURRENT_BG='NONE'
CURRENT_RBG='NONE'
SEGMENT_SEPARATOR=''
RIGHT_SEPARATOR=''
LEFT_SUBSEG=''
RIGHT_SUBSEG=''
text_effect() {
case "$1" in
reset) echo 0;;
bold) echo 1;;
underline) echo 4;;
esac
}
# to add colors, see
# http://bitmote.com/index.php?post/2012/11/19/Using-ANSI-Color-Codes-to-Colorize-Your-Bash-Prompt-on-Linux
# under the "256 (8-bit) Colors" section, and follow the example for orange below
fg_color() {
case "$1" in
black) echo 30;;
red) echo 31;;
green) echo 32;;
yellow) echo 33;;
blue) echo 34;;
magenta) echo 35;;
cyan) echo 36;;
white) echo 37;;
orange) echo 38\;5\;166;;
esac
}
bg_color() {
case "$1" in
black) echo 40;;
red) echo 41;;
green) echo 42;;
yellow) echo 43;;
blue) echo 44;;
magenta) echo 45;;
cyan) echo 46;;
white) echo 47;;
orange) echo 48\;5\;166;;
esac;
}
# TIL: declare is global not local, so best use a different name
# for codes (mycodes) as otherwise it'll clobber the original.
# this changes from BASH v3 to BASH v4.
ansi() {
local seq
declare -a mycodes=("${!1}")
debug "ansi: ${!1} all: $* aka ${mycodes[@]}"
seq=""
for ((i = 0; i < ${#mycodes[@]}; i++)); do
if [[ -n $seq ]]; then
seq="${seq};"
fi
seq="${seq}${mycodes[$i]}"
done
debug "ansi debug:" '\\[\\033['${seq}'m\\]'
echo -ne '\[\033['${seq}'m\]'
# PR="$PR\[\033[${seq}m\]"
}
ansi_single() {
echo -ne '\[\033['$1'm\]'
}
# Begin a segment
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_segment() {
local bg fg
declare -a codes
debug "Prompting $1 $2 $3"
# if commented out from kruton's original... I'm not clear
# if it did anything, but it messed up things like
# prompt_status - Erik 1/14/17
# if [[ -z $1 || ( -z $2 && $2 != default ) ]]; then
codes=("${codes[@]}" $(text_effect reset))
# fi
if [[ -n $1 ]]; then
bg=$(bg_color $1)
codes=("${codes[@]}" $bg)
debug "Added $bg as background to codes"
fi
if [[ -n $2 ]]; then
fg=$(fg_color $2)
codes=("${codes[@]}" $fg)
debug "Added $fg as foreground to codes"
fi
debug "Codes: "
# declare -p codes
if [[ $CURRENT_BG != NONE && $1 != $CURRENT_BG ]]; then
declare -a intermediate=($(fg_color $CURRENT_BG) $(bg_color $1))
debug "pre prompt " $(ansi intermediate[@])
PR="$PR $(ansi intermediate[@])$SEGMENT_SEPARATOR"
debug "post prompt " $(ansi codes[@])
PR="$PR$(ansi codes[@]) "
else
debug "no current BG, codes is $codes[@]"
PR="$PR$(ansi codes[@]) "
fi
CURRENT_BG=$1
[[ -n $3 ]] && PR="$PR$3"
}
# Add last runtime to prompt https://stackoverflow.com/a/31694983/6699440
function timer_now {
date +%s%N
}
function timer_start {
timer_start=${timer_start:-$(timer_now)}
}
function timer_stop {
local delta_us=$((($(timer_now) - $timer_start) / 1000))
local us=$((delta_us % 1000))
local ms=$(((delta_us / 1000) % 1000))
local s=$(((delta_us / 1000000) % 60))
local m=$(((delta_us / 60000000) % 60))
local h=$((delta_us / 3600000000))
# Goal: always show around 3 digits of accuracy
if ((h > 0)); then timer_show=${h}h${m}m
elif ((m > 0)); then timer_show=${m}m${s}s
elif ((s >= 10)); then timer_show=${s}.$((ms / 100))s
elif ((s > 0)); then timer_show=${s}.$(printf %03d $ms)s
elif ((ms >= 100)); then timer_show=${ms}ms
elif ((ms > 0)); then timer_show=${ms}.$((us / 100))ms
else timer_show=${us}us
fi
unset timer_start
}
trap 'timer_start' DEBUG
# End the prompt, closing any open segments
prompt_end() {
if [[ -n $CURRENT_BG ]]; then
declare -a codes=($(text_effect reset) $(fg_color $CURRENT_BG))
mem=`free --mega | awk 'NF>6{printf (" :%2.2fGB/%2.2fGB\n", ( $3 / 1024), ($2 / 1024))}'`
PR="\[\033[0;34m\]╭─\[\033[0;34m\]$RIGHT_SEPARATOR$PR $(ansi codes[@])$SEGMENT_SEPARATOR \d \t $mem :${timer_show} \n\[\033[0;34m\]╰─$LEFT_SUBSEG \[\033[1;36m\]\$\[\033[0m\]"
fi
declare -a reset=($(text_effect reset))
PR="$PR $(ansi reset[@])"
CURRENT_BG=''
}
### virtualenv prompt
prompt_virtualenv() {
if [[ -n $VIRTUAL_ENV ]]; then
color=cyan
prompt_segment $color $PRIMARY_FG
prompt_segment $color white "$(basename $VIRTUAL_ENV)"
fi
}
### 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 user=`whoami`
if [[ $user != $DEFAULT_USER || -n $SSH_CLIENT ]]; then
prompt_segment black default "$user@\h"
fi
}
# prints history followed by HH:MM, useful for remembering what
# we did previously
prompt_histdt() {
prompt_segment black default "\! [\A]"
}
git_status_dirty() {
dirty=$(git status -s 2> /dev/null | tail -n 1)
[[ -n $dirty ]] && echo " ●"
}
git_stash_dirty() {
stash=$(git stash list 2> /dev/null | tail -n 1)
[[ -n $stash ]] && echo " ⚑"
}
# Git: branch/detached head, dirty status
prompt_git() {
local ref dirty
if $(git rev-parse --is-inside-work-tree >/dev/null 2>&1); then
ZSH_THEME_GIT_PROMPT_DIRTY='±'
dirty=$(git_status_dirty)
stash=$(git_stash_dirty)
ref=$(git symbolic-ref HEAD 2> /dev/null) \
|| ref="➦ $(git describe --exact-match --tags HEAD 2> /dev/null)" \
|| ref="➦ $(git show-ref --head -s --abbrev | head -n1 2> /dev/null)"
if [[ -n $dirty ]]; then
prompt_segment yellow black
else
prompt_segment green black
fi
PR="$PR${ref/refs\/heads\// }$stash$dirty"
fi
}
# Mercurial: clean, modified and uncomitted files
prompt_hg() {
local rev st branch
if $(hg id >/dev/null 2>&1); then
if $(hg prompt >/dev/null 2>&1); then
if [[ $(hg prompt "{status|unknown}") = "?" ]]; then
# if files are not added
prompt_segment red white
st='±'
elif [[ -n $(hg prompt "{status|modified}") ]]; then
# if any modification
prompt_segment yellow black
st='±'
else
# if working copy is clean
prompt_segment green black $CURRENT_FG
fi
PR="$PR$(hg prompt "☿ {rev}@{branch}") $st"
else
st=""
rev=$(hg id -n 2>/dev/null | sed 's/[^-0-9]//g')
branch=$(hg id -b 2>/dev/null)
if `hg st | grep -q "^\?"`; then
prompt_segment red white
st='±'
elif `hg st | grep -q "^[MA]"`; then
prompt_segment yellow black
st='±'
else
prompt_segment green black $CURRENT_FG
fi
PR="$PR☿ $rev@$branch $st"
fi
fi
}
# Dir: current working directory
prompt_dir() {
prompt_segment blue black '\w'
}
# Status:
# - was there an error
# - am I root
# - are there background jobs?
prompt_status() {
local symbols
symbols=()
[[ $RETVAL -ne 0 ]] && symbols+="$(ansi_single $(fg_color red))✘"
[[ $UID -eq 0 ]] && symbols+="$(ansi_single $(fg_color yellow))⚡"
[[ $(jobs -l | wc -l) -gt 0 ]] && symbols+="$(ansi_single $(fg_color cyan))⚙"
[[ -n "$symbols" ]] && prompt_segment black default "$symbols"
}
######################################################################
#
# experimental right prompt stuff
# requires setting prompt_foo to use PRIGHT vs PR
# doesn't quite work per above
rightprompt() {
printf "%*s" $COLUMNS "$PRIGHT"
}
# quick right prompt I grabbed to test things.
__command_rprompt() {
local times= n=$COLUMNS tz
for tz in ZRH:Europe/Zurich PIT:US/Eastern \
MTV:US/Pacific TOK:Asia/Tokyo; do
[ $n -gt 40 ] || break
times="$times ${tz%%:*}\e[30;1m:\e[0;36;1m"
times="$times$(TZ=${tz#*:} date +%H:%M)\e[0m"
n=$(( $n - 10 ))
done
[ -z "$times" ] || printf "%${n}s$times\\r" ''
}
# PROMPT_COMMAND=__command_rprompt
# this doens't wrap code in \[ \]
ansi_r() {
local seq
declare -a mycodes2=("${!1}")
debug "ansi: ${!1} all: $* aka ${mycodes2[@]}"
seq=""
for ((i = 0; i < ${#mycodes2[@]}; i++)); do
if [[ -n $seq ]]; then
seq="${seq};"
fi
seq="${seq}${mycodes2[$i]}"
done
debug "ansi debug:" '\\[\\033['${seq}'m\\]'
echo -ne '\033['${seq}'m'
# PR="$PR\[\033[${seq}m\]"
}
# Begin a segment on the right
# Takes two arguments, background and foreground. Both can be omitted,
# rendering default background/foreground.
prompt_right_segment() {
local bg fg
declare -a codes
debug "Prompt right"
debug "Prompting $1 $2 $3"
# if commented out from kruton's original... I'm not clear
# if it did anything, but it messed up things like
# prompt_status - Erik 1/14/17
# if [[ -z $1 || ( -z $2 && $2 != default ) ]]; then
codes=("${codes[@]}" $(text_effect reset))
# fi
if [[ -n $1 ]]; then
bg=$(bg_color $1)
codes=("${codes[@]}" $bg)
debug "Added $bg as background to codes"
fi
if [[ -n $2 ]]; then
fg=$(fg_color $2)
codes=("${codes[@]}" $fg)
debug "Added $fg as foreground to codes"
fi
debug "Right Codes: "
# declare -p codes
# right always has a separator
# if [[ $CURRENT_RBG != NONE && $1 != $CURRENT_RBG ]]; then
# $CURRENT_RBG=
# fi
declare -a intermediate2=($(fg_color $1) $(bg_color $CURRENT_RBG) )
# PRIGHT="$PRIGHT---"
debug "pre prompt " $(ansi_r intermediate2[@])
PRIGHT="$PRIGHT$(ansi_r intermediate2[@])$RIGHT_SEPARATOR"
debug "post prompt " $(ansi_r codes[@])
PRIGHT="$PRIGHT$(ansi_r codes[@]) "
# else
# debug "no current BG, codes is $codes[@]"
# PRIGHT="$PRIGHT$(ansi codes[@]) "
# fi
CURRENT_RBG=$1
[[ -n $3 ]] && PRIGHT="$PRIGHT$3"
}
######################################################################
## Emacs prompt --- for dir tracking
# stick the following in your .emacs if you use this:
# (setq dirtrack-list '(".*DIR *\\([^ ]*\\) DIR" 1 nil))
# (defun dirtrack-filter-out-pwd-prompt (string)
# "dirtrack-mode doesn't remove the PWD match from the prompt. This does."
# ;; TODO: support dirtrack-mode's multiline regexp.
# (if (and (stringp string) (string-match (first dirtrack-list) string))
# (replace-match "" t t string 0)
# string))
# (add-hook 'shell-mode-hook
# #'(lambda ()
# (dirtrack-mode 1)
# (add-hook 'comint-preoutput-filter-functions
# 'dirtrack-filter-out-pwd-prompt t t)))
prompt_emacsdir() {
# no color or other setting... this will be deleted per above
PR="DIR \w DIR$PR"
}
######################################################################
## Main prompt
build_prompt() {
[[ ! -z ${AG_EMACS_DIR+x} ]] && prompt_emacsdir
prompt_status
#[[ -z ${AG_NO_HIST+x} ]] && prompt_histdt
[[ -z ${AG_NO_CONTEXT+x} ]] && prompt_context
prompt_virtualenv
prompt_dir
prompt_git
prompt_hg
prompt_end
}
# from orig...
# export PS1='$(ansi_single $(text_effect reset)) $(build_prompt) '
# this doesn't work... new model: create a prompt via a PR variable and
# use that.
set_bash_prompt() {
timer_stop
RETVAL=$?
PR=""
PRIGHT=""
CURRENT_BG=NONE
PR="$(ansi_single $(text_effect reset))"
build_prompt
# uncomment below to use right prompt
# PS1='\[$(tput sc; printf "%*s" $COLUMNS "$PRIGHT"; tput rc)\]'$PR
PS1=$PR
}
PROMPT_COMMAND=set_bash_prompt