Break out status component values from formatting
authorSiraaj Khandkar <siraaj@khandkar.net>
Wed, 29 Aug 2018 23:57:31 +0000 (19:57 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Wed, 29 Aug 2018 23:57:31 +0000 (19:57 -0400)
first step in giving user more fine-grained bar control

src/awk/exe/bar.awk

index 6862ee8..d3deefe 100755 (executable)
@@ -23,13 +23,22 @@ $2 == "khatus_sensor_datetime" {
 
 
 # -----------------------------------------------------------------------------
-# Status bar
+# Energy
 # -----------------------------------------------------------------------------
 
-function bar_make_status_energy(    state, charge, direction_of_change) {
-    cache_get(state , "khatus_sensor_energy", "battery_state"     , 0)
+function bar_make_status_energy(    d, p) {
+    d = bar_make_status_energy_direction()
+    p = bar_make_status_energy_percent()
+    return sprintf("E%s%d%%", d, p)
+}
+
+function bar_make_status_energy_percent(    charge) {
     cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0)
+    return charge["value"]
+}
 
+function bar_make_status_energy_direction(    state, direction_of_change) {
+    cache_get(state, "khatus_sensor_energy", "battery_state", 0)
     if (state["value"] == "discharging") {
         direction_of_change = "<"
     } else if (state["value"] == "charging") {
@@ -37,11 +46,18 @@ function bar_make_status_energy(    state, charge, direction_of_change) {
     } else {
         direction_of_change = "="
     }
+    return direction_of_change
+}
 
-    return sprintf("E%s%d%%", direction_of_change, charge["value"])
+# -----------------------------------------------------------------------------
+# Memory
+# -----------------------------------------------------------------------------
+
+function bar_make_status_mem() {
+    return sprintf("M=%s%%", bar_make_status_mem_percent())
 }
 
-function bar_make_status_mem(    total, used, percent, status) {
+function bar_make_status_mem_percent(    total, used, percent, percent_str) {
     cache_get(total, "khatus_sensor_memory", "total", 5)
     cache_get(used , "khatus_sensor_memory", "used" , 5)
     # Checking total["value"] to avoid division by zero when data is missing
@@ -50,13 +66,17 @@ function bar_make_status_mem(    total, used, percent, status) {
         total["value"] \
         ) {
         percent = util_round((used["value"] / total["value"]) * 100)
-        status = sprintf("%d%%", percent)
+        percent_str = sprintf("%d", percent)
     } else {
-        status = "__"
+        percent_str = "__"
     }
-    return sprintf("M=%s", status)
+    return percent_str
 }
 
+# -----------------------------------------------------------------------------
+# Processes
+# -----------------------------------------------------------------------------
+
 function bar_make_status_procs() {
     # From man ps:
     #   D    uninterruptible sleep (usually IO)
@@ -71,87 +91,185 @@ function bar_make_status_procs() {
     # Additionally, not documented in ps man page:
     #   I    Idle
     #
-    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")
+    all = bar_make_status_procs_count_all()
+    r   = bar_make_status_procs_count_r()
+    d   = bar_make_status_procs_count_d()
+    t   = bar_make_status_procs_count_t()
+    i   = bar_make_status_procs_count_i()
+    z   = bar_make_status_procs_count_z()
     return sprintf("P=[%s %sr %sd %st %si %sz]", all, r, d, t, i, z)
 }
 
+function bar_make_status_procs_count_all() {
+    return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d")
+}
+
+function bar_make_status_procs_count_r(    src) {
+    src = "khatus_sensor_procs"
+    return cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0")
+}
+
+function bar_make_status_procs_count_d(    src) {
+    src = "khatus_sensor_procs"
+    return cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0")
+}
+
+function bar_make_status_procs_count_t(    src) {
+    src = "khatus_sensor_procs"
+    return cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0")
+}
+
+function bar_make_status_procs_count_i(    src) {
+    src = "khatus_sensor_procs"
+    return cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0")
+}
+
+function bar_make_status_procs_count_z(    src) {
+    src = "khatus_sensor_procs"
+    return cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0")
+}
+
+# -----------------------------------------------------------------------------
+# CPU
+# -----------------------------------------------------------------------------
+
 function bar_make_status_cpu(    l, t, f) {
-    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"  )
+    l = bar_make_status_cpu_loadavg()
+    t = bar_make_status_cpu_temperature()
+    f = bar_make_status_cpu_fan_speed()
     return sprintf("C=[%s %s°C %srpm]", l, t, f)
 }
 
-function bar_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")
+function bar_make_status_cpu_loadavg(    src) {
+    src = "khatus_sensor_loadavg"
+    return cache_get_fmt_def(src, "load_avg_1min", 5, "%4.2f")
+}
+
+function bar_make_status_cpu_temperature() {
+    return cache_get_fmt_def("khatus_sensor_temperature", "temp_c", 5, "%d")
+}
+
+function bar_make_status_cpu_fan_speed() {
+    return cache_get_fmt_def("khatus_sensor_fan", "speed", 5, "%4d")
+}
+
+# -----------------------------------------------------------------------------
+# Disk
+# -----------------------------------------------------------------------------
+
+function bar_make_status_disk(    u, w, r) {
+    u = bar_make_status_disk_space()
+    w = bar_make_status_disk_io_w()
+    r = bar_make_status_disk_io_r()
     return sprintf("D=[%s%% %s▲ %s▼]", u, w, r)
 }
 
+function bar_make_status_disk_space(    src) {
+    src = "khatus_sensor_disk_space"
+    return cache_get_fmt_def(src, "disk_usage_percentage", 10, "%s")
+}
+
+function bar_make_status_disk_io_w(    src) {
+    src = "khatus_sensor_disk_io"
+    return cache_get_fmt_def(src, "sectors_written", 5, "%0.3f")
+}
+
+function bar_make_status_disk_io_r(    src) {
+    src = "khatus_sensor_disk_io"
+    return cache_get_fmt_def(src, "sectors_read", 5, "%0.3f")
+}
+
+# -----------------------------------------------------------------------------
+# Network
+# -----------------------------------------------------------------------------
+
 function bar_make_status_net(    \
     number_of_net_interfaces_to_show, \
     net_interfaces_to_show, \
-    io, \
-    wi, \
     i, \
     interface, \
     label, \
-    wifi, \
     addr, \
     w, \
     r, \
-    io_stat, \
     out, \
     sep \
 ) {
     number_of_net_interfaces_to_show = \
         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(wi, "status" Kfs interface, 10, "%s")
-            label = label ":" wifi
+            label = label ":" bar_make_status_net_wifi(interface)
         }
-        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
+        addr = bar_make_status_net_addr(interface)
+        w    = bar_make_status_net_io_w(interface)
+        r    = bar_make_status_net_io_r(interface)
+        out = out sep label ":" sprintf("%s▲ %s▼", w, r)
         sep = " "
     }
     return sprintf("N[%s]", out)
 }
 
-function bar_make_status_bluetooth(    src, key) {
+function bar_make_status_net_addr(interface,    src) {
+    src = "khatus_sensor_net_addr_io"
+    return cache_get_fmt_def(src, "addr" Kfs interface, 5, "%s", "")
+}
+
+function bar_make_status_net_io_w(interface,    src) {
+    src = "khatus_sensor_net_addr_io"
+    return cache_get_fmt_def(src, "bytes_written" Kfs interface, 5, "%0.3f")
+}
+
+function bar_make_status_net_io_r(interface,    src) {
+    src = "khatus_sensor_net_addr_io"
+    return cache_get_fmt_def(src, "bytes_read" Kfs interface, 5, "%0.3f")
+}
+
+function bar_make_status_net_wifi(interface,    src) {
+    src = "khatus_sensor_net_wifi_status"
+    return cache_get_fmt_def(src, "status" Kfs interface, 10, "%s")
+}
+
+# -----------------------------------------------------------------------------
+# Bluetooth
+# -----------------------------------------------------------------------------
+
+function bar_make_status_bluetooth() {
+    return sprintf("B=%s", bar_make_status_bluetooth_power())
+}
+
+function bar_make_status_bluetooth_power(    src) {
     src = "khatus_sensor_bluetooth_power"
-    key = "power_status"
-    return sprintf("B=%s", cache_get_fmt_def(src, key, 10, "%s"))
+    return cache_get_fmt_def(src, "power_status", 10, "%s")
 }
 
-function bar_make_status_screen_brightness(    src, key) {
+# -----------------------------------------------------------------------------
+# Backlight
+# -----------------------------------------------------------------------------
+
+function bar_make_status_screen_brightness() {
+    return sprintf("*%s%%", bar_make_status_backlight_percent())
+}
+
+function bar_make_status_backlight_percent(    src) {
     src = "khatus_sensor_screen_brightness"
-    key = "percentage"
-    return sprintf("*%s%%", cache_get_fmt_def(src, key, 5, "%d"))
+    return cache_get_fmt_def(src, "percentage", 5, "%d")
 }
 
-function bar_make_status_volume(    sink, mu, vl, vr, show) {
+# -----------------------------------------------------------------------------
+# Volume
+# -----------------------------------------------------------------------------
+
+function bar_make_status_volume() {
+    return sprintf("(%s)", bar_make_status_volume_value())
+}
+
+# TODO: better name than "bar_make_status_volume_value" ... :)
+function bar_make_status_volume_value(    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)
@@ -167,10 +285,18 @@ function bar_make_status_volume(    sink, mu, vl, vr, show) {
             )
         }
     }
-    return sprintf("(%s)", show)
+    return show
 }
 
-function bar_make_status_mpd(    state, status) {
+# -----------------------------------------------------------------------------
+# MPD
+# -----------------------------------------------------------------------------
+
+function bar_make_status_mpd() {
+    return sprintf("[%s]", bar_make_status_mpd_value())
+}
+
+function bar_make_status_mpd_value(    state, status) {
     cache_get(state, "khatus_sensor_mpd", "state", 5)
     if (!state["is_expired"] && state["value"]) {
         if (state["value"] == "play") {
@@ -189,8 +315,7 @@ function bar_make_status_mpd(    state, status) {
     } else {
         status = "--"
     }
-
-    return sprintf("[%s]", status)
+    return status
 }
 
 function bar_make_status_mpd_state_known(symbol,    s, song, time, percentage) {
@@ -202,13 +327,24 @@ function bar_make_status_mpd_state_known(symbol,    s, song, time, percentage) {
     return sprintf("%s %s %s %s", symbol, time, percent, song)
 }
 
-function bar_make_status_weather(    src, hour, t_f) {
+# -----------------------------------------------------------------------------
+# Weather
+# -----------------------------------------------------------------------------
+
+function bar_make_status_weather() {
+    return sprintf("%s°F", bar_make_status_weather_temp_f())
+}
+
+function bar_make_status_weather_temp_f(    src, hour) {
     src = "khatus_sensor_weather"
     hour = 60 * 60
-    t_f = cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
-    return sprintf("%s°F", t_f)
+    return cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
 }
 
+# -----------------------------------------------------------------------------
+# Datetime
+# -----------------------------------------------------------------------------
+
 function bar_make_status_datetime(    dt) {
     return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
 }
This page took 0.02484 seconds and 4 git commands to generate.