Recently Written · git

sh

Small shell and Python scripts

git clone https://github.com/equwal/sh

Log | Files | Refs


pomodoro (1909 bytes)

1 #!/bin/sh
2 # Pomodoro timer. Usage: pomodoro [-s [profile] | -k | -g]; no args opens a dmenu menu.
3 # -g prints "profile mm:ss" and advances the timer: poll it from a status bar.
4 
5 remaining() {
6   if [ -e "$statusfile" ]; then
7     stime=$(awk '$1 == "start" {print $2}' "$statusfile")
8     time=$(awk -v p="$(current)" '$1 == p {print $2}' "$configfile")
9     echo $((stime - $(date +%s) + time))
10   fi
11 }
12 
13 current() {
14   ! [ -e "$statusfile" ] && echo "null" || awk '$1 == "current" {print $2}' "$statusfile"
15 }
16 
17 next() {
18   awk -v p="$(current)" '$1 == p {print $3}' "$configfile"
19 }
20 
21 update() {
22   printf "current %s\nstart %s" "$1" "$(date +%s)" > "$statusfile"
23 }
24 
25 list() {
26   awk '{print $1}' "$configfile"
27 }
28 
29 configdir="${XDG_CONFIG_HOME:-$HOME/.config}/pomodoro"
30 configfile="$configdir/config"
31 statusfile="${XDG_RUNTIME_DIR:-/tmp}/.pomodoro-$(id -u)"
32 
33 # Check for config file, if it doesnt exist, copy the default.
34 if ! [ -e "$configfile" ]; then
35   mkdir -p "$configdir"
36   printf 'work 1500 break\nbreak 300 work\n' > "$configfile"
37 fi
38 
39 # Set flags for the arguments.
40 while getopts ":sgk" opt; do
41   case $opt in
42     s)
43       s=0
44       ;;
45     k)
46       k=0
47       ;;
48     g)
49       g=0
50       ;;
51     \?)
52       exit 1
53   esac
54 done
55 
56 # If no input arguments, show a simple menu.
57 if [ $# -le 0 ]; then 
58   selection=$(printf "start\nkill" | dmenu)
59   [ $? -ne 0 ] && exit
60   [ "$selection" = "start" ] && s=0 || k=0
61 fi
62 shift $((OPTIND - 1))
63 
64 # Handlers.
65 if [ "$s" ]; then
66   if [ $# -le 0 ]; then
67     profile=$(list | dmenu)
68     [ $? -ne 0 ] && exit
69   else
70     profile=$1
71   fi
72   list | grep -qxF -- "$profile" || exit 1
73   update "$profile"
74 fi
75 
76 [ "$k" ] && rm -f "$statusfile"
77 
78 if [ "$g" ]; then
79   [ "$(current)" = "null" ] && exit 1
80   r=$(remaining)
81   if [ "$r" -le 0 ]; then
82     notify-send "Timer $(current)" "done"
83     update "$(next)"
84     "$0" -g
85   else
86     printf "%s %02d:%02d\n" "$(current)" $((r / 60)) $((r % 60))
87   fi
88 fi