Last active
December 23, 2015 18:19
-
-
Save lauriro/6675170 to your computer and use it in GitHub Desktop.
Show command execution time in shell when it takes more than five seconds.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add to .bashrc or .zshrc and try it out | |
# $ sleep 5 | |
# -- Runs 5 seconds | |
time_to_words() { | |
local mod num="$1" out="" | |
set -- 60 second 60 minute 24 hour 7 day 4 week 12 month | |
while test $num -gt 0; do | |
mod=`expr $num % $1` | |
num=`expr $num / $1` | |
test $mod -gt 0 && out="$mod $2`test $mod -gt 1 && echo s` $out" | |
shift 2 | |
done | |
echo $out | |
} | |
precmd() { | |
CUR=`date +%s` | |
RUN=`expr $CUR - ${PS_TIME-$CUR}` | |
test $RUN -gt 4 && echo "-- Runs `time_to_words $RUN`" >&2 | |
# Capture time | |
trap 'PS_TIME=`date +%s`; trap DEBUG' DEBUG | |
} | |
PROMPT_COMMAND=precmd # for bash, zsh uses precmd directly |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment