Move screen device name to a global constant
[khatus.git] / bin / khatus_show
1 #! /bin/bash
2
3 set -e
4
5 BIN=$HOME/bin
6 STATUS_DIR=$HOME/var/run/status
7 STATUS_FILE__WIFI=$STATUS_DIR/wifi
8 STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF=$STATUS_DIR/notified_energy_bellow_half
9 DISK_IO_DEVICE='dm-1'
10 DISK_SPACE_DEVICE='/dev/mapper/kubuntu--vg-root'
11 SCREEN_DEVICE='acpi_video0'
12
13
14 load=$(awk '{printf("%4.2f", $1)}' /proc/loadavg)
15
16 fan=$(awk '/^speed:/ {printf "%4d", $2}' /proc/acpi/ibm/fan)
17
18 #cpu=$($BIN/khatus_cpu_usage_from_proc_since_last_check)
19
20 memory=$(
21 free \
22 | awk '
23 function round(n) {return int(n + 0.5)}
24
25 $1 == "Mem:" {
26 total=$2;
27 used=$3;
28 cache=$6;
29 prev_file = ENVIRON["HOME"] "/var/run/status/memory_used_percentage";
30 curr = round(used / total * 100);
31 getline prev < prev_file;
32 print curr > prev_file;
33 if (curr > prev) {
34 direction = ">";
35 } else if (curr < prev) {
36 direction = "<";
37 } else {
38 direction = "=";
39 }
40 printf("%s%d%%", direction, curr);
41 }')
42
43 temp=$(awk 'NR == 1 {print $1 / 1000}' /sys/class/thermal/thermal_zone0/temp)
44
45 disk_io=$(
46 awk '
47 {
48 bytes_per_sector = 512
49 bytes_per_unit = 1024 * 1024
50
51 curr_sectors_read = $3
52 curr_sectors_write = $7
53
54 prev_file_prefix = ENVIRON["HOME"] "/var/run/status/disk_io"
55 prev_sectors_read_file = prev_file_prefix "_sectors_read"
56 prev_sectors_write_file = prev_file_prefix "_sectors_write"
57
58 getline prev_sectors_read < prev_sectors_read_file
59 getline prev_sectors_write < prev_sectors_write_file
60
61 diff_read_sectors = (curr_sectors_read - prev_sectors_read)
62 diff_write_sectors = (curr_sectors_write - prev_sectors_write)
63
64 diff_read_bytes = diff_read_sectors * bytes_per_sector
65 diff_write_bytes = diff_write_sectors * bytes_per_sector
66
67 diff_read = diff_read_bytes / bytes_per_unit
68 diff_write = diff_write_bytes / bytes_per_unit
69
70 print curr_sectors_read > prev_sectors_read_file
71 print curr_sectors_write > prev_sectors_write_file
72
73 printf("%0.3f▲ %0.3f▼\n", diff_write, diff_read);
74
75 }
76 ' "/sys/block/$DISK_IO_DEVICE/stat"
77 )
78
79 disk=$(
80 df \
81 | awk \
82 -v disk_io="$disk_io" \
83 -v device="$DISK_SPACE_DEVICE" \
84 '
85 function round(n) {return int(n + 0.5)}
86
87 $1 == device {
88 curr_perc = $5; sub("%$", "", curr_perc);
89 prev_perc_file = ENVIRON["HOME"] "/var/run/status/disk_space_used";
90 getline prev_perc < prev_perc_file;
91 print curr_perc > prev_perc_file;
92 if (curr_perc > prev_perc) {
93 direction = ">";
94 } else if (curr_perc < prev_perc) {
95 direction = "<";
96 } else {
97 direction = "=";
98 }
99 printf("%s[%d%% %s]", direction, curr_perc, disk_io);
100 }')
101
102 # TODO: Wi-Fi status file should be a file per-wifi-device
103 network=$(
104 ip -s addr \
105 | awk \
106 -v wifi_conn="$(cat $STATUS_FILE__WIFI)" \
107 -v prefixes_of_interfaces_to_show='w' \
108 '
109 BEGIN {
110 bytes_per_unit = 1024 * 1024
111 }
112
113 /^[0-9]+:/ {
114 sub(":$", "", $1)
115 sub(":$", "", $2)
116 sequence = $1
117 interface = $2
118 interfaces[sequence] = interface
119 }
120
121 /^ +inet [0-9]/ {
122 sub("/[0-9]+", "", $2)
123 addr = $2
124 addrs[interface] = addr
125 }
126
127 /^ +RX: / {transfer_direction = "r"}
128 /^ +TX: / {transfer_direction = "w"}
129
130 /^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ {
131 io[interface, transfer_direction] = $1;
132 }
133
134 END {
135 for (seq=1; seq<=sequence; seq++) {
136 interface = interfaces[seq]
137 label = substr(interface, 1, 1)
138 if (addrs[interface]) {
139 curr_read = io[interface, "r"]
140 curr_write = io[interface, "w"]
141
142 prefix = ENVIRON["HOME"] "/var/run/status/io_net_" interface
143 prev_read_file = prefix "_read"
144 prev_write_file = prefix "_write"
145
146 getline prev_read < prev_read_file
147 getline prev_write < prev_write_file
148
149 diff_read = (curr_read - prev_read ) / bytes_per_unit
150 diff_written = (curr_write - prev_write) / bytes_per_unit
151
152 print curr_read > prev_read_file
153 print curr_write > prev_write_file
154
155 io_stat = sprintf("%0.3f▲ %0.3f▼", diff_written, diff_read)
156 if (interface ~ "^w") {
157 label = label ":" wifi_conn
158 }
159 } else {
160 io_stat = "--"
161 }
162 number_of_interfaces_to_show = \
163 split(\
164 prefixes_of_interfaces_to_show,\
165 array_of_prefixes_of_interfaces_to_show,\
166 " +"\
167 )
168 for (n = 1; n <= number_of_interfaces_to_show; n++) {
169 prefix = array_of_prefixes_of_interfaces_to_show[n]
170 if (interface ~ ("^" prefix)) {
171 if (++count_printed > 1) {
172 sep = " "
173 } else {
174 sep = ""
175 }
176 printf("%s%s:%s", sep, label, io_stat)
177 }
178 }
179 }
180 }'
181 )
182
183 energy=$(
184 upower -e \
185 | grep battery \
186 | xargs upower -i \
187 | awk '
188 /^ +percentage: +/ {percentage=$2}
189 /^ +state: +/ {state=$2}
190 END {
191 if (state == "discharging") {
192 direction_of_change = "<"
193 } else if (state == "charging") {
194 direction_of_change = ">"
195 } else {
196 direction_of_change = "="
197 };
198 printf("%s%s", direction_of_change, percentage)
199 }')
200
201 datetime=$(date +'%a %b %d %H:%M:%S')
202 #datetime=$(
203 # date +'%a %u %b %d %H:%M:%S' \
204 # | awk '
205 # {
206 # wday_name = $1
207 # wday_seq = $2
208 # month = $3
209 # mday = $4
210 # time = $5
211 #
212 # week = ""
213 # for (i=1; i<=7; i++) {
214 # if (i == 6 || i == 4) {
215 # sep = " "
216 # } else {
217 # sep = ""
218 # }
219 #
220 # if (i == wday_seq) {
221 # #symbol = substr(wday_name, 1, 1)
222 # symbol = "/"
223 # } else if (i < wday_seq){
224 # symbol = "X"
225 # } else {
226 # symbol = "_"
227 # }
228 # week = week sep symbol
229 # }
230 #
231 # print "["week"]", month, mday, time;
232 # }
233 # '
234 #)
235
236 #volume_amixer=$(
237 # amixer get Master \
238 # | tail -1 \
239 # | awk '
240 # {
241 # level = $4;
242 # sub("^\\[", "", level);
243 # sub("\\]$", "", level);
244 # print level;
245 # }' \
246 # )
247
248 #volume_amixer=$(
249 # amixer get Master \
250 # | tail -n 1 \
251 # | awk '{print $4}' \
252 # | tr -d '[]'
253 #)
254
255 volume_pactl=$(
256 pactl list sinks \
257 | awk '
258 /^\tMute:/ {
259 printf("%s,", $0);
260 }
261 /^\tVolume:/ {
262 for (i=2; i<=NF; i++) printf(" %s", $i);
263 }' \
264 | awk -v RS=',' '
265 /^[ \t]*Mute:/ {mute = $2}
266 /^[ \t]*front-left:/ {left = $4}
267 /^[ \t]*front-right:/ {right = $4}
268 END {
269 if (mute == "yes") {
270 printf("x")
271 } else {
272 printf("%s %s", left, right)
273 }
274 }
275 '
276 )
277
278 volume="($volume_pactl)"
279
280 screen_brightness=$(
281 max=$(cat /sys/class/backlight/"$SCREEN_DEVICE"/max_brightness)
282 cur=$(cat /sys/class/backlight/"$SCREEN_DEVICE"/brightness)
283 awk -v max=$max -v cur=$cur 'BEGIN {printf("%d%%", cur/max*100)}'
284 )
285
286 #bluetooth_status=$(
287 # grep '^status:' /proc/acpi/ibm/bluetooth \
288 # | awk '
289 # $2 == "disabled" {printf "off"}
290 # $2 == "enabled" {printf "on"}
291 # '
292 #)
293
294 bluetooth_power=$(
295 echo -e 'show \n quit' \
296 | bluetoothctl \
297 | awk '
298 /^Controller / {
299 controller = $2;
300 controllers[++ctrl_count] = controller;
301 }
302 /^\t[A-Z][A-Za-z]+:/ {
303 key = $1;
304 sub(":$", "", key);
305 val = $2;
306 for (i=3; i<=NF; i++) {
307 val = val " " $i};
308 data[controller, key] = val;
309 }
310 END {
311 # Using the 1st seen controller. Should we select specific instead?
312 power_status = data[controllers[1], "Powered"];
313 if (ctrl_count > 0) {
314 if (power_status == "no") {
315 power_status = "off"
316 } else if (power_status == "yes") {
317 power_status = "on"
318 } else {
319 printf("Unexpected bluetooth power status: %s\n", power_status)\
320 > "/dev/stderr";
321 power_status = "ERROR"
322 }
323 } else {
324 power_status = "off" # TODO: Perhaps use differentiated marker?
325 }
326 printf("%s", power_status);
327 }'
328 )
329
330 #touchpad_status=$(
331 # xinput list-props 12 \
332 # | awk '
333 # /^\tDevice Enabled \([0-9]+\):/ {
334 # status = $4;
335 # printf("%s", status);
336 # }'
337 #)
338
339 #color_off='\033[0m'
340 #color_on_bg_gray='\033[m\033[40m'
341
342 energy_direction=$(echo "$energy" | cut -b 1)
343 energy_percentage=$(echo "$energy" | tr -d '<>=%')
344 if [[ "$energy_direction" = '<' ]]
345 then
346 if [[ $energy_percentage -le 5 ]]
347 then
348 DISPLAY=:0.0 notify-send \
349 -u critical \
350 "Energy CRITICALLY low: $energy" \
351 'CHARGE NOW!!! GO GO GO!!!'
352 elif [[ $energy_percentage -le 10 ]]
353 then
354 DISPLAY=:0.0 notify-send \
355 -u critical \
356 "Energy VERY low: $energy" \
357 'Plug it in ASAP.'
358 elif [[ $energy_percentage -le 15 ]]
359 then
360 DISPLAY=:0.0 notify-send \
361 -u critical \
362 "Energy low: $energy" \
363 'Get the charger.'
364 elif [[ $energy_percentage -le 50 ]]
365 then
366 if [[ ! -a "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF" ]]
367 then
368 DISPLAY=:0.0 notify-send \
369 -u normal \
370 "Energy bellow half: $energy" \
371 'Where is the charger?'
372 touch "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF"
373 fi
374 fi
375 else
376 rm -f "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF"
377 fi
378
379 weather=$(awk 'NR == 1 {printf("%s°F", $1)}' ~/var/run/metar-KJFK-decoded-temp-fahrenheit)
380
381 #signal_last_msg_age=$(
382 # ls -lt --time-style=+%s $HOME/var/lib/signal/latest_message.json \
383 # | awk -v now_seconds=$(date +%s) \
384 # '{
385 # mtime_seconds = $6;
386 # seconds = now_seconds - mtime_seconds;
387 # minutes = seconds / 60;
388 # hours = minutes / 60;
389 # days = hours / 24;
390 # weeks = days / 7;
391 # months = days / 30;
392 # #fmt = "%.1f";
393 # fmt = "%d";
394 # printf(fmt " s\n", seconds);
395 # printf(fmt " m\n", minutes);
396 # printf(fmt " h\n", hours);
397 # printf(fmt " d\n", days);
398 # printf(fmt " w\n", weeks);
399 # printf(fmt " mo\n", months);
400 # }' \
401 # | awk '$1 >= 1' \
402 # | sort -n -k 1 \
403 # | head -1 \
404 # | tr -d ' '
405 #)
406
407 mpd_currentsong=$(
408 echo 'currentsong' \
409 | nc 127.0.0.1 6600 \
410 | awk -v max_chars=10 '
411 /^OK/ {
412 next
413 }
414
415 {
416 key = $1
417 val = $2
418 for (i=3; i<=NF; i++) {val = val " " $i}
419 data[key] = val
420 }
421
422 END {
423 name = data["Name:"]
424 title = data["Title:"]
425 file = data["file:"]
426
427 if (name) {
428 out = name
429 } else if (title) {
430 out = title
431 } else if (file) {
432 last = split(file, parts, "/")
433 out = parts[last]
434 } else {
435 out = ""
436 }
437
438 printf("%s", substr(out, 1, max_chars))
439 }
440 '
441 )
442
443 mpd_state=$(
444 echo 'status' \
445 | nc 127.0.0.1 6600 \
446 | awk \
447 -v current_song="$mpd_currentsong" \
448 '
449 {
450 status[$1] = $2
451 }
452
453 /^time: +[0-9]+:[0-9]+$/ {
454 split($2, time, ":")
455 seconds_current = time[1]
456 seconds_total = time[2]
457
458 hours = int(seconds_current / 60 / 60);
459 secs_beyond_hours = seconds_current - (hours * 60 * 60);
460 mins = int(secs_beyond_hours / 60);
461 secs = secs_beyond_hours - (mins * 60);
462 if (hours > 0) {
463 current_time = sprintf("%d:%.2d:%.2d", hours, mins, secs)
464 } else {
465 current_time = sprintf("%.2d:%.2d", mins, secs)
466 }
467
468 if (seconds_total > 0) {
469 time_percentage = (seconds_current / seconds_total) * 100
470 current_percentage = sprintf("%d%%", time_percentage)
471 } else {
472 current_percentage = "~"
473 }
474 }
475
476 function print_known_state(symbol) {
477 printf(\
478 "%s %s %s %s",
479 symbol, current_time, current_percentage, current_song \
480 )
481 }
482
483 function print_unknown_state(symbol) {
484 printf("%s", symbol)
485 }
486
487 END {
488 state = status["state:"]
489
490 if (state == "play") {
491 print_known_state("▶")
492 } else if (state == "pause") {
493 print_known_state("❚❚")
494 } else if (state == "stop") {
495 print_known_state("⬛")
496 } else {
497 print_unknown_state("--")
498 }
499 }
500 '
501 )
502
503 #graphics_card=$(
504 #nvidia-smi \
505 #--format=csv,noheader,nounits \
506 #--query-gpu=memory.total,memory.used,temperature.gpu \
507 #| awk -F ',' '
508 #{
509 #mem_total = $1;
510 #mem_used = $2;
511 #temp = $3;
512 #mem_used_percent = (100 * mem_used) / mem_total;
513 #printf("[%d%% %dC]", mem_used_percent, temp);
514 #}
515 #'
516 #)
517
518 echo \
519 "\
520 E$energy\
521 \
522 M$memory\
523 \
524 C=[$load ${temp}°C ${fan}rpm]\
525 \
526 D$disk\
527 \
528 N:[$network]\
529 \
530 B:$bluetooth_power\
531 \
532 *$screen_brightness\
533 \
534 $volume\
535 \
536 [$mpd_state]\
537 \
538 $weather\
539 \
540 $datetime \
541 "
This page took 0.156354 seconds and 4 git commands to generate.