| 1 | # Naming convention: |
| 2 | # Variables: |
| 3 | # - global, builtin : ALLCAPS |
| 4 | # - global, public : Camel_Snake_Man_Bear_Pig |
| 5 | # - global, private : _snake_case_prefixed_underscore |
| 6 | # - local : snake_case |
| 7 | # Functions: |
| 8 | # - global, public : snake_case |
| 9 | |
| 10 | # ----------------------------------------------------------------------------- |
| 11 | # Input |
| 12 | # ----------------------------------------------------------------------------- |
| 13 | $1 == "OK" { |
| 14 | cache_update() |
| 15 | } |
| 16 | |
| 17 | $1 == "OK" && \ |
| 18 | $2 == "khatus_sensor_datetime" { |
| 19 | # Code for bar_make_status is expected to be passed as an |
| 20 | # additional source file, using -f flag. |
| 21 | msg_out_ok("status_bar", bar_make_status()) |
| 22 | } |
| 23 | |
| 24 | |
| 25 | # ----------------------------------------------------------------------------- |
| 26 | # Status bar |
| 27 | # ----------------------------------------------------------------------------- |
| 28 | |
| 29 | function bar_make_status_energy( state, charge, direction_of_change) { |
| 30 | cache_get(state , "khatus_sensor_energy", "battery_state" , 0) |
| 31 | cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0) |
| 32 | |
| 33 | if (state["value"] == "discharging") { |
| 34 | direction_of_change = "<" |
| 35 | } else if (state["value"] == "charging") { |
| 36 | direction_of_change = ">" |
| 37 | } else { |
| 38 | direction_of_change = "=" |
| 39 | } |
| 40 | |
| 41 | return sprintf("E%s%d%%", direction_of_change, charge["value"]) |
| 42 | } |
| 43 | |
| 44 | function bar_make_status_mem( total, used, percent, status) { |
| 45 | cache_get(total, "khatus_sensor_memory", "total", 5) |
| 46 | cache_get(used , "khatus_sensor_memory", "used" , 5) |
| 47 | # Checking total["value"] to avoid division by zero when data is missing |
| 48 | if (!total["is_expired"] && \ |
| 49 | !used["is_expired"] && \ |
| 50 | total["value"] \ |
| 51 | ) { |
| 52 | percent = util_round((used["value"] / total["value"]) * 100) |
| 53 | status = sprintf("%d%%", percent) |
| 54 | } else { |
| 55 | status = "__" |
| 56 | } |
| 57 | return sprintf("M=%s", status) |
| 58 | } |
| 59 | |
| 60 | function bar_make_status_procs() { |
| 61 | # From man ps: |
| 62 | # D uninterruptible sleep (usually IO) |
| 63 | # R running or runnable (on run queue) |
| 64 | # S interruptible sleep (waiting for an event to complete) |
| 65 | # T stopped by job control signal |
| 66 | # t stopped by debugger during the tracing |
| 67 | # W paging (not valid since the 2.6.xx kernel) |
| 68 | # X dead (should never be seen) |
| 69 | # Z defunct ("zombie") process, terminated but not reaped by its parent |
| 70 | # |
| 71 | # Additionally, not documented in ps man page: |
| 72 | # I Idle |
| 73 | # |
| 74 | src = "khatus_sensor_procs" |
| 75 | all = cache_get_fmt_def(src, "total_procs" , 15, "%d") |
| 76 | r = cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0") |
| 77 | d = cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0") |
| 78 | t = cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0") |
| 79 | i = cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0") |
| 80 | z = cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0") |
| 81 | return sprintf("P=[%s %sr %sd %st %si %sz]", all, r, d, t, i, z) |
| 82 | } |
| 83 | |
| 84 | function bar_make_status_cpu( l, t, f) { |
| 85 | l_src = "khatus_sensor_loadavg" |
| 86 | t_src = "khatus_sensor_temperature" |
| 87 | f_src = "khatus_sensor_fan" |
| 88 | l = cache_get_fmt_def(l_src, "load_avg_1min", 5, "%4.2f") |
| 89 | t = cache_get_fmt_def(t_src, "temp_c" , 5, "%d" ) |
| 90 | f = cache_get_fmt_def(f_src, "speed" , 5, "%4d" ) |
| 91 | return sprintf("C=[%s %s°C %srpm]", l, t, f) |
| 92 | } |
| 93 | |
| 94 | function bar_make_status_disk( u, w, r, src_u, src_io) { |
| 95 | src_u = "khatus_sensor_disk_space" |
| 96 | src_io = "khatus_sensor_disk_io" |
| 97 | u = cache_get_fmt_def(src_u , "disk_usage_percentage", 10, "%s") |
| 98 | w = cache_get_fmt_def(src_io, "sectors_written" , 5, "%0.3f") |
| 99 | r = cache_get_fmt_def(src_io, "sectors_read" , 5, "%0.3f") |
| 100 | return sprintf("D=[%s%% %s▲ %s▼]", u, w, r) |
| 101 | } |
| 102 | |
| 103 | function bar_make_status_net( \ |
| 104 | number_of_net_interfaces_to_show, \ |
| 105 | net_interfaces_to_show, \ |
| 106 | io, \ |
| 107 | wi, \ |
| 108 | i, \ |
| 109 | interface, \ |
| 110 | label, \ |
| 111 | wifi, \ |
| 112 | addr, \ |
| 113 | w, \ |
| 114 | r, \ |
| 115 | io_stat, \ |
| 116 | out, \ |
| 117 | sep \ |
| 118 | ) { |
| 119 | number_of_net_interfaces_to_show = \ |
| 120 | split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",") |
| 121 | io = "khatus_sensor_net_addr_io" |
| 122 | wi = "khatus_sensor_net_wifi_status" |
| 123 | out = "" |
| 124 | sep = "" |
| 125 | for (i = number_of_net_interfaces_to_show; i > 0; i--) { |
| 126 | interface = net_interfaces_to_show[i] |
| 127 | label = substr(interface, 1, 1) |
| 128 | if (interface ~ "^w") { |
| 129 | wifi = cache_get_fmt_def(wi, "status" Kfs interface, 10, "%s") |
| 130 | label = label ":" wifi |
| 131 | } |
| 132 | addr = cache_get_fmt_def(io, "addr" Kfs interface, 5, "%s", "") |
| 133 | w = cache_get_fmt_def(io, "bytes_written" Kfs interface, 5, "%0.3f") |
| 134 | r = cache_get_fmt_def(io, "bytes_read" Kfs interface, 5, "%0.3f") |
| 135 | io_stat = addr ? sprintf("%s▲ %s▼", w, r) : "--" |
| 136 | out = out sep label ":" io_stat |
| 137 | sep = " " |
| 138 | } |
| 139 | return sprintf("N[%s]", out) |
| 140 | } |
| 141 | |
| 142 | function bar_make_status_bluetooth( src, key) { |
| 143 | src = "khatus_sensor_bluetooth_power" |
| 144 | key = "power_status" |
| 145 | return sprintf("B=%s", cache_get_fmt_def(src, key, 10, "%s")) |
| 146 | } |
| 147 | |
| 148 | function bar_make_status_screen_brightness( src, key) { |
| 149 | src = "khatus_sensor_screen_brightness" |
| 150 | key = "percentage" |
| 151 | return sprintf("*%s%%", cache_get_fmt_def(src, key, 5, "%d")) |
| 152 | } |
| 153 | |
| 154 | function bar_make_status_volume( sink, mu, vl, vr, show) { |
| 155 | sink = Opt_Pulseaudio_Sink |
| 156 | cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5) |
| 157 | cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5) |
| 158 | cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5) |
| 159 | show = "--" |
| 160 | if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) { |
| 161 | if (mu["value"] == "yes") {show = "X"} |
| 162 | else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]} |
| 163 | else { |
| 164 | msg_out_error(\ |
| 165 | "bar_make_status_volume", \ |
| 166 | "Unexpected value for 'mute' field: " mu["value"] \ |
| 167 | ) |
| 168 | } |
| 169 | } |
| 170 | return sprintf("(%s)", show) |
| 171 | } |
| 172 | |
| 173 | function bar_make_status_mpd( state, status) { |
| 174 | cache_get(state, "khatus_sensor_mpd", "state", 5) |
| 175 | if (!state["is_expired"] && state["value"]) { |
| 176 | if (state["value"] == "play") { |
| 177 | status = bar_make_status_mpd_state_known("▶") |
| 178 | } else if (state["value"] == "pause") { |
| 179 | status = bar_make_status_mpd_state_known("❚❚") |
| 180 | } else if (state["value"] == "stop") { |
| 181 | status = bar_make_status_mpd_state_known("⬛") |
| 182 | } else { |
| 183 | msg_out_error(\ |
| 184 | "bar_make_status_mpd", \ |
| 185 | "Unexpected value for 'state' field: " state["value"] \ |
| 186 | ) |
| 187 | status = "--" |
| 188 | } |
| 189 | } else { |
| 190 | status = "--" |
| 191 | } |
| 192 | |
| 193 | return sprintf("[%s]", status) |
| 194 | } |
| 195 | |
| 196 | function bar_make_status_mpd_state_known(symbol, s, song, time, percentage) { |
| 197 | s = "khatus_sensor_mpd" |
| 198 | song = cache_get_fmt_def(s, "song" , 5, "%s", "?") |
| 199 | time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?") |
| 200 | percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?") |
| 201 | song = substr(song, 1, Opt_Mpd_Song_Max_Chars) |
| 202 | return sprintf("%s %s %s %s", symbol, time, percent, song) |
| 203 | } |
| 204 | |
| 205 | function bar_make_status_weather( src, hour, t_f) { |
| 206 | src = "khatus_sensor_weather" |
| 207 | hour = 60 * 60 |
| 208 | t_f = cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d") |
| 209 | return sprintf("%s°F", t_f) |
| 210 | } |
| 211 | |
| 212 | function bar_make_status_datetime( dt) { |
| 213 | return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s") |
| 214 | } |