Last active
September 15, 2023 07:25
-
-
Save konsolebox/4e0a361935ab62cbc470160c74434848 to your computer and use it in GitHub Desktop.
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
#!/bin/bash | |
function stopwatch { | |
local start=${EPOCHREALTIME} diff_i diff_f stop_at= IFS=. | |
trap 'stop_at=${EPOCHREALTIME}' INT | |
printf '\r\e[K%s.%06d' 0 0 | |
function stopwatch_get_diff { | |
set -- $1 $2 | |
diff_i=$(( 10#$3 - 10#$1 )) | |
diff_f=$(( 10#$4 - 10#$2 )) | |
(( diff_f < 0 )) && (( --diff_i, diff_f = 10#"1${4}" - 10#$2 )) | |
} | |
if enable sleep &>/dev/null; then | |
function stopwatch_sleep_10ms { builtin sleep 0.01; } | |
else | |
function stopwatch_sleep_10ms { read -t0.01 -N1; } | |
fi | |
until | |
stopwatch_sleep_10ms | |
[[ ${stop_at} ]] | |
do | |
stopwatch_get_diff "${start}" "${EPOCHREALTIME}" | |
printf '\r\e[K%s.%06d' "${diff_i}" "${diff_f}" | |
done | |
stopwatch_get_diff "${start}" "${stop_at}" | |
printf '\r\e[K%s.%06d\n' "${diff_i}" "${diff_f}" | |
unset -f stopwatch_get_diff stopwatch_sleep_10ms | |
} | |
# export -f stopwatch | |
# gnome-terminal -e '/bin/bash -c stopwatch' | |
stopwatch |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment