d8c5d22e7c020280231c927ab784c1519b38860c
[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 tmp_focus1=$(mktemp)
61 tmp_focus2=$(mktemp)
62
63 mins=0
64 half_hours=0
65 start=$EPOCHSECONDS
66 domins=false
67
68 if date -d 'february 29' &>/dev/null; then
69 days_in_this_year=366
70 else
71 days_in_this_year=365
72 fi
73
74
75 get_daystart() {
76 if [[ /b/data/daystart -ot /b/data/daystart-generated && $day_start_24h ]]; then
77 return 0
78 fi
79 day_start_24h=$(cat /b/data/daystart)
80 day_start_sig_digits=${day_start_24h%?}
81 day_start_sig_digits=${day_start_sig_digits%0}
82 day_start_hour=${day_start_24h%??}
83 day_start_min=${day_start_24h: -2}
84 echo $day_start_hour > /b/data/daystart-generated
85 echo $day_start_min >> /b/data/daystart-generated
86 day_start=$(date -d $day_start_hour:$day_start_min +%s)
87 # for after midnight but before the day start.
88 if (( day_start > EPOCHSECONDS )); then
89 day_start=$(date -d "$day_start_hour:$day_start_min yesterday" +%s)
90 fi
91 }
92
93
94 main() {
95 get_daystart
96
97 ## debug
98 # if [[ $line ]]; then
99 # echo "line=$line" >>/tmp/t
100 # fi
101 case $line in
102 ""|"[") : ;;
103 *)
104 json="${line#,}"
105 case $(echo "$json" | jq -r .name) in
106 seconds)
107 case $(echo "$json" | jq -r .button) in
108 # left click
109 1)
110 start=$EPOCHSECONDS
111 domins=true
112 ;;
113 # right click
114 3)
115 domins=false
116 ;;
117 esac
118 ;;
119 mins)
120 case $(echo "$json" | jq -r .button) in
121 1)
122 start2=$EPOCHSECONDS
123 ;;
124 3)
125 start2=
126 ;;
127 esac
128
129 ;;
130 esac
131 esac
132
133 time=$((EPOCHSECONDS - start))
134 total_mins=$(( time / 60 ))
135 mins=$(( total_mins % 30 ))
136 half_hours=$(( total_mins / 30 ))
137
138
139
140 printf '['
141
142 if [[ -e /dev/shm/iank-status && ! -e /tmp/quiet-status ]]; then
143 ps_char=
144 eval "$(< /dev/shm/iank-status)"
145 fi
146
147 touch --date="10 minutes ago" $tmp_focus1
148 touch --date="30 minutes ago" $tmp_focus2
149 f=/tmp/focus-last-input
150
151 # We output a reminder to do input to the focus app if we haven't done
152 # it less 10 minutes ago (but dont bother if its been more than 30
153 # minutes, maybe we aren't running it anymore.)
154 if [[ -e $f && $tmp_focus2 -ot $f && $tmp_focus1 -nt $f ]]; then
155 ps_char="=======FOCUS====== $ps_char"
156 fi
157
158
159 printf '{ "name":"status", "color":"#ED297D", "full_text": "%s' "$ps_char"
160 printf '"},'
161
162
163 if [[ $start2 ]]; then
164 time2=$((EPOCHSECONDS - start2))
165 total_mins2=$(( time2 / 60 ))
166 mins2=$(( total_mins2 % 30 ))
167 half_hours2=$(( total_mins2 / 30 ))
168 # this is duplicate of mins and half hours except for different
169 # colors and looking at vars above.
170
171 # begin half hours
172 printf '{ "color": "#EFC93E", "full_text": "'
173 for ((i=half_hours2-1; i >= 0 ; i--)); do
174 printf
175 if (( i > 0 && i % 2 == 0 )); then
176 printf " "
177 fi
178 done
179 printf '"},'
180
181
182 # begin minutes
183 printf '{ "name":"mins2", "color":"#ED297D", "full_text": "'
184 for ((i=29; i >= 0 ; i--)); do
185 if (( i < mins2 )); then
186 printf
187 else
188 printf " "
189 fi
190 if (( i > 0 && i % 5 == 0 )); then
191 printf " "
192 fi
193 done
194 printf '"},'
195
196 fi
197
198 if $domins; then
199 # begin half hours
200 printf '{ "color": "#FFB542", "full_text": "'
201 for ((i=half_hours-1; i >= 0 ; i--)); do
202 printf
203 if (( i > 0 && i % 2 == 0 )); then
204 printf " "
205 fi
206 done
207 printf '"},'
208
209
210 # begin minutes
211 printf '{ "name":"mins", "color":"#0D6BDD", "full_text": "'
212 for ((i=29; i >= 0 ; i--)); do
213 if (( i < mins )); then
214 printf
215 else
216 printf " "
217 fi
218 if (( i > 0 && i % 5 == 0 )); then
219 printf " "
220 fi
221 done
222 printf '"},'
223 fi
224
225 # begin seconds
226 printf '{ "name": "seconds", "full_text": "'
227
228 for ((i=0; i < 20; i++)); do
229 # This first condition is to make the transition from full to empty
230 # be less jarring. We are filling a bucket of space with ticks of
231 # time, we would have to choose to show full or empty, but never
232 # both. Or we could have a half/tick to show full then empty real
233 # quick. I decided to try having it work like a snake, empty out the
234 # 1st quarter as we fill up the last quarter.
235
236 if (( i > 0 && i % 5 == 0 )); then
237 printf " "
238 fi
239 i_end=$(( time % 60 / 3 + 1 ))
240 if (( i_end - i > 15 )); then
241 printf " "
242 elif (( i_end == 1 && i == 0 )); then
243 printf B
244 elif (( i < i_end )); then
245 printf
246 else
247 printf " "
248 fi
249 done
250 printf '"},'
251
252 ## begin day percent, in thousandths, plus a spark block for ten_thousandth.
253 printf '{ "name": "day_percent", "full_text": "'
254
255
256 # after 24 hours, reset the day start
257 if (( day_start + 24 * 60 * 60 < EPOCHSECONDS )); then
258 day_start=$(date -d $day_start_hour:$day_start_min +%s)
259 fi
260
261 # there are 9 spark levels, 1/9 = .111.... In order to keep it in bash math, we upscale
262 # the number and divide by 111, 3 digits is good enough accuracy.
263 spark_index="$(( (1000000 -(EPOCHSECONDS - day_start)*1000000 / (16*60*60) ) % 1000 / 111 ))"
264 spark=" ▁▂▃▄▅▆▇█"
265 # note: 960 minutes, so 10 minutes is about 1%
266 day_thousandth=$(( 1000 - (EPOCHSECONDS - day_start)*1000 / (16*60*60) ))
267 printf %s "$day_thousandth${spark:spark_index:1}$day_start_sig_digits $(date "+%l:%M")"
268
269 # .1% of a waking year is ~5.75 hours, or 365 thousandths of a day.
270 # 1% of a waking year is 3.7 days
271 # A spark line of a thousandth of a waking year is 39 minutes.
272 year_start=$(date +%s -d 'january 1 6am')
273 year_days=$(( (EPOCHSECONDS - year_start) / (24*60*60) ))
274 year_start=$(( year_start + year_days * 8*60*60 ))
275 year_spark_index=$(( ( 1000000 - (EPOCHSECONDS - year_start)*1000000 / (days_in_this_year*16*60*60) ) % 1000 / 111 ))
276 year_tenthousandth=$(( 10000 - (EPOCHSECONDS - year_start)*10000 / (days_in_this_year*16*60*60) ))
277 printf %s " $year_tenthousandth${spark:year_spark_index:1}"
278
279
280 echo '"}
281 ],
282 '
283 }
284
285
286 # pass any arg and we just run once. mainly for debugging
287 if (( $# )); then
288 main
289
290 # debug
291 #echo date -d $day_start_hour:$day_start_min +%s
292 #echo day_start=$day_start now=$EPOCHSECONDS
293
294 else
295 printf '{ "version": 1, "click_events": true }\n['
296 while true; do
297 main
298 line=
299 read -r -t 3 line ||:
300 done
301 fi
302
303 rm -f $tmp_focus1 $tmp_focus2