Skip to content

Instantly share code, notes, and snippets.

@matti
Created September 26, 2017 17:42
Show Gist options
  • Save matti/4cbe78e377f658a1eb5256af8606a965 to your computer and use it in GitHub Desktop.
Save matti/4cbe78e377f658a1eb5256af8606a965 to your computer and use it in GitHub Desktop.
### Display execution time if command took longer than REPORTTIME_TOTAL
#
# https://superuser.com/a/847411/376927
#
REPORTTIME_TOTAL=1 # seconds
cmd_execution_time() {
local stop=$((`date "+%s"`))
let local "cmd_elapsed = ${stop} - ${cmd_start_time}"
let local "cmd_minutes = ${cmd_elapsed} / 60"
let local "cmd_seconds = ${cmd_elapsed} % 60"
(( $cmd_elapsed > $REPORTTIME_TOTAL )) && print -P "%F{yellow}${cmd_minutes} min ${cmd_seconds} sec%f"
}
preexec() {
cmd_start_time=$((`date "+%s"`))
}
precmd() {
if (($+cmd_start_time)); then
cmd_execution_time
fi
}
###
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment