#!/bin/bash
#
# gromo - To count your daily gromodoros :thumbsup
#
# Dependencies: curl and mpv for the 'ding' sound; slock to force you to rest :)
################################################################################
DEFAULT_DING=https://gramos.me/ding.opus
GROMO_SECONDS=$((60 * 20))
STOP_SECONDS=20
DATA_DIR=${XDG_DATA_HOME:-$HOME/.local/share}/gromo
CACHE_DIR=${XDG_CACHE_HOME:-$HOME/.cache}/gromo
STATE_DIR=${XDG_STATE_HOME:-$HOME/.local/state}/gromo
TODAY_FILE=$DATA_DIR/$(date -I)
DING_FILE=$CACHE_DIR/ding.opus
STATE_FILE=$STATE_DIR/state
gromos=0
[ -f "$TODAY_FILE" ] && read -r gromos < "$TODAY_FILE"
if [ "$1" = "--xmobar-status" ]; then
if [ -f "$STATE_FILE" ]; then
echo "$(cat $STATE_FILE) ($gromos)"
else
echo "idle ($gromos)"
fi
exit 0
elif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
cmd=$(basename $0)
echo -e "Usage:
$cmd --xmobar-status\tReturn state formatted for display in xmobar
$cmd --help | -h \tShow this help
$cmd [task] \tStart counting (default task: "work")
"
exit 0
elif [ "$#" -ge 1 ]; then
task=$1
else
task=work
fi
mkdir -p "$DATA_DIR" "$STATE_DIR"
[ -f "$DING_FILE" ] || curl $DEFAULT_DING --create-dirs -so "$DING_FILE"
if [ -f $STATE_FILE ]; then
echo "Another instance is currently running; exiting (remove $STATE_FILE to override)"
exit 1
fi
echo idle > "$STATE_FILE"
trap "rm -f $STATE_FILE; exit" INT TERM EXIT
while true; do
read -rn1 -p "Press any key to start gromo > "
echo -ne "\r\033[K"
echo $task > "$STATE_FILE"
sleep $GROMO_SECONDS
echo idle > "$STATE_FILE"
gromos=$((gromos+1))
seconds=$((gromos * GROMO_SECONDS))
echo "$gromos" > "$TODAY_FILE"
(sleep $STOP_SECONDS && mpv --no-terminal "$DING_FILE") &
slock
if [ $seconds -lt 60 ]; then
gromo_time="$seconds seconds"
elif [ $seconds -lt 3600 ]; then
gromo_time="$((seconds / 60)) minutes"
else
gromo_time="$((seconds / 3600)) hours"
fi
echo -ne "Completed $gromos gromos today ($gromo_time). "
done