Skip to content

Instantly share code, notes, and snippets.

@leo-pfeiffer
Last active February 23, 2024 00:50
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save leo-pfeiffer/3e19a8c2c4f6f91f9b17b9f6bc8e0328 to your computer and use it in GitHub Desktop.
Save leo-pfeiffer/3e19a8c2c4f6f91f9b17b9f6bc8e0328 to your computer and use it in GitHub Desktop.
Pomodoro timer for the (Mac OS) terminal
#!/bin/bash
set -eu
# sourceable pomodoro command
pomodoro() {
# print help
echo_help() {
# Display help
echo "Pomodoro timer for the terminal."
echo
echo "Syntax: pomodoro [ OPTIONS ]"
echo "options:"
echo " -s or --short-break [INTEGER]"
echo " length of short breaks in seconds"
echo " -l or --long-break [INTEGER]"
echo " length of long breaks in seconds"
echo " -w or --work [INTEGER]"
echo " length of work interval in seconds"
echo " -q or --quiet"
echo " do not show notifications"
echo " -h or --help"
echo " print help and exit"
echo
}
# Default arguments
WORK=1500 # 25 mins
SHORT_BREAK=300 # 5 mins
LONG_BREAK=1800 # 30 mins
QUIET=false
# Parse arguments
for i in "$@"; do
case $i in
-h|--help)
echo_help
return;
;;
-s|--short-break)
if [[ $2 =~ ^-?[0-9]+$ ]]
then
SHORT_BREAK=$2
shift # past argument
shift # past value
else
echo_help
return;
fi
;;
-l|--long-break)
if [[ $2 =~ ^-?[0-9]+$ ]]
then
LONG_BREAK=$2
shift # past argument
shift # past value
else
echo_help
return;
fi
;;
-w|--work)
if [[ $2 =~ ^-?[0-9]+$ ]]
then
WORK="$2"
shift # past argument
shift # past value
else
echo_help
return;
fi
;;
-q|--quiet)
QUIET=true
shift # past argument
;;
-*|--*)
echo "Unknown option $1"
echo_help
return;
;;
esac
done
# show notification
notify() {
msg=$1
secs=$2
time=$(convert_secs $secs)
osascript -e 'display notification "'"$msg\n$time"'" with title "Pomodoro" sound name "default"'
}
# convert notifications to h:m:s format
convert_secs() {
secs=${1}
printf "%dh:%dm:%ds" $((secs/3600)) $((secs%3600/60)) $((secs%60))
}
# start a countdown for x seconds
countdown() {
secs=$1
shift
msg=$@
while [ $secs -gt 0 ]
do
t=$(convert_secs $secs)
printf "\r\033[K$msg $t"
((secs--))
sleep 1
done
echo
}
# single step pomodoro step (work / break interval)
pomodoro_step() {
if ! $QUIET; then
notify "$1!" $2
fi
countdown $2 "$1:"
}
# main pomodoro loop (infinite)
pomodoro_loop() {
counter=1
while true; do
for i in {1..3}; do
echo "\nPomodoro #$((counter++)) ..."
pomodoro_step "Work" $WORK
pomodoro_step "Break" $SHORT_BREAK
done
echo "\nPomodoro #$((counter++)) ..."
pomodoro_step "Work" $WORK
pomodoro_step "Break" $LONG_BREAK
done
}
echo "Pomodoro Timer ==="
echo "Work: $WORK sec"
echo "Short break: $SHORT_BREAK sec"
echo "Long break: $LONG_BREAK sec"
pomodoro_loop
}
@leo-pfeiffer
Copy link
Author

leo-pfeiffer commented May 8, 2022

Usage

To use, source the file:

source pomodoro.sh

and then call

pomodoro

The following commands are options are available (from pomodoro --help):

Syntax: pomodoro [ OPTIONS ]
options:
   -s or --short-break [INTEGER]
       length of short breaks in seconds
   -l or --long-break [INTEGER]
       length of long breaks in seconds
   -w or --work [INTEGER]
       length of work interval in seconds
   -q or --quiet
       do not show notifications
   -h or --help
       print help and exit

@leo-pfeiffer
Copy link
Author

I wrote an article on dev.to about the pomodoro timer. Enjoy!

@iambigyandahal
Copy link

iambigyandahal commented May 10, 2022

Hey thanks for the gist. I have added notifications for linux.

Mostly latest desktop environment will already have the requirements for running this. If in case, terminal just closes or display some error messages you may need libnotify, notification server and notify-send in order to run this.

Gist Link

@leo-pfeiffer
Copy link
Author

Hey thanks for the gist. I have added notifications for linux.

Mostly latest desktop environment will already have the requirements for running this. If in case, terminal just closes or display some error messages you may need libnotify, notification server and notify-send in order to run this.

Gist Link

Very cool, thanks for the addition!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment