bashpomo.sh

Small pomodoro timer in bash. Should be run in tmux session.
[raw]

01: #!/bin/bash
02: # @desc Small pomodoro timer in bash. Should be run in tmux session.
03: SCRIPTDIR=$(dirname $(readlink -f $0))
04: eprintf(){
05:     printf "$@" >&2
06: }
07: datefmt(){
08:     date -d@"$1" +%H:%M:%S
09: }
10: datediff(){
11:     date -ud@"$(($1-$2))" +%H:%M:%S
12: }
13: setstatus(){
14:     eprintf "%s\n" "$1"
15:     notify-send "$1"
16:     bash $SCRIPTDIR/telegram.sh "$1"
17: }
18: timer_wait(){
19:     TIME_START="$(date +%s)"
20:     REAL_START=$TIME_START
21:     TIME_END=$(( $(date +%s) + $1 ))
22:     sp='/-\|'
23:     i=1
24:     while [ $(date +%s) -lt $TIME_END ]  || [ ${PAUSE:-0} -gt 0 ]
25:     do
26:         TIME_NOW=$(date +%s)
27:         PERCENT=$(( ( $TIME_NOW - $TIME_START ) * 100 / ($TIME_END - $TIME_START) ))
28:         [ ${PAUSE:-0} -eq 0 ]&& \
29:             eprintf "[%s-%s] %s %d%% %c\r" "$(datefmt $REAL_START)" "$(datefmt $TIME_END)" \
30:                         "$(datediff $TIME_END $TIME_NOW)" "$PERCENT" "${sp:i++%${#sp}:1}"
31:         read -n 1 -t 1 -s action_ch
32:         case "$action_ch" in
33:             "b" | "B") break ;;
34:             "s" | "S" | "q" | "Q" ) exit 0;;
35:             "p") 
36:                 if [ "${PAUSE:-0}" -gt 0 ]
37:                 then
38:                     DELTA=$(( $(date +%s) - $PAUSE ))
39:                     PAUSE=0
40:                     TIME_START=$(($TIME_START +$DELTA))
41:                     TIME_END=$(( $TIME_END + $DELTA ))
42:                 else
43:                     eprintf "\n[timer paused]\n"
44:                     PAUSE=$(date +%s)
45:                 fi
46:             ;;
47:         esac
48:     done
49:     echo >&2
50: 
51: }
52: state_work(){
53:     setstatus "Work"
54:     mpc play >&2
55:     task >&2
56:     timer_wait $(( 25*60 ))
57:     mpc stop >&2
58:     if (( $BRKS % $BREAK_RATIO )) || ! (($BRKS))
59:     then
60:         echo state_short_break
61:     else 
62:         echo state_long_break
63:     fi
64: }
65: state_short_break(){
66:     setstatus "Short break"
67:     timer_wait $(( 5*60 )) 
68:     echo state_work
69: }
70: state_long_break(){
71:     setstatus "Long break"
72:     fortune >&2
73:     timer_wait $(( 15*60 ))
74:     echo state_work
75: }
76: STATE=state_work
77: BRKS=0
78: BREAK_RATIO=3
79: while [ -n "$STATE" ]
80: do
81:     if [[ $STATE =~ break ]]
82:     then 
83:         BRKS=$(( $BRKS + 1 ))
84:     fi
85:     STATE=`$STATE`
86: done
87: