944319efaedcefb66598e2168fc4cd9b58beee25
[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=false
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 # left click
58 1)
59 start=$EPOCHSECONDS
60 domins=true
61 ;;
62 # right click
63 3)
64 domins=false
65 ;;
66 esac
67 ;;
68 mins)
69 case $(echo "$json" | jq -r .button) in
70 1)
71 start2=$EPOCHSECONDS
72 ;;
73 3)
74 start2=
75 ;;
76 esac
77
78 ;;
79 esac
80 esac
81
82 time=$((EPOCHSECONDS - start))
83 total_mins=$(( time / 60 ))
84 mins=$(( total_mins % 30 ))
85 half_hours=$(( total_mins / 30 ))
86
87
88
89 printf '['
90
91
92 if [[ $start2 ]]; then
93 time2=$((EPOCHSECONDS - start2))
94 total_mins2=$(( time2 / 60 ))
95 mins2=$(( total_mins2 % 30 ))
96 half_hours2=$(( total_mins2 / 30 ))
97 # this is duplicate of mins and half hours except for different
98 # colors and looking at vars above.
99
100 # begin half hours
101 printf '{ "color": "#EFC93E", "full_text": "'
102 for ((i=half_hours2-1; i >= 0 ; i--)); do
103 printf
104 if (( i > 0 && i % 2 == 0 )); then
105 printf " "
106 fi
107 done
108 printf '"},'
109
110
111 # begin minutes
112 printf '{ "name":"mins2", "color":"#ED297D", "full_text": "'
113 for ((i=29; i >= 0 ; i--)); do
114 if (( i < mins2 )); then
115 printf
116 else
117 printf " "
118 fi
119 if (( i > 0 && i % 5 == 0 )); then
120 printf " "
121 fi
122 done
123 printf '"},'
124
125 fi
126
127 if $domins; then
128 # begin half hours
129 printf '{ "color": "#FFB542", "full_text": "'
130 for ((i=half_hours-1; i >= 0 ; i--)); do
131 printf
132 if (( i > 0 && i % 2 == 0 )); then
133 printf " "
134 fi
135 done
136 printf '"},'
137
138
139 # begin minutes
140 printf '{ "name":"mins", "color":"#0D6BDD", "full_text": "'
141 for ((i=29; i >= 0 ; i--)); do
142 if (( i < mins )); then
143 printf
144 else
145 printf " "
146 fi
147 if (( i > 0 && i % 5 == 0 )); then
148 printf " "
149 fi
150 done
151 printf '"},'
152 fi
153
154 # begin seconds
155 printf '{ "name": "seconds", "full_text": "'
156
157 for ((i=0; i < 20; i++)); do
158 # This first condition is to make the transition from full to empty
159 # be less jarring. We are filling a bucket of space with ticks of
160 # time, we would have to choose to show full or empty, but never
161 # both. Or we could have a half/tick to show full then empty real
162 # quick. I decided to try having it work like a snake, empty out the
163 # 1st quarter as we fill up the last quarter.
164
165 if (( i > 0 && i % 5 == 0 )); then
166 printf " "
167 fi
168 i_end=$(( time % 60 / 3 + 1 ))
169 if (( i_end - i > 15 )); then
170 printf " "
171 elif (( i < i_end )); then
172 printf
173 else
174 printf " "
175 fi
176 done
177 printf '"},'
178
179 ## begin day percent, in thousandths, plus a spark block for ten_thousandth.
180 printf '{ "name": "day_percent", "full_text": "'
181
182
183 # after 24 hours, reset the day start
184 if (( day_start + 24 * 60 * 60 < EPOCHSECONDS )); then
185 day_start=$(date -d $day_start_hour:$day_start_min +%s)
186 fi
187
188 spark_index="$(( (100000 -($EPOCHSECONDS - $day_start)*100000 / (16*60*60) ) % 100 / 11 ))"
189 spark=" ▁▂▃▄▅▆▇█"
190 # note: 960 minutes, so 10 minutes is about 1%
191 day_thousandth=$(( 1000 - (EPOCHSECONDS - day_start)*1000 / (16*60*60) ))
192 printf %s "$day_thousandth${spark:spark_index:1} $(date "+%l:%M")"
193
194 # .1% of a waking year is ~5.75 hours, or 365 thousandths of a day.
195 # 1% of a waking year is 3.7 days
196 # A spark line of a thousandth of a waking year is 39 minutes.
197 year_start=$(date +%s -d 'january 1 6am')
198 year_days=$(( (EPOCHSECONDS - year_start) / (24*60*60) ))
199 year_start=$(( year_start + year_days * 8*60*60 ))
200 year_spark_index=$(( ( 100000 - (EPOCHSECONDS - year_start)*100000 / (365*16*60*60) ) % 100 / 11 ))
201 year_tenthousandth=$(( 10000 - (EPOCHSECONDS - year_start)*10000 / (365*16*60*60) ))
202 printf %s " $year_tenthousandth${spark:year_spark_index:1}"
203
204
205 echo '"}
206 ],
207 '
208 }
209
210
211 # pass any arg and we just run once. mainly for debugging
212 if (( $# )); then
213 main
214
215 # debug
216 #echo date -d $day_start_hour:$day_start_min +%s
217 #echo day_start=$day_start now=$EPOCHSECONDS
218
219 else
220 printf '{ "version": 1, "click_events": true }\n['
221 while true; do
222 main
223 line=
224 read -r -t 3 line ||:
225 done
226 fi