1bf14c1c058ab08971ba7b95d8cc2e268d2eb85d
[distro-setup] / myi3status
1 #!/bin/bash
2 # Copyright (C) 2019 Ian Kelling
3 # SPDX-License-Identifier: AGPL-3.0-or-later
4
5 # usage:
6 # * left click seconds to reset main timer
7 # * right click seconds to disable minutes & half hours of main timer
8 # * left click minutes to reset 2nd timer.
9 # * right click minutes to disable 2nd timer
10
11 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
12
13 source /a/bin/errhandle/err
14
15 shopt -s nullglob
16 shopt -s dotglob
17
18 mins=0
19 half_hours=0
20 fast_blocks=30
21 start=$EPOCHSECONDS
22 domins=true
23
24
25
26 get_daystart() {
27 if [[ /b/data/daystart -ot /b/data/daystart-generated && $day_tmp ]]; then
28 return 0
29 fi
30 day_tmp=$(cat /b/data/daystart)
31 day_start_hour=${day_tmp%??}
32 day_start_min=${day_tmp: -2}
33 echo $day_start_hour > /b/data/daystart-generated
34 echo $day_start_min >> /b/data/daystart-generated
35 day_start=$(date -d $day_start_hour:$day_start_min +%s)
36 # for after midnight but before the day start.
37 if (( day_start > EPOCHSECONDS )); then
38 day_start=$(date -d "$day_start_hour:$day_start_min yesterday" +%s)
39 fi
40 }
41
42
43 main() {
44 get_daystart
45
46 ## debug
47 # if [[ $line ]]; then
48 # echo "line=$line" >>/tmp/t
49 # fi
50 case $line in
51 ""|"[") : ;;
52 *)
53 json="${line#,}"
54 case $(echo "$json" | jq -r .name) in
55 seconds)
56 case $(echo "$json" | jq -r .button) in
57 1)
58 start=$EPOCHSECONDS
59 domins=true
60 ;;
61 3)
62 domins=false
63 ;;
64 esac
65 ;;
66 mins)
67 case $(echo "$json" | jq -r .button) in
68 1)
69 start2=$EPOCHSECONDS
70 ;;
71 3)
72 start2=
73 ;;
74 esac
75
76 ;;
77 esac
78 esac
79
80 time=$((EPOCHSECONDS - start))
81 total_mins=$(( time / 60 ))
82 mins=$(( total_mins % 30 ))
83 half_hours=$(( total_mins / 30 ))
84
85
86
87 printf '['
88
89
90 if [[ $start2 ]]; then
91 time2=$((EPOCHSECONDS - start2))
92 total_mins2=$(( time2 / 60 ))
93 mins2=$(( total_mins2 % 30 ))
94 half_hours2=$(( total_mins2 / 30 ))
95 # this is duplicate of mins and half hours except for different
96 # colors and looking at vars above.
97
98 # begin half hours
99 printf '{ "color": "#EFC93E", "full_text": "'
100 for ((i=half_hours2-1; i >= 0 ; i--)); do
101 printf
102 if (( i > 0 && i % 2 == 0 )); then
103 printf " "
104 fi
105 done
106 printf '"},'
107
108
109 # begin minutes
110 printf '{ "name":"mins2", "color":"#ED297D", "full_text": "'
111 for ((i=29; i >= 0 ; i--)); do
112 if (( i < mins2 )); then
113 printf
114 else
115 printf " "
116 fi
117 if (( i > 0 && i % 5 == 0 )); then
118 printf " "
119 fi
120 done
121 printf '"},'
122
123 fi
124
125 if $domins; then
126 # begin half hours
127 printf '{ "color": "#FFB542", "full_text": "'
128 for ((i=half_hours-1; i >= 0 ; i--)); do
129 printf
130 if (( i > 0 && i % 2 == 0 )); then
131 printf " "
132 fi
133 done
134 printf '"},'
135
136
137 # begin minutes
138 printf '{ "name":"mins", "color":"#0D6BDD", "full_text": "'
139 for ((i=29; i >= 0 ; i--)); do
140 if (( i < mins )); then
141 printf
142 else
143 printf " "
144 fi
145 if (( i > 0 && i % 5 == 0 )); then
146 printf " "
147 fi
148 done
149 printf '"},'
150 fi
151
152 # begin seconds
153 printf '{ "name": "seconds", "full_text": "'
154
155 for ((i=0; i < 20; i++)); do
156 # This first condition is to make the transition from full to empty
157 # be less jarring. We are filling a bucket of space with ticks of
158 # time, we would have to choose to show full or empty, but never
159 # both. Or we could have a half/tick to show full then empty real
160 # quick. I decided to try having it work like a snake, empty out the
161 # 1st quarter as we fill up the last quarter.
162
163 if (( i > 0 && i % 5 == 0 )); then
164 printf " "
165 fi
166 i_end=$(( time % 60 / 3 + 1 ))
167 if (( i_end - i > 15 )); then
168 printf " "
169 elif (( i < i_end )); then
170 printf
171 else
172 printf " "
173 fi
174 done
175 printf '"},'
176
177 ## begin day percent, in thousandths, plus a spark block for ten_thousandth.
178 printf '{ "name": "day_percent", "full_text": "'
179
180
181 # after 24 hours, reset the day start
182 if (( day_start + 24 * 60 * 60 < EPOCHSECONDS )); then
183 day_start=$(date -d $day_start_hour:$day_start_min +%s)
184 fi
185
186 spark_index="$(( (100000 -($EPOCHSECONDS - $day_start)*100000 / (16*60*60) ) % 100 / 11 ))"
187 spark=" ▁▂▃▄▅▆▇█"
188 # note: 960 minutes, so 10 minutes is about 1%
189 day_thousandth=$(( 1000 - (EPOCHSECONDS - day_start)*1000 / (16*60*60) ))
190 printf %s "$day_thousandth${spark:spark_index:1} $(date "+%l:%M")"
191
192 # .1% of a waking year is ~5.75 hours, or 365 thousandths of a day.
193 # 1% of a waking year is 3.7 days
194 # A spark line of a thousandth of a waking year is 39 minutes.
195 year_start=$(date +%s -d 'january 1 6am')
196 year_days=$(( (EPOCHSECONDS - year_start) / (24*60*60) ))
197 year_start=$(( year_start + year_days * 8*60*60 ))
198 year_spark_index=$(( ( 100000 - (EPOCHSECONDS - year_start)*100000 / (365*16*60*60) ) % 100 / 11 ))
199 year_tenthousandth=$(( 10000 - (EPOCHSECONDS - year_start)*10000 / (365*16*60*60) ))
200 printf %s " $year_tenthousandth${spark:year_spark_index:1}"
201
202
203 echo '"}
204 ],
205 '
206 }
207
208
209 # pass any arg and we just run once. mainly for debugging
210 if (( $# )); then
211 main
212
213 # debug
214 #echo date -d $day_start_hour:$day_start_min +%s
215 #echo day_start=$day_start now=$EPOCHSECONDS
216
217 else
218 printf '{ "version": 1, "click_events": true }\n['
219 while true; do
220 main
221 line=
222 read -r -t 3 line ||:
223 done
224 fi