X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=bin%2Fkhatus_bar;h=b1e9e3602764dd200d37137e120d05882035b61b;hb=ab9fe663178272812f83e496c428813e3611368f;hp=e6fd1ffe2172abc9b5eef353a90b2964871d851b;hpb=171acde37ae95b9a5a130f06efdb5037ad85ee7e;p=khatus.git diff --git a/bin/khatus_bar b/bin/khatus_bar index e6fd1ff..b1e9e36 100755 --- a/bin/khatus_bar +++ b/bin/khatus_bar @@ -3,7 +3,7 @@ # Naming convention: # Variables: # - global, builtin : ALLCAPS -# - global, public : CamelCase +# - global, public : Camel_Snake_Man_Bear_Pig # - global, private : _snake_case_prefixed_underscore # - local : snake_case # Functions: @@ -13,6 +13,7 @@ BEGIN { FS = msg_fs ? msg_fs : "|" OFS = msg_fs ? msg_fs : "|" Kfs = key_fs ? key_fs : ":" + GC_Interval = GC_Interval ? GC_Interval : 3600 # seconds _total_to_diff["khatus_sensor_net_addr_io", "bytes_read" ] = 1 _total_to_diff["khatus_sensor_net_addr_io", "bytes_written" ] = 1 @@ -44,6 +45,8 @@ $1 == "OK" { $1 == "OK" && \ $2 == "khatus_sensor_datetime" { + # Code for make_status_bar definition is expected to be passed as an + # additional source file, using -f flag. print_msg_ok("status_bar", make_status_bar()) } @@ -65,7 +68,7 @@ function cache_update( src, key, val, len_line, len_head, len_val, time) { _cache[src, key] = val time = cache_get_time() _cache_mtime[src, key] = time - if (time % 3600 == 0) { + if (time % GC_Interval == 0) { cache_gc() } } @@ -96,14 +99,22 @@ function cache_get_time( src, key, time) { return time } -function cache_gc( src_and_key, unused_for) { +function cache_gc( src_and_key, parts, src, key, unused_for) { for (src_and_key in _cache) { - unused_for = cache_get_time() - _cache_atime[src_and_key] - if (unused_for > 3600) { + split(src_and_key, parts, SUBSEP) + src = parts[1] + key = parts[2] + val = _cache[src, key] + unused_for = cache_get_time() - _cache_atime[src, key] + if (unused_for > GC_Interval) { print_msg_info(\ - "cache_gc", "Deleting unused src_and_key: " src_and_key \ + "cache_gc", + sprintf(\ + "Deleting unused data SRC=%s KEY=%s VAL=%s", + src, key, val\ + ) \ ) - delete _cache[src_and_key] + delete _cache[src, key] } } } @@ -132,29 +143,6 @@ function cache_maybe_scale(src, key, val, key_parts) { # Status bar # ----------------------------------------------------------------------------- -function make_status_bar( position, bar, sep, i, j) { - position[++i] = "" - position[++i] = make_status_energy() - position[++i] = make_status_mem() - position[++i] = make_status_cpu() - position[++i] = make_status_disk() - position[++i] = make_status_net() - position[++i] = make_status_bluetooth() - position[++i] = make_status_screen_brightness() - position[++i] = make_status_volume() - position[++i] = make_status_mpd() - position[++i] = make_status_weather() - position[++i] = make_status_datetime() - position[++i] = "" - bar = "" - sep = "" - for (j = 1; j <= i; j++) { - bar = bar sep position[j] - sep = " " - } - return bar -} - function make_status_energy( state, charge, direction_of_change) { cache_get(state , "khatus_sensor_energy", "battery_state" , 0) cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0) @@ -186,25 +174,41 @@ function make_status_mem( total, used, percent, status) { return sprintf("M=%s", status) } +function make_status_procs() { + src = "khatus_sensor_procs" + all = cache_get_fmt_def(src, "total_procs" , 15, "%d") + r = cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0") + d = cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0") + t = cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0") + i = cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0") + z = cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0") + return sprintf("P=[%s %sr %sd %st %si %sz]", all, r, d, t, i, z) +} + function make_status_cpu( l, t, f) { - l = cache_get_fmt_def("khatus_sensor_loadavg" , "load_avg_1min", 5, "%4.2f") - t = cache_get_fmt_def("khatus_sensor_temperature", "temp_c" , 5, "%d" ) - f = cache_get_fmt_def("khatus_sensor_fan" , "speed" , 5, "%4d" ) + l_src = "khatus_sensor_loadavg" + t_src = "khatus_sensor_temperature" + f_src = "khatus_sensor_fan" + l = cache_get_fmt_def(l_src, "load_avg_1min", 5, "%4.2f") + t = cache_get_fmt_def(t_src, "temp_c" , 5, "%d" ) + f = cache_get_fmt_def(f_src, "speed" , 5, "%4d" ) return sprintf("C=[%s %s°C %srpm]", l, t, f) } -function make_status_disk( u, w, r) { - u = cache_get_fmt_def("khatus_sensor_disk_space", "disk_usage_percentage", 10, "%s") - w = cache_get_fmt_def("khatus_sensor_disk_io" , "sectors_written" , 5, "%0.3f") - r = cache_get_fmt_def("khatus_sensor_disk_io" , "sectors_read" , 5, "%0.3f") +function make_status_disk( u, w, r, src_u, src_io) { + src_u = "khatus_sensor_disk_space" + src_io = "khatus_sensor_disk_io" + u = cache_get_fmt_def(src_u , "disk_usage_percentage", 10, "%s") + w = cache_get_fmt_def(src_io, "sectors_written" , 5, "%0.3f") + r = cache_get_fmt_def(src_io, "sectors_read" , 5, "%0.3f") return sprintf("D=[%s%% %s▲ %s▼]", u, w, r) } function make_status_net( \ number_of_net_interfaces_to_show, \ net_interfaces_to_show, \ - sensor_io, \ - sensor_wi, \ + io, \ + wi, \ i, \ interface, \ label, \ @@ -217,21 +221,21 @@ function make_status_net( \ sep \ ) { number_of_net_interfaces_to_show = \ - split(opt_net_interfaces_to_show, net_interfaces_to_show, ",") - sensor_io = "khatus_sensor_net_addr_io" - sensor_wi = "khatus_sensor_net_wifi_status" + split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",") + io = "khatus_sensor_net_addr_io" + wi = "khatus_sensor_net_wifi_status" out = "" sep = "" for (i = number_of_net_interfaces_to_show; i > 0; i--) { interface = net_interfaces_to_show[i] label = substr(interface, 1, 1) if (interface ~ "^w") { - wifi = cache_get_fmt_def(sensor_wi, "status" Kfs interface, 10, "%s") + wifi = cache_get_fmt_def(wi, "status" Kfs interface, 10, "%s") label = label ":" wifi } - addr = cache_get_fmt_def(sensor_io, "addr" Kfs interface, 5, "%s", "") - w = cache_get_fmt_def(sensor_io, "bytes_written" Kfs interface, 5, "%0.3f") - r = cache_get_fmt_def(sensor_io, "bytes_read" Kfs interface, 5, "%0.3f") + addr = cache_get_fmt_def(io, "addr" Kfs interface, 5, "%s", "") + w = cache_get_fmt_def(io, "bytes_written" Kfs interface, 5, "%0.3f") + r = cache_get_fmt_def(io, "bytes_read" Kfs interface, 5, "%0.3f") io_stat = addr ? sprintf("%s▲ %s▼", w, r) : "--" out = out sep label ":" io_stat sep = " " @@ -251,25 +255,22 @@ function make_status_screen_brightness( src, key) { return sprintf("*%s%%", cache_get_fmt_def(src, key, 5, "%d")) } -function make_status_volume( sink, mute, vol_l, vol_r, show) { - sink = opt_pulseaudio_sink - cache_get(mute , "khatus_sensor_volume", "mute" Kfs sink, 5) - cache_get(vol_l, "khatus_sensor_volume", "vol_left" Kfs sink, 5) - cache_get(vol_r, "khatus_sensor_volume", "vol_right" Kfs sink, 5) - - if (!mute["is_expired"] && !vol_l["is_expired"] && !vol_r["is_expired"]) { - if (mute["value"] == "yes") {show = "X"} - else if (mute["value"] == "no") {show = vol_l["value"] " " vol_r["value"]} +function make_status_volume( sink, mu, vl, vr, show) { + sink = Opt_Pulseaudio_Sink + cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5) + cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5) + cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5) + show = "--" + if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) { + if (mu["value"] == "yes") {show = "X"} + else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]} else { print_msg_error(\ "make_status_volume", \ - "Unexpected value for 'mute' field: " mute["value"] \ + "Unexpected value for 'mute' field: " mu["value"] \ ) } - } else { - show = "--" } - return sprintf("(%s)", show) } @@ -301,13 +302,14 @@ function make_status_mpd_state_known(symbol, s, song, time, percentage) { song = cache_get_fmt_def(s, "song" , 5, "%s", "?") time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?") percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?") - song = substr(song, 1, opt_mpd_song_max_chars) + song = substr(song, 1, Opt_Mpd_Song_Max_Chars) return sprintf("%s %s %s %s", symbol, time, percent, song) } -function make_status_weather( hour, t_f) { +function make_status_weather( src, hour, t_f) { + src = "khatus_sensor_weather" hour = 60 * 60 - t_f = cache_get_fmt_def("khatus_sensor_weather", "temperature_f", 3 * hour, "%d") + t_f = cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d") return sprintf("%s°F", t_f) }