Skip to content

Instantly share code, notes, and snippets.

@legionus
Last active October 2, 2017 20:42
Show Gist options
  • Save legionus/48fe922c1443a796de8c65dd832b9792 to your computer and use it in GitHub Desktop.
Save legionus/48fe922c1443a796de8c65dd832b9792 to your computer and use it in GitHub Desktop.
#!/bin/sh
gettimeofday() {
date '+%s%N'
}
bash_timing() {
local LANG=C rc=$?
if [ "$BASH_COMMAND" == "$PROMPT_COMMAND" ]; then
[ -n "${__BASH_TIMING__-}" ] ||
return 0
local us=$(( ($(gettimeofday) / 1000) - $__BASH_TIMING__ ))
if [ $us -gt 500000 ] || [ "$rc" != 0 ]; then
local pair i
for pair in 3600000000:h 60000000:m 1000000:s 1000:ms 0:us; do
i="${pair%:*}"
[ $us -gt $i ] ||
continue
printf '\033[1;31m%s\033[0m :: \033[1;33m' "$rc"
[ $x != 0 ] &&
printf "%g" "$(($us / $i)).$(($us / ($i / 100) % 100))" ||
printf '%d' "$us"
printf " %s\033[0m\n" "${pair#*:}"
break
done
fi >&2
__BASH_TIMING__=
elif [ -z "${__BASH_TIMING__-}" ]; then
__BASH_TIMING__=$(( $(gettimeofday) / 1000 ))
fi
}
PROMPT_COMMAND="${PROMPT_COMMAND:-:}"
trap 'bash_timing' DEBUG
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment