-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzsh_prompt
executable file
·58 lines (51 loc) · 1.84 KB
/
zsh_prompt
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
# Display seconds in human readable fromat
# Based on http://stackoverflow.com/a/32164707/3859566
# USAGE:
# _displaytime <seconds>
_displaytime() {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
[[ $D > 0 ]] && printf '%dd %dh' $D $H
[[ $H > 0 ]] && [[ $D == 0 ]] && printf '%dh %dm' $H $M
[[ $M > 0 ]] && [[ $H == 0 ]] && printf '%dm %ds' $M $S
[[ $M == 0 ]] && printf '%ds' $1
}
function prompt {
# Retirve exit code of last command to use in exit_code
# Must be captured before any other command in prompt is executed
RETVAL=$?
# Option EXTENDED_GLOB is set locally to force filename generation on
# argument to conditions, i.e. allow usage of explicit glob qualifier (#q).
# See the description of filename generation in
# http://zsh.sourceforge.net/Doc/Release/Conditional-Expressions.html
setopt EXTENDED_GLOB LOCAL_OPTIONS
[[ $RETVAL != 0 ]] && echo -n "%{$fg_bold[red]%}E($RETVAL)%{$reset_color%} "
if [[ $EXEC_TIME_duration -ge 2 ]]; then
echo -n "%{$fg_bold[magenta]%}$(_displaytime $EXEC_TIME_duration)%{$reset_color%} "
fi
if [[ $LOGNAME != $USER ]] || [[ $UID == 0 ]] || [[ -n $SSH_CONNECTION ]]; then
echo -n "%{$fg[magenta]%}%n%{$reset_color%} at %{$fg[yellow]%}%m%{$reset_color%} "
fi
echo -n "%{$fg_bold[blue]%}λ%{$reset_color%} "
}
PROMPT="$(prompt)"
export PROMPT
# Execution time start
exec_time_preexec_hook() {
EXEC_TIME_start=$(date +%s)
}
# Execution time end
exec_time_precmd_hook() {
[[ -n $EXEC_TIME_duration ]] && unset EXEC_TIME_duration
[[ -z $EXEC_TIME_start ]] && return
local EXEC_TIME_stop=$(date +%s)
EXEC_TIME_duration=$(($EXEC_TIME_stop - $EXEC_TIME_start))
unset EXEC_TIME_start
}
autoload -Uz add-zsh-hook
# Add exec_time hooks
add-zsh-hook preexec exec_time_preexec_hook
add-zsh-hook precmd exec_time_precmd_hook