aboutsummaryrefslogtreecommitdiff
path: root/gromo
blob: d4adff4dcf5d59e80784cc37e786774cf71c4f9b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/bin/bash
#
# gromo - To count your daily Gromodoros :thumbsup. A Gromo is a short unit of
#         time, usually 20 minutes, in which you are committed to do your work
#         and only your work. It's basically the Pomodoro technique, but more
#         opinionated. In particular, it's designed to implement the advice an
#         optician once gave me, the "20/20/20" rule, which is: for each 20
#         minutes looking at the screen, look away at least 20 meters away for
#         at least 20 seconds. Also, not every type of work benefits from the
#         interruptions 5-minute rests provoke. So, if the mainstream is not
#         enough for you, enter the all-flexible, eye-careful, non-interrupting
#         Gromodoros.
#
# Dependencies: curl and mpv for the 'ding' sound; slock to force you to rest :)
################################################################################

shopt -s nullglob

DEFAULT_DING=https://gramos.me/ding.opus
GROMO_SECONDS=$((60 * 20))
STOP_SECONDS=20

DATA_DIR=${XDG_DATA_HOME:-$HOME/.local/share}/gromo
CACHE_DIR=${XDG_CACHE_HOME:-$HOME/.cache}/gromo

TODAY_DIR=$DATA_DIR/$(date -I)
DING_FILE=$CACHE_DIR/ding.opus
STATE_FILE=${XDG_STATE_HOME:-$HOME/.local/state}/gromo

if [ "$1" = "--xmobar-status" ]; then
    state=idle
    [ -f "$STATE_FILE" ] && state=$(cat $STATE_FILE)

    color=red
    [ "$state" = "idle" ] && color=green

    if [ -d "$TODAY_DIR" ]; then
        declare -a today
        for gromo in "$TODAY_DIR"/*; do
            today+=("$(basename $gromo): $(cat $gromo)")
        done
        [ -n "$today" ] && today=" (${today[@]})"
    fi
    echo "<fc=$color>$state</fc>$today"
    exit 0
elif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
    cmd=$(basename $0)
    echo -e "Usage:
        $cmd --xmobar-status  \tReturn state formatted for display in xmobar
        $cmd --list | -l      \tList past gromos
        $cmd --help | -h      \tShow this help
        $cmd [gromo] [descr]  \tStart counting gromo (default gromo: "work")
    "
    exit 0
elif [ "$1" = "--list" ] || [ "$1" = "-l" ]; then
    find $DATA_DIR/ -type f -print | xargs -n 1 basename | sort -u
    exit 0
elif [ "$#" -ge 1 ]; then
    gromo="$1"
    desc="$2"
else
    gromo=work
fi

gromos=0
gromo_file="$TODAY_DIR/$gromo"
mkdir -p "$DATA_DIR" "$TODAY_DIR"

[ -f "$DING_FILE" ] || curl $DEFAULT_DING --create-dirs -so "$DING_FILE"

if [ -f $STATE_FILE ]; then
    echo "Another instance is currently running; exiting (remove $STATE_FILE to override)"
    exit 1
fi
echo idle > "$STATE_FILE"
trap 'rm $STATE_FILE 2> /dev/null && echo -e "\n\nToday: $gromos \"$gromo\" gromos ($gromo_time)"; exit' INT TERM EXIT

while true; do
    [ -f "$gromo_file" ] && read -r gromos < "$gromo_file"
    seconds=$((gromos * GROMO_SECONDS))
    if [ $seconds -lt 60 ]; then
        gromo_time="$seconds seconds"
    elif [ $seconds -lt 3600 ]; then
        gromo_time="$((seconds / 60)) minutes"
    else
        gromo_time="$((seconds / 3600)) hours"
    fi

    echo -ne "\r\033[K[** IN PROGRESS: $gromo **] "

    if [ -n "$desc" ]; then
        echo "$gromo ($desc)" > "$STATE_FILE"
    else
        echo "$gromo" > "$STATE_FILE"
    fi
    sleep $GROMO_SECONDS
    echo idle > "$STATE_FILE"

    gromos=$((gromos+1))
    echo "$gromos" > "$gromo_file"

    (sleep $STOP_SECONDS && mpv --no-terminal "$DING_FILE") &
    slock
done