forked from popstas/zsh-command-time
-
Notifications
You must be signed in to change notification settings - Fork 0
/
command-time.plugin.zsh
40 lines (36 loc) · 1.14 KB
/
command-time.plugin.zsh
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
_command_time_preexec() {
# check excluded
if [ -n "$ZSH_COMMAND_TIME_EXCLUDE" ]; then
cmd="$1"
for exc ($ZSH_COMMAND_TIME_EXCLUDE) do;
if [ "$(echo $cmd | grep -c "$exc")" -gt 0 ]; then
# echo "command excluded: $exc"
return
fi
done
fi
timer=${timer:-$SECONDS}
ZSH_COMMAND_TIME_MSG=${ZSH_COMMAND_TIME_MSG-"Time: %s"}
ZSH_COMMAND_TIME_COLOR=${ZSH_COMMAND_TIME_COLOR-"white"}
export ZSH_COMMAND_TIME=""
}
_command_time_precmd() {
if [ $timer ]; then
timer_show=$(($SECONDS - $timer))
if [ -n "$TTY" ] && [ $timer_show -ge ${ZSH_COMMAND_TIME_MIN_SECONDS:-3} ]; then
export ZSH_COMMAND_TIME="$timer_show"
if [ ! -z ${ZSH_COMMAND_TIME_MSG} ]; then
zsh_command_time
fi
fi
unset timer
fi
}
zsh_command_time() {
if [ -n "$ZSH_COMMAND_TIME" ]; then
timer_show=$(printf '%dh:%02dm:%02ds\n' $(($ZSH_COMMAND_TIME/3600)) $(($ZSH_COMMAND_TIME%3600/60)) $(($ZSH_COMMAND_TIME%60)))
print -P "%F{$ZSH_COMMAND_TIME_COLOR}$(printf "${ZSH_COMMAND_TIME_MSG}\n" "$timer_show")%f"
fi
}
precmd_functions+=(_command_time_precmd)
preexec_functions+=(_command_time_preexec)