65285329a36f51fc717f484e248e7a6a40c2d599
[distro-setup] / myi3status
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
6
7 source /a/bin/errhandle/err
8
9 shopt -s nullglob
10 shopt -s dotglob
11
12 shopt -s nullglob
13 for p in ~/.gem/ruby/*/bin; do
14 PATH="$PATH:$p"
15 done
16
17 todaycache=/tmp/timetraptoday
18 newer() {
19 if [[ ! -e $todaycache ]]; then
20 return 1
21 fi
22 ! find $todaycache -mmin $1 -type f -exec false {} + &>/dev/null
23 }
24 td() { /a/opt/timetrap/bin/t d -ftotal all -m '^w|x$' "$@"; }
25
26 main() {
27
28 ps_char=
29 if [[ -e /dev/shm/iank-status ]]; then
30 eval $(< /dev/shm/iank-status)
31 fi
32
33 # clocked in or out?
34 cursheet=$(sqlite3 /p/.timetrap.db "select sheet from entries where end is NULL;")
35
36 if [[ $cursheet ]]; then
37 if i3-msg -t get_bar_config bar-0 | grep '"mode":"dock"' &>/dev/null; then
38 i3-msg -q 'bar mode hide'
39 fi
40 else
41 if i3-msg -t get_bar_config bar-0 | grep '"mode":"hide"' &>/dev/null; then
42 i3-msg -q 'bar mode dock'
43 fi
44 cursheet=out
45 fi
46
47 # maybe sometime show work and x separate
48 if newer 3; then
49 today=$(< /tmp/timetraptoday)
50 elif newer 10; then
51 today=$(< /tmp/timetraptoday)
52 td -s today >/tmp/timetraptoday &
53 else
54 today=$(td -s today)
55 echo "$today" >/tmp/timetraptoday
56 fi
57
58 # hours remaining in the day:
59
60 # work+study in the last week, not counting today.
61 # no arg to printf for the intentional behavior of no arg
62 # shellcheck disable=SC2183
63 cache=/tmp/timetrap$(printf '%(%F)T')
64 if [[ -e $cache ]]; then
65 eval $(<$cache)
66 else
67 week=$(td -s '8 days ago' -e 'yesterday')
68 printf 'week="%s"\n' "$week" >$cache
69 week4=$(td -s '29 days ago' -e 'yesterday')
70 printf 'week4="%s"\n' "$week4" >>$cache
71 fi
72 # weekly work+study in the last 4 weeks, not counting today.
73 # todo later:
74 # 4 weeks
75 # ($x4week + $w4week + .05) /4
76 #weekly work+study in the last 16 weeks, not counting today.
77 #weekly work+study in the last 52 weeks, not counting today.
78
79 unixtime=$EPOCHSECONDS
80 midnight=$(date +%s -d 'tomorrow 00:00')
81 tmp=($(echo "scale=1; today_left=($midnight-$unixtime) / 60 / 60;
82 today_left;
83 done_today=($today + .05) /1;
84 /* hours I could do today by working 80% of the remainder time */
85 today_left * .8 + done_today;
86 done_today;
87 ($week + .05) /1;
88 ($week4 + .05) /4"|bc))
89 printf "[ { \"full_text\": \""
90 if [[ $ps_char ]]; then
91 printf "%s| " "$ps_char"
92 fi
93 printf "%s | left today %s %s | today %s | week %s | 4weeks %s | %s\" } ],\n" "$cursheet" "${tmp[@]}" "$(date "+%A, %B %d, %r")"
94 }
95
96 # pass any arg and we just run once
97 if (( $# )); then
98 main
99 else
100 printf '{ "version": 1 }\n['
101 while true; do
102 main
103 sleep 5
104 done
105 fi