Skip to content

Instantly share code, notes, and snippets.

@lillesvin lillesvin/shplit.sh
Created Aug 3, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.