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