Skip to content

Instantly share code, notes, and snippets.

@lillesvin
Created August 3, 2016 07:22
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lillesvin/20edc4446817875787a415c3e345de6c to your computer and use it in GitHub Desktop.
Save lillesvin/20edc4446817875787a415c3e345de6c to your computer and use it in GitHub Desktop.
Command line split timer that optionally uses a file as a pipe. You can have OBS read the contents of that file to display your timer in a video/stream if you don't want to just capture part of the terminal window. You can "split" by hitting Return/Enter (in the terminal) while the timer is running, and stop the timer with Ctrl-C (in the termina…
#!/bin/bash
if [[ "${1}x" == "x" ]]; then
USE_PIPE=false
else
USE_PIPE=true
PIPE=${1}
fi
FORMAT="%s.%2N"
STARTTIME=$(date +${FORMAT})
function show_info {
if [[ $USE_PIPE == true ]]; then
echo "Timer file: ${PIPE}"
fi
echo "End timing with ctrl-c."
}
# bc formats 0.nn as .nn which sucks
function format_duration {
printf "%.2f" $1
}
function tick_time {
DUR=$(format_duration $(bc <<< $(date +${FORMAT})-$STARTTIME))
PRETTY_DUR=$(date +%T.%2N -u -d "@${DUR}")
printf "\r%s " ${PRETTY_DUR}
if [[ $USE_PIPE == true ]]; then
echo ${PRETTY_DUR} > ${PIPE}
fi
}
function finish_up {
tick_time
echo
}
trap finish_up exit
show_info
while true; do
tick_time
if [[ $USE_PIPE == true ]]; then
sleep .1
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment