Last active
March 6, 2021 11:54
-
-
Save heitor31415/7ec80e79a7a7b9d21fc77bc2da4508fd to your computer and use it in GitHub Desktop.
Bash Pomodoro
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 | |
# My shell pomodoro | |
WORK=25 # Minutes of work | |
PAUSE=5 # Minutes to a short pause | |
TIMES=1 # Repeat Pomodoro cycle TIMES times | |
usage() { # Function: Print a help message. | |
echo "Usage: $0 [ -t TIMES ] [ -w WORK ] [ -p PAUSE ]" 1>&2 | |
} | |
exit_abnormal() { # Function: Exit with error. | |
usage | |
exit 1 | |
} | |
while getopts ":t:w:p:" options; do # Loop: Get the next option; | |
# use silent error checking; | |
# options t, w and p take arguments. | |
case "${options}" in | |
t) # If the option is t, | |
TIMES=${OPTARG} # Set $TIMES to specified value. | |
re_isanum='^[0-9]+$' # Regex: match whole numbers only | |
if ! [[ $TIMES =~ $re_isanum ]] ; then # if $TIMES not a whole number: | |
echo "Error: TIMES must be a positive, whole number." | |
exit_abnormal | |
exit 1 | |
elif [ $TIMES -eq "0" ]; then # If it's zero: | |
echo "Error: TIMES must be greater than zero." | |
exit_abnormal # Exit abnormally. | |
fi | |
;; | |
w) | |
WORK=${OPTARG} | |
re_isanum='^[0-9]+$' | |
if ! [[ $WORK =~ $re_isanum ]] ; then | |
echo "Error: WORK must be a positive, whole number." | |
exit_abnormal | |
exit 1 | |
elif [ $WORK -eq "0" ]; then | |
echo "Error: WORK must be greater than zero." | |
exit_abnormal | |
fi | |
;; | |
p) | |
PAUSE=${OPTARG} | |
re_isanum='^[0-9]+$' | |
if ! [[ $PAUSE =~ $re_isanum ]] ; then | |
echo "Error: PAUSE must be a positive, whole number." | |
exit_abnormal | |
exit 1 | |
elif [ $PAUSE -eq "0" ]; then | |
echo "Error: PAUSE must be greater than zero." | |
exit_abnormal | |
fi | |
;; | |
:) # If expected argument omitted: | |
echo "Error: -${OPTARG} requires an argument." | |
exit_abnormal # Exit abnormally. | |
;; | |
*) # If unknown (any other) option: | |
exit_abnormal # Exit abnormally. | |
;; | |
esac | |
done | |
echo Pomodoro repeating $TIMES times with $WORK working minutes and $PAUSE minutes of pause. | |
WORK=$(echo $(( WORK * 60 ))) | |
PAUSE=$(echo $(( PAUSE * 60 ))) | |
COUNT=1 # A counter. | |
while [ $COUNT -le $TIMES ]; do # While counter is less than | |
# or equal to $TIMES, | |
notify-send "Your pomodoro session just started." | |
sleep $WORK && notify-send "Your pomodoro session just ended. You may rest" && sleep $PAUSE && let COUNT+=1 # then increment the counter. | |
done | |
notify-send "Your pomodoro cycle has ended" | |
exit 0 # Exit normally. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment