7 df
--output=pcent
"$disk_space_device" |
awk 'NR == 2 {print $1}'
10 produce_net_addr_io
() {
13 -v prefixes_of_interfaces_to_show
="$PREFIXES_OF_INTERFACES_TO_SHOW" \
16 bytes_per_unit = 1024 * 1024
24 interfaces[sequence] = interface
28 sub("/[0-9]+", "", $2)
30 addrs[interface] = addr
33 /^ +RX: / {transfer_direction = "r"}
34 /^ +TX: / {transfer_direction = "w"}
36 /^ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ +[0-9]+ *$/ {
37 io[interface, transfer_direction] = $1;
41 for (seq=1; seq<=sequence; seq++) {
42 interface = interfaces[seq]
43 label = substr(interface, 1, 1)
44 if (addrs[interface]) {
45 curr_read = io[interface, "r"]
46 curr_write = io[interface, "w"]
47 print(interface, addrs[interface], curr_write, curr_read)
55 produce_net_wifi_status
() {
57 -f ACTIVE
,SSID
,SIGNAL \
63 BEGIN {wifi_status = "--"}
64 $1 == "yes" {wifi_status = $2 ":" $3 "%"}
65 END {print wifi_status}
69 produce_bluetooth_power
() {
70 echo -e 'show \n quit' \
75 controllers[++ctrl_count] = controller;
77 /^\t[A-Z][A-Za-z]+:/ {
81 for (i=3; i<=NF; i++) {
83 data[controller, key] = val;
86 # Using the 1st seen controller. Should we select specific instead?
87 power_status = data[controllers[1], "Powered"];
89 if (power_status == "no") {
91 } else if (power_status == "yes") {
94 printf("Unexpected bluetooth power status: %s\n", power_status)\
96 power_status = "ERROR"
99 power_status = "off" # TODO: Perhaps use differentiated marker?
101 printf("%s\n", power_status);
105 produce_screen_brightness
() {
106 screen_brightness_device_path
="$1"
108 $(cat $screen_brightness_device_path/max_brightness) \
109 $(cat $screen_brightness_device_path/brightness)\
120 for (i=2; i<=NF; i++) printf(" %s", $i);
123 /^[ \t]*Mute:/ {mute = $2}
124 /^[ \t]*front-left:/ {left = $4}
125 /^[ \t]*front-right:/ {right = $4}
130 print("%s %s\n", left, right)
136 produce_mpd_state
() {
138 | nc
127.0.0.1 6600 \
144 /^time: +[0-9]+:[0-9]+$/ {
146 seconds_current = time[1]
147 seconds_total = time[2]
149 hours = int(seconds_current / 60 / 60);
150 secs_beyond_hours = seconds_current - (hours * 60 * 60);
151 mins = int(secs_beyond_hours / 60);
152 secs = secs_beyond_hours - (mins * 60);
154 current_time = sprintf("%d:%.2d:%.2d", hours, mins, secs)
156 current_time = sprintf("%.2d:%.2d", mins, secs)
159 if (seconds_total > 0) {
160 time_percentage = (seconds_current / seconds_total) * 100
161 current_percentage = sprintf("%d%%", time_percentage)
163 current_percentage = "~"
168 state = status["state:"]
170 if (state == "play") {
172 } else if (state == "pause") {
174 } else if (state == "stop") {
182 status["state:"], current_time, current_percentage\
190 | nc
127.0.0.1 6600 \
198 sub("^" key " +", "")
205 title = data["Title:"]
213 last = split(file, parts, "/")
224 weather_station_id
="$1"
225 metar
-d "$weather_station_id" 2>&1 \
227 /METAR pattern not found in NOAA data/ {
233 fahrenheit = (celsius * (9 / 5)) + 32;
234 temperature = fahrenheit
241 print temperature "°F"
246 date +'%a %b %d %H:%M:%S'
252 prefixes_of_net_interfaces_to_show
="$3"
255 -v opt_debug
="$debug" \
256 -v opt_mpd_song_max_chars
=10 \
257 -v opt_prefixes_of_net_interfaces_to_show
="$prefixes_of_net_interfaces_to_show" \
262 db["disk_space_used"] = msg_body
271 /^in:NET_WIFI_STATUS/\
274 db["net_wifi_status"] = msg_body
277 /^in:BLUETOOTH_POWER/\
280 db["bluetooth_power"] = msg_body
283 /^in:SCREEN_BRIGHTNESS/\
286 set_screen_brightness()
292 db["volume"] = msg_body
299 db["mpd_curr_song_time"] = $2
300 db["mpd_curr_song_percent"] = $3
306 db["mpd_curr_song_name"] = msg_body
312 db["weather_temperature"] = msg_body
318 db["datetime"] = msg_body
328 function set_net_addr_io( \
329 interface, address, io_curr_w, io_curr_r, io_prev_w, io_prev_r\
336 if (address && io_curr_w && io_curr_r) {
338 io_prev_w = net_io_curr_w[interface]
339 io_prev_r = net_io_curr_r[interface]
341 net_addr[interface] = address
342 net_io_curr_w[interface] = io_curr_w
343 net_io_curr_r[interface] = io_curr_r
344 net_io_diff_w[interface] = io_curr_w - io_prev_w
345 net_io_diff_r[interface] = io_curr_r - io_prev_r
348 net_addr[interface] = ""
349 net_io_curr_w[interface] = 0
350 net_io_curr_r[interface] = 0
351 net_io_diff_w[interface] = 0
352 net_io_diff_r[interface] = 0
357 function set_screen_brightness( max, cur) {
360 db["screen_brightness"] = (cur / max) * 100
363 function split_msg_parts() {
365 sub("^" msg_head " +", "")
367 debug(msg_head, msg_body)
370 function make_bar( position, bar, sep, i, j) {
371 position[++i] = sprintf("D=[%s]", db["disk_space_used"])
372 position[++i] = make_status_net()
373 position[++i] = sprintf("B=%s", db["bluetooth_power"])
374 position[++i] = sprintf("*%d%%", db["screen_brightness"])
375 position[++i] = sprintf("(%s)", db["volume"])
376 position[++i] = make_status_mpd()
377 position[++i] = db["weather_temperature"]
378 position[++i] = db["datetime"]
381 for (j = 1; j <= i; j++) {
382 bar = bar sep position[j]
388 function make_status_net( \
390 number_of_interfaces_to_show,
392 array_of_prefixes_of_interfaces_to_show,
403 number_of_interfaces_to_show = \
405 opt_prefixes_of_net_interfaces_to_show,\
406 array_of_prefixes_of_interfaces_to_show,\
409 for (n = 1; n <= number_of_interfaces_to_show; n++) {
410 prefix = array_of_prefixes_of_interfaces_to_show[n]
411 for (interface in net_addr) {
412 if (interface ~ ("^" prefix)) {
413 label = substr(interface, 1, 1)
414 if (net_addr[interface]) {
415 bytes_per_mb = 1024 * 1024 # TODO: option
416 dw = net_io_diff_w[interface] / bytes_per_mb
417 dr = net_io_diff_r[interface] / bytes_per_mb
418 io_stat = sprintf("%0.3f▲ %0.3f▼", dw, dr)
422 if (interface ~ "^w") {
423 label = label ":" db["net_wifi_status"]
425 if (++count_printed > 1) {
430 out = out sep label ":" io_stat
434 return sprintf("N[%s]", out)
437 function make_status_mpd( state, status) {
438 state = db["mpd_state"]
440 if (state == "play") {
441 status = make_status_mpd_state_known("▶")
442 } else if (state == "pause") {
443 status = make_status_mpd_state_known("❚❚")
444 } else if (state == "stop") {
445 status = make_status_mpd_state_known("⬛")
447 status = make_status_mpd_state_unknown("--")
450 return sprintf("[%s]", status)
453 function make_status_mpd_state_known(symbol) {
457 db["mpd_curr_song_time"],
458 db["mpd_curr_song_percent"],
459 substr(db["mpd_curr_song_name"], 1, opt_mpd_song_max_chars)\
463 function make_status_mpd_state_unknown(symbol) {
464 return sprintf("%s", symbol)
467 function debug(location, msg) {
469 print_error(location, msg)
473 function print_error(location, msg) {
474 print(location " ==> " msg) > "/dev/stderr"
489 $cmd |
while read line
; do
490 echo "${msg_head} $line" > "$pipe"
499 dir_data
="$HOME/.khatus"
500 weather_station_id
='KJFK'
501 screen_brightness_device_name
='acpi_video0'
502 prefixes_of_net_interfaces_to_show
='w' # comma-separated
503 disk_space_device
='/'
507 long_options
+='debug'
508 long_options
+=',data-dir:'
509 long_options
+=',weather-station:'
510 long_options
+=',screen-device:'
511 long_options
+=',prefixes_of_net_interfaces_to_show:'
512 long_options
+=',disk_space_device:'
532 weather_station_id
="$2"
536 screen_brightness_device_name
="$2"
539 --prefixes_of_net_interfaces_to_show)
540 prefixes_of_net_interfaces_to_show
="$2"
544 disk_space_device
="$2"
554 pipe
="$dir_data/khatus_data_pipe"
555 screen_brightness_device_path
='/sys/class/backlight'
556 screen_brightness_device_path
+="/$screen_brightness_device_name"
558 ( echo "Khatus starting with the following parameters:"
559 ( echo " debug|= $debug"
560 echo " dir_data|= $dir_data"
562 echo " screen_brightness_device_name|= $screen_brightness_device_name"
563 echo " screen_brightness_device_path|= $screen_brightness_device_path"
564 echo " weather_station_id|= $weather_station_id"
565 echo " prefixes_of_net_interfaces_to_show|= $prefixes_of_net_interfaces_to_show"
566 echo " disk_space_device|= $disk_space_device"
575 cmd_produce_screen_brightness
='produce_screen_brightness'
576 cmd_produce_screen_brightness
+=" $screen_brightness_device_path"
578 cmd_produce_weather
="produce_weather $weather_station_id"
580 cmd_produce_disk_space
="produce_disk_space $disk_space_device"
582 # TODO: Redirect each worker's stderr to a dedicated log file
583 spawn produce_datetime
"$pipe" 'in:DATE_TIME' 1
584 spawn
"$cmd_produce_screen_brightness" "$pipe" 'in:SCREEN_BRIGHTNESS' 1
585 spawn
"$cmd_produce_weather" "$pipe" 'in:WEATHER' $
(( 30 * 60 ))
586 spawn produce_mpd_state
"$pipe" 'in:MPD_STATE' 1
587 spawn produce_mpd_song
"$pipe" 'in:MPD_SONG' 1
588 spawn produce_volume
"$pipe" 'in:VOLUME' 1
589 spawn produce_bluetooth_power
"$pipe" 'in:BLUETOOTH_POWER' 5
590 spawn produce_net_wifi_status
"$pipe" 'in:NET_WIFI_STATUS' 5
591 spawn produce_net_addr_io
"$pipe" 'in:NET_ADDR_IO' 1
592 spawn
"$cmd_produce_disk_space" "$pipe" 'in:DISK_SPACE' 1
593 spawn produce_bar_req
"$pipe" 'out:BAR' 1
598 "$prefixes_of_net_interfaces_to_show"