diff --git a/content/posts/tea-timer-in-bash/index.md b/content/posts/tea-timer-in-bash/index.md new file mode 100644 index 0000000..015932d --- /dev/null +++ b/content/posts/tea-timer-in-bash/index.md @@ -0,0 +1,62 @@ ++++ +title = "Tea Timer in Bash" +date = 2025-10-29 + +[taxonomies] +categories = ["Linux"] + +[extra] +author = "Emil Miler" ++++ + +I grew tired of unlocking my phone and starting a timer every time I brewed tea. Running a simple shell script is far more convenient, and writing it also gave me a chance to refresh my dulled shell scripting skills. + + + +Here is the full script which I named `tea` in my file system: + +```sh +#!/bin/bash + +NOTIFICATION_SOUND=~/.scripts/notification.mp3 +DELAY="${1:-180}" +DELAY_REMAINING=$DELAY + +echo "Timer set to $DELAY seconds" +echo "Remaining: $DELAY_REMAINING" + +while [ "$DELAY_REMAINING" -ne 0 ]; do + sleep 1 + ((DELAY_REMAINING--)) + echo -e "\e[1A\e[KRemaining: $DELAY_REMAINING" +done + +notify-send 'Tea is Ready' "Timer was set to $DELAY seconds" +pw-play "$NOTIFICATION_SOUND" & +``` + +So how does it work? First, we set some variables: + +```sh +NOTIFICATION_SOUND=~/.scripts/notification.mp3 +DELAY="${1:-180}" +``` + +`NOTIFICATION_SOUND` contains the path for a sound which gets played after the timer runs out. `DELAY` sets the delay to `$1`, or to default of 180 seconds if the `$1` parameter is missing. + +Next interesting part is the timing itself. It runs in a loop until `DELAY_REMAINING` reaches zero. The loop also contains a peculiar `echo`: + +```sh +echo -e "\e[1A\e[KRemaining: $DELAY_REMAINING" +``` + +This basically replaces the last line in _stdout_, where `\e[1A` moves the cursor to the previous line and `\e[K` erases the line, therefore letting us overwrite the line with new content. + +When the timer runs out, system notification is sent with `notify-send` and the predefined sound is played trough _pipewire_ using `pw-play`: + +```sh +notify-send 'Tea is Ready' "Timer was set to $DELAY seconds" +pw-play "$NOTIFICATION_SOUND" & +``` + +The usage is simple: running `tea` with an optional parameter, such as `tea 120`.