summaryrefslogtreecommitdiffstats
path: root/listimer
blob: 4c43b7f6702f5be92490332146cd92639aa1f3f7 (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
#!/bin/sh

COMMAND=$1
case "$COMMAND"
in
  "start")
    if [ "$#" -ne 2 ]; then
        echo "Usage: listimer start <minutes>";
        exit 1;
    fi
    DURATION_MIN=$2
    CURRENT_TIME=$(date +%s)
    echo "$CURRENT_TIME\n\n$DURATION_MIN" > /tmp/listimer
  ;;

  "restart")
    if [ "$#" -ne 1 ]; then
        echo "Usage: listimer restart";
        exit 1;
    fi
    CURRENT_TIME=$(date +%s)
    sed -i "1s/.*/$CURRENT_TIME/" /tmp/listimer
  ;;

  "stop")
    if [ "$#" -ne 1 ]; then
        echo "Usage: listimer stop";
        exit 1;
    fi
    CURRENT_TIME=$(date +%s)
    rm /tmp/listimer
  ;;

  "query")
    if [ ! -f /tmp/listimer ]; then
      echo "No timer"
      exit 0;
    fi

    START_TIME=$(sed -n '1p' < /tmp/listimer)
    DURATION_MIN=$(sed -n '3p' < /tmp/listimer)
    DURATION_SEC=$DURATION_MIN*60
    CURRENT_TIME=$(date +%s)

    ELAPSED_TIME_SEC=$(($CURRENT_TIME-$START_TIME))
    ELAPSED_TIME_MIN=$(($ELAPSED_TIME_SEC/60))
    REMAINING_TIME_MIN=$(($DURATION_MIN-$ELAPSED_TIME_MIN))

    if [ $REMAINING_TIME_MIN -lt 0 ]; then
      echo "TIME OUT!"
    elif [ "$REMAINING_TIME_MIN" -lt "$(($DURATION_MIN / 10))" ]; then
      echo "<span weight=\"bold\" size=\"larger\" background=\"#FF0000\">$REMAINING_TIME_MIN minutes</span>"
    else
      echo "$REMAINING_TIME_MIN minutes" 
    fi
  ;;
  *) echo "Unexpected command."
esac
Go back to lisible.xyz