Last active
January 14, 2025 11:38
-
-
Save ignassew/5664497c680cf2b08439479d14e75c1b to your computer and use it in GitHub Desktop.
Pomodoro timer in 62 lines of bash code
This file contains hidden or 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 | |
| USAGE="usage: pomo <n> | |
| Where <n> is a cycle number starting from 1. | |
| If you increase 'n' by 1 every time after every pomoodoro, you will get a | |
| pomodoro cycle. | |
| Examples: | |
| Start the pomodoro from the beginning: | |
| $ pomo 1 | |
| Take a 5 minute break: | |
| $ pomo 2 | |
| Take a 15 minute break: | |
| $ pomo 8 | |
| " | |
| if ! [[ $* =~ ^[0-9]+$ ]]; then # not a number | |
| echo "$USAGE" | |
| exit 1 | |
| elif (( $@ % 2 != 0 )); then # odd | |
| DURATION=$(( 25 * 60 )) | |
| elif (( $@ % 8 == 0 )); then # even, multiple of 8 | |
| DURATION=$(( 15 * 60 )) | |
| BREAK=1 | |
| else # even | |
| DURATION=$(( 5 * 60 )) | |
| BREAK=1 | |
| fi | |
| if [[ -n $BREAK ]]; then | |
| echo Time for a break! | |
| else | |
| echo Time to focus. | |
| fi | |
| START=$(date +%s) | |
| LEFT=1 | |
| while [[ $LEFT -gt 0 ]]; do | |
| LEFT=$(( DURATION - ($(date +%s) - START))) | |
| MINUTES=$(( LEFT / 60 )) | |
| SECONDS=$(( LEFT % 60 )) | |
| echo -ne "\r${MINUTES} minutes ${SECONDS} seconds left." | |
| sleep 1 | |
| done | |
| printf "\n" | |
| if command -v notify-send > /dev/null 2>&1 ; then | |
| if [[ -n $BREAK ]]; then | |
| notify-send "Time to focus." | |
| else | |
| notify-send "Time for a break!" | |
| fi | |
| fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment