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