Skip to content

Instantly share code, notes, and snippets.

@lezsakdomi
Last active December 13, 2023 14:15
Show Gist options
  • Save lezsakdomi/027dfa7d45c603e8662af20b5f90b196 to your computer and use it in GitHub Desktop.
Save lezsakdomi/027dfa7d45c603e8662af20b5f90b196 to your computer and use it in GitHub Desktop.
Linux Pomodoro timer

pomodoro.sh

A pomodoro timer for Linux-like systems.

Highly inspired by Pomtify

Advantages

Disadvantages

  • Developing this tool actually decreases my productivity

Requirements

  • bash
  • spotify-tui
  • speech-dispatcher (make sure you also installed espeak-ng and/or festival, and ran spd-conf)

Features

  • Play/pause Spotify for breaks
  • Say it loud
  • Wait for user interaction after focus/break
#!/bin/bash
set -e
notify_interval=${notify_interval:-30s}
focus_time=${1:-25}
break_time=${2:-5}
which spt >/dev/null || { echo "spt not found"; exit 1; }
which spd-say >/dev/null || { echo "spd-say not found"; exit 1; }
remaining_sleep_time() {
ps -o etime= -o args= -p "$1" | perl -MPOSIX -lane '
%map = qw(d 86400 h 3600 m 60 s 1);
$F[0] =~ /(\d+-)?(\d+:)?(\d+):(\d+)/;
$t = -($4+60*($3+60*($2+24*$1)));
for (@F[2..$#F]) {
s/\?//g;
($n, $p) = strtod($_);
$n *= $map{substr($_, -$p)} if $p;
$t += $n
}
$output = "";
for ([60,"%02d"],[60,"%02d:"],[24,"%02d:"],[inf,"%d-"]) {
last unless $t;
$output = sprintf($_->[1], $t % $_->[0]) . $output;
$t = int($t / $_->[0])
}
printf "%s", $output;'
}
end-of(){
msg="$1 time is over, press enter to assert"
spt pb -t
echo "[$i] $msg"
printf '\7'
time while :; do
spd-say "$msg"
timeout --foreground ${notify_interval} sh -c read && break
done
}
sleep-cdn(){
sleep $1 & pid=$!
while test -d /proc/"$pid"/; do
sleep 1
printf '\e[2K'
remaining_sleep_time $pid
printf '\r'
done
}
cycle(){
date
echo "$1 for $2m"
sleep-cdn $(($2 * 60))
end-of $1
}
i=0
while :; do
let i+=1
cycle Focus $focus_time
cycle Break $break_time
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment