Skip to content

Instantly share code, notes, and snippets.

@lauriro
Last active December 23, 2015 18:19
Show Gist options
  • Save lauriro/6675170 to your computer and use it in GitHub Desktop.
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.
# 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