Skip to content

Instantly share code, notes, and snippets.

@shinh
Created September 11, 2020 10:30
Show Gist options
  • Save shinh/d0d0c1333b593bc47648f77075c483db to your computer and use it in GitHub Desktop.
Save shinh/d0d0c1333b593bc47648f77075c483db to your computer and use it in GitHub Desktop.
show_time() {
local e=$1
local o="$(printf "%02d" $(($e % 60)))s"
e=$(($e / 60))
if (( $e >= 0 )); then
o="$(printf "%02d" $(($e % 60)))m${o}"
e=$(($e / 60))
if (( $e > 0 )); then
o="$(($e % 24))h${o}"
e=$(($e / 24))
if (( $e > 0 )); then
o="${e}d${o}"
fi
fi
fi
echo $o
}
show_time_start() {
export CMD_START_TIME=${SECONDS}
}
show_time_end() {
if [ -z ${CMD_START_TIME} ]; then
return
fi
local elapsed=$((${SECONDS} - ${CMD_START_TIME}))
export CMD_START_TIME=
if (( $elapsed > 59 )); then
show_time $elapsed
fi
}
preexec_functions=($preexec_functions show_time_start)
precmd_functions=($precmd_functions show_time_end)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment