fixes and improvements
[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 #
7 # * left click seconds to reset & enable main timer which shows 30
8 # minutes in blue and half hours in orange.
9 #
10 # * right click seconds to disable minutes & half hours of main timer
11 #
12 # * left click minutes to reset & enable 2nd timer.
13 #
14 # * right click minutes to disable 2nd timer
15
16 # docs:
17 #
18 # Sections from right to left.
19 #
20 # section #1, labeled day_percent
21 #
22 # 1. Thousandths of a 16 hour day left. 1 = 57.6 seconds.
23 # 2. Bar of one thousand of a day which gets shorter by ninths. One ninth is 6.4 seconds.
24 # 3. Start of the day (only the non-zero numbers). Set this with the ds command.
25 # 4. The time right now.
26 # 5. Ten thousands of the year left. 1 = 52:34 minutes (leap year = 52:42)
27 # 6. Bar of 1/10,000 of a year shrinking by ninths. 1 ninth = 5:50 minutes (leap year = 5:51 mins).
28 #
29 # section #2, labeled seconds
30 #
31 # block characters, ▉, are added every 3 seconds, a total of 20 for 1
32 # minute. The groups of 5 blocks = 15 seconds each.
33 #
34 # section #3, labeled mins
35 #
36 # Only enabled by clicking on seconds, see usage.
37 #
38 # Block characters are added every minute until 30, then reset. There
39 # are 6 groups of 5 minutes.
40 #
41 # Section #4
42 #
43 # Half hours in orange, grouped into groups of 2. This will grow until
44 # it fills the screen.
45 #
46 # Section #5 & 6
47 #
48 # Only enabled by clicking on mins section, see usage.
49 #
50 # These repeat sections 3 and 4 in violet and some other color.
51 #
52 #
53 if [ -z "$BASH_VERSION" ]; then echo "error: shell is not bash" >&2; exit 1; fi
54
55 source /a/bin/errhandle/err
56
57 shopt -s nullglob
58 shopt -s dotglob
59
60 mins=0
61 half_hours=0
62 fast_blocks=30
63 start=$EPOCHSECONDS
64 domins=false
65
66 if date -d 'february 29' &>/dev/null; then
67 days_in_this_year=366
68 else
69 days_in_this_year=365
70 fi
71
72
73 get_daystart() {
74 if [[ /b/data/daystart -ot /b/data/daystart-generated && $day_start_24h ]]; then
75 return 0
76 fi
77 day_start_24h=$(cat /b/data/daystart)
78 day_start_sig_digits=${day_start_24h%?}
79 day_start_sig_digits=${day_start_sig_digits%0}
80 day_start_hour=${day_start_24h%??}
81 day_start_min=${day_start_24h: -2}
82 echo $day_start_hour > /b/data/daystart-generated
83 echo $day_start_min >> /b/data/daystart-generated
84 day_start=$(date -d $day_start_hour:$day_start_min +%s)
85 # for after midnight but before the day start.
86 if (( day_start > EPOCHSECONDS )); then
87 day_start=$(date -d "$day_start_hour:$day_start_min yesterday" +%s)
88 fi
89 }
90
91
92 main() {
93 get_daystart
94
95 ## debug
96 # if [[ $line ]]; then
97 # echo "line=$line" >>/tmp/t
98 # fi
99 case $line in
100 ""|"[") : ;;
101 *)
102 json="${line#,}"
103 case $(echo "$json" | jq -r .name) in
104 seconds)
105 case $(echo "$json" | jq -r .button) in
106 # left click
107 1)
108 start=$EPOCHSECONDS
109 domins=true
110 ;;
111 # right click
112 3)
113 domins=false
114 ;;
115 esac
116 ;;
117 mins)
118 case $(echo "$json" | jq -r .button) in
119 1)
120 start2=$EPOCHSECONDS
121 ;;
122 3)
123 start2=
124 ;;
125 esac
126
127 ;;
128 esac
129 esac
130
131 time=$((EPOCHSECONDS - start))
132 total_mins=$(( time / 60 ))
133 mins=$(( total_mins % 30 ))
134 half_hours=$(( total_mins / 30 ))
135
136
137
138 printf '['
139
140
141 if [[ $start2 ]]; then
142 time2=$((EPOCHSECONDS - start2))
143 total_mins2=$(( time2 / 60 ))
144 mins2=$(( total_mins2 % 30 ))
145 half_hours2=$(( total_mins2 / 30 ))
146 # this is duplicate of mins and half hours except for different
147 # colors and looking at vars above.
148
149 # begin half hours
150 printf '{ "color": "#EFC93E", "full_text": "'
151 for ((i=half_hours2-1; i >= 0 ; i--)); do
152 printf
153 if (( i > 0 && i % 2 == 0 )); then
154 printf " "
155 fi
156 done
157 printf '"},'
158
159
160 # begin minutes
161 printf '{ "name":"mins2", "color":"#ED297D", "full_text": "'
162 for ((i=29; i >= 0 ; i--)); do
163 if (( i < mins2 )); then
164 printf
165 else
166 printf " "
167 fi
168 if (( i > 0 && i % 5 == 0 )); then
169 printf " "
170 fi
171 done
172 printf '"},'
173
174 fi
175
176 if $domins; then
177 # begin half hours
178 printf '{ "color": "#FFB542", "full_text": "'
179 for ((i=half_hours-1; i >= 0 ; i--)); do
180 printf
181 if (( i > 0 && i % 2 == 0 )); then
182 printf " "
183 fi
184 done
185 printf '"},'
186
187
188 # begin minutes
189 printf '{ "name":"mins", "color":"#0D6BDD", "full_text": "'
190 for ((i=29; i >= 0 ; i--)); do
191 if (( i < mins )); then
192 printf
193 else
194 printf " "
195 fi
196 if (( i > 0 && i % 5 == 0 )); then
197 printf " "
198 fi
199 done
200 printf '"},'
201 fi
202
203 # begin seconds
204 printf '{ "name": "seconds", "full_text": "'
205
206 for ((i=0; i < 20; i++)); do
207 # This first condition is to make the transition from full to empty
208 # be less jarring. We are filling a bucket of space with ticks of
209 # time, we would have to choose to show full or empty, but never
210 # both. Or we could have a half/tick to show full then empty real
211 # quick. I decided to try having it work like a snake, empty out the
212 # 1st quarter as we fill up the last quarter.
213
214 if (( i > 0 && i % 5 == 0 )); then
215 printf " "
216 fi
217 i_end=$(( time % 60 / 3 + 1 ))
218 if (( i_end - i > 15 )); then
219 printf " "
220 elif (( i_end == 1 && i == 0 )); then
221 printf B
222 elif (( i < i_end )); then
223 printf
224 else
225 printf " "
226 fi
227 done
228 printf '"},'
229
230 ## begin day percent, in thousandths, plus a spark block for ten_thousandth.
231 printf '{ "name": "day_percent", "full_text": "'
232
233
234 # after 24 hours, reset the day start
235 if (( day_start + 24 * 60 * 60 < EPOCHSECONDS )); then
236 day_start=$(date -d $day_start_hour:$day_start_min +%s)
237 fi
238
239 # there are 9 spark levels, 1/9 = .111.... In order to keep it in bash math, we upscale
240 # the number and divide by 111, 3 digits is good enough accuracy.
241 spark_index="$(( (1000000 -($EPOCHSECONDS - $day_start)*1000000 / (16*60*60) ) % 1000 / 111 ))"
242 spark=" ▁▂▃▄▅▆▇█"
243 # note: 960 minutes, so 10 minutes is about 1%
244 day_thousandth=$(( 1000 - (EPOCHSECONDS - day_start)*1000 / (16*60*60) ))
245 printf %s "$day_thousandth${spark:spark_index:1}$day_start_sig_digits $(date "+%l:%M")"
246
247 # .1% of a waking year is ~5.75 hours, or 365 thousandths of a day.
248 # 1% of a waking year is 3.7 days
249 # A spark line of a thousandth of a waking year is 39 minutes.
250 year_start=$(date +%s -d 'january 1 6am')
251 year_days=$(( (EPOCHSECONDS - year_start) / (24*60*60) ))
252 year_start=$(( year_start + year_days * 8*60*60 ))
253 year_spark_index=$(( ( 1000000 - (EPOCHSECONDS - year_start)*1000000 / (days_in_this_year*16*60*60) ) % 1000 / 111 ))
254 year_tenthousandth=$(( 10000 - (EPOCHSECONDS - year_start)*10000 / (days_in_this_year*16*60*60) ))
255 printf %s " $year_tenthousandth${spark:year_spark_index:1}"
256
257
258 echo '"}
259 ],
260 '
261 }
262
263
264 # pass any arg and we just run once. mainly for debugging
265 if (( $# )); then
266 main
267
268 # debug
269 #echo date -d $day_start_hour:$day_start_min +%s
270 #echo day_start=$day_start now=$EPOCHSECONDS
271
272 else
273 printf '{ "version": 1, "click_events": true }\n['
274 while true; do
275 main
276 line=
277 read -r -t 3 line ||:
278 done
279 fi