Move (most of) bar formatting to the user
authorSiraaj Khandkar <siraaj@khandkar.net>
Thu, 30 Aug 2018 14:42:43 +0000 (10:42 -0400)
committerSiraaj Khandkar <siraaj@khandkar.net>
Thu, 30 Aug 2018 14:42:43 +0000 (10:42 -0400)
bin/khatus_gen_bar_make_status
sanity_check
src/awk/exe/bar.awk

index 00e5506..7c34148 100755 (executable)
@@ -1,39 +1,74 @@
 #! /usr/bin/awk -f
 
 BEGIN {
-    aliases["@energy"]    = "bar_make_status_energy()"
-    aliases["@memory"]    = "bar_make_status_mem()"
-    aliases["@processes"] = "bar_make_status_procs()"
-    aliases["@cpu"]       = "bar_make_status_cpu()"
-    aliases["@disk"]      = "bar_make_status_disk()"
-    aliases["@net"]       = "bar_make_status_net()"
-    aliases["@bluetooth"] = "bar_make_status_bluetooth()"
-    aliases["@backlight"] = "bar_make_status_screen_brightness()"
-    aliases["@volume"]    = "bar_make_status_volume()"
-    aliases["@mpd"]       = "bar_make_status_mpd()"
-    aliases["@weather"]   = "bar_make_status_weather()"
-    aliases["@datetime"]  = "bar_make_status_datetime()"
-    out = "function bar_make_status(    position, bar, sep, i, j) {\n"
-    n = split(Status_Bar, s, ",")
-    for (i=1; i<=n; i++) {
-        alias = s[i]
-        function_call = aliases[alias]
+    aliases["@energy_percent"]      = "bar_make_status_energy_percent()"
+    aliases["@energy_direction"]    = "bar_make_status_energy_direction()"
+
+    aliases["@memory_percent"]      = "bar_make_status_mem_percent()"
+
+    aliases["@processes_count_all"] = "bar_make_status_procs_count_all()"
+    aliases["@processes_count_r"]   = "bar_make_status_procs_count_r()"
+    aliases["@processes_count_d"]   = "bar_make_status_procs_count_d()"
+    aliases["@processes_count_t"]   = "bar_make_status_procs_count_t()"
+    aliases["@processes_count_i"]   = "bar_make_status_procs_count_i()"
+    aliases["@processes_count_z"]   = "bar_make_status_procs_count_z()"
+
+    aliases["@cpu_loadavg"]         = "bar_make_status_cpu_loadavg()"
+    aliases["@cpu_temp"]            = "bar_make_status_cpu_temperature()"
+    aliases["@cpu_fan_speed"]       = "bar_make_status_cpu_fan_speed()"
+
+    aliases["@disk_space"]          = "bar_make_status_disk_space()"
+    aliases["@disk_io_w"]           = "bar_make_status_disk_io_w()"
+    aliases["@disk_io_r"]           = "bar_make_status_disk_io_r()"
+
+    aliases["@net_addr"]            = "bar_make_status_net_addr(\"%s\")"
+     params["@net_addr"]            = 1
+    aliases["@net_io_w"]            = "bar_make_status_net_io_w(\"%s\")"
+     params["@net_io_w"]            = 1
+    aliases["@net_io_r"]            = "bar_make_status_net_io_r(\"%s\")"
+     params["@net_io_r"]            = 1
+    aliases["@net_wifi"]            = "bar_make_status_net_wifi(\"%s\")"
+     params["@net_wifi"]            = 1
+
+    aliases["@bluetooth_power"]     = "bar_make_status_bluetooth_power()"
+
+    aliases["@backlight_percent"]   = "bar_make_status_backlight_percent()"
+
+    aliases["@volume_pa_sink"]      = "bar_make_status_volume_pulseaudio_sink(%d)"
+     params["@volume_pa_sink"]      = 1
+
+    aliases["@mpd"]                 = "bar_make_status_mpd()"
+
+    aliases["@weather_temp_f"]      = "bar_make_status_weather_temp_f()"
+
+    aliases["@datetime"]            = "bar_make_status_datetime()"
+
+    out = "function bar_make_status() {\n"
+    n_args = split(Status_Args, args_arr, ",")
+    for (i=1; i<=n_args; i++) {
+        arg = args_arr[i]
+        split(arg, arg_parts, ":")
+        alias = arg_parts[1]
+        n_expected_params = params[alias]
+        if (n_expected_params == 0) {
+            function_call = aliases[alias]
+        } else if (n_expected_params == 1) {
+            function_call = sprintf(aliases[alias], arg_parts[2])
+        } else {
+            printf(\
+                "Unsupported number of params: %d in %s\n",
+                n_expected_params, alias) \
+                > "/dev/stderr"
+            exit(1)
+        }
         if (function_call) {
-            out = out "        position[++i] = " function_call ";\n"
+            args_str = args_str ", " function_call
         } else {
             printf("Unexpected status bar component alias: \"%s\"\n", alias) \
                 > "/dev/stderr"
             exit(1)
         }
     }
-    out = out "\
-        bar = \"\";\
-        sep = \"\";\
-        for (j = 1; j <= i; j++) {\
-           bar = bar sep position[j];\
-           sep = \" \";\
-        }\
-        return bar;\
-    }";
+    out = out "    return sprintf(\"" Status_Fmt "\"" args_str ");\n}";
     print out
 }
index ed7f574..09fc142 100755 (executable)
@@ -6,11 +6,11 @@
 | stdbuf -o L tee \
     >(stdbuf -o L ./bin/khatus_bar \
         -v Opt_Mpd_Song_Max_Chars=10 \
-        -v Opt_Net_Interfaces_To_Show=wlp3s0 \
         -v Opt_Pulseaudio_Sink=0 \
         -v GC_Interval=300 \
         -f <(./bin/khatus_gen_bar_make_status \
-                -v Status_Bar='@energy,@memory,@processes,@cpu,@disk,@net,@bluetooth,@backlight,@volume,@mpd,@weather,@datetime' \
+                -v Status_Fmt=' E=%s%% M=%d%% P=[%s %sr %sd %st %si %sz] C=[%s %s°C %srpm] D=[%s%% %s▲ %s▼] W=[%s %s▲ %s▼] B=%s *=%s%% (%s) [%s] %s°F %s ' \
+                -v Status_Args='@energy_percent,@memory_percent,@processes_count_all,@processes_count_r,@processes_count_d,@processes_count_t,@processes_count_i,@processes_count_z,@cpu_loadavg,@cpu_temp,@cpu_fan_speed,@disk_space,@disk_io_w,@disk_io_r,@net_wifi:wlp3s0,@net_io_w:wlp3s0,@net_io_r:wlp3s0,@bluetooth_power,@backlight_percent,@volume_pa_sink:0,@mpd,@weather_temp_f,@datetime' \
             ) \
     ) \
     >(stdbuf -o L ./bin/khatus_monitor_energy) \
index d3deefe..9be1adb 100755 (executable)
@@ -26,15 +26,8 @@ $2 == "khatus_sensor_datetime" {
 # Energy
 # -----------------------------------------------------------------------------
 
-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_percent() {
+    return cache_get_fmt_def("khatus_sensor_energy", "battery_percentage", 0, "%d")
 }
 
 function bar_make_status_energy_direction(    state, direction_of_change) {
@@ -53,10 +46,6 @@ function bar_make_status_energy_direction(    state, direction_of_change) {
 # Memory
 # -----------------------------------------------------------------------------
 
-function bar_make_status_mem() {
-    return sprintf("M=%s%%", bar_make_status_mem_percent())
-}
-
 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)
@@ -76,29 +65,18 @@ function bar_make_status_mem_percent(    total, used, percent, percent_str) {
 # -----------------------------------------------------------------------------
 # Processes
 # -----------------------------------------------------------------------------
-
-function bar_make_status_procs() {
-    # From man ps:
-    #   D    uninterruptible sleep (usually IO)
-    #   R    running or runnable (on run queue)
-    #   S    interruptible sleep (waiting for an event to complete)
-    #   T    stopped by job control signal
-    #   t    stopped by debugger during the tracing
-    #   W    paging (not valid since the 2.6.xx kernel)
-    #   X    dead (should never be seen)
-    #   Z    defunct ("zombie") process, terminated but not reaped by its parent
-    #
-    # Additionally, not documented in ps man page:
-    #   I    Idle
-    #
-    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)
-}
+# From man ps:
+#   D    uninterruptible sleep (usually IO)
+#   R    running or runnable (on run queue)
+#   S    interruptible sleep (waiting for an event to complete)
+#   T    stopped by job control signal
+#   t    stopped by debugger during the tracing
+#   W    paging (not valid since the 2.6.xx kernel)
+#   X    dead (should never be seen)
+#   Z    defunct ("zombie") process, terminated but not reaped by its parent
+#
+# Additionally, not documented in ps man page:
+#   I    Idle
 
 function bar_make_status_procs_count_all() {
     return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d")
@@ -133,13 +111,6 @@ function bar_make_status_procs_count_z(    src) {
 # CPU
 # -----------------------------------------------------------------------------
 
-function bar_make_status_cpu(    l, t, f) {
-    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_cpu_loadavg(    src) {
     src = "khatus_sensor_loadavg"
     return cache_get_fmt_def(src, "load_avg_1min", 5, "%4.2f")
@@ -157,13 +128,6 @@ function bar_make_status_cpu_fan_speed() {
 # 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")
@@ -183,37 +147,6 @@ function bar_make_status_disk_io_r(    src) {
 # Network
 # -----------------------------------------------------------------------------
 
-function bar_make_status_net(    \
-    number_of_net_interfaces_to_show, \
-    net_interfaces_to_show, \
-    i, \
-    interface, \
-    label, \
-    addr, \
-    w, \
-    r, \
-    out, \
-    sep \
-) {
-    number_of_net_interfaces_to_show = \
-        split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",")
-    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") {
-            label = label ":" bar_make_status_net_wifi(interface)
-        }
-        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_net_addr(interface,    src) {
     src = "khatus_sensor_net_addr_io"
     return cache_get_fmt_def(src, "addr" Kfs interface, 5, "%s", "")
@@ -238,23 +171,15 @@ function bar_make_status_net_wifi(interface,    src) {
 # 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"
     return cache_get_fmt_def(src, "power_status", 10, "%s")
 }
 
 # -----------------------------------------------------------------------------
-# Backlight
+# Backlight (screen brightness)
 # -----------------------------------------------------------------------------
 
-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"
     return cache_get_fmt_def(src, "percentage", 5, "%d")
@@ -264,13 +189,7 @@ function bar_make_status_backlight_percent(    src) {
 # 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
+function bar_make_status_volume_pulseaudio_sink(sink,    mu, vl, vr, show) {
     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)
@@ -280,7 +199,7 @@ function bar_make_status_volume_value(    sink, mu, vl, vr, show) {
         else if (mu["value"] == "no")  {show = vl["value"] " " vr["value"]}
         else {
             msg_out_error(\
-                "bar_make_status_volume", \
+                "bar_make_status_volume_pulseaudio_sink: " sink ". ", \
                 "Unexpected value for 'mute' field: " mu["value"] \
             )
         }
@@ -292,11 +211,7 @@ function bar_make_status_volume_value(    sink, mu, vl, vr, show) {
 # MPD
 # -----------------------------------------------------------------------------
 
-function bar_make_status_mpd() {
-    return sprintf("[%s]", bar_make_status_mpd_value())
-}
-
-function bar_make_status_mpd_value(    state, status) {
+function bar_make_status_mpd(    state, status) {
     cache_get(state, "khatus_sensor_mpd", "state", 5)
     if (!state["is_expired"] && state["value"]) {
         if (state["value"] == "play") {
@@ -331,10 +246,6 @@ function bar_make_status_mpd_state_known(symbol,    s, song, time, percentage) {
 # 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
This page took 0.043197 seconds and 4 git commands to generate.