Break out status component values from formatting
[khatus.git] / src / awk / exe / bar.awk
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 # Energy
27 # -----------------------------------------------------------------------------
28
29 function bar_make_status_energy( d, p) {
30 d = bar_make_status_energy_direction()
31 p = bar_make_status_energy_percent()
32 return sprintf("E%s%d%%", d, p)
33 }
34
35 function bar_make_status_energy_percent( charge) {
36 cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0)
37 return charge["value"]
38 }
39
40 function bar_make_status_energy_direction( state, direction_of_change) {
41 cache_get(state, "khatus_sensor_energy", "battery_state", 0)
42 if (state["value"] == "discharging") {
43 direction_of_change = "<"
44 } else if (state["value"] == "charging") {
45 direction_of_change = ">"
46 } else {
47 direction_of_change = "="
48 }
49 return direction_of_change
50 }
51
52 # -----------------------------------------------------------------------------
53 # Memory
54 # -----------------------------------------------------------------------------
55
56 function bar_make_status_mem() {
57 return sprintf("M=%s%%", bar_make_status_mem_percent())
58 }
59
60 function bar_make_status_mem_percent( total, used, percent, percent_str) {
61 cache_get(total, "khatus_sensor_memory", "total", 5)
62 cache_get(used , "khatus_sensor_memory", "used" , 5)
63 # Checking total["value"] to avoid division by zero when data is missing
64 if (!total["is_expired"] && \
65 !used["is_expired"] && \
66 total["value"] \
67 ) {
68 percent = util_round((used["value"] / total["value"]) * 100)
69 percent_str = sprintf("%d", percent)
70 } else {
71 percent_str = "__"
72 }
73 return percent_str
74 }
75
76 # -----------------------------------------------------------------------------
77 # Processes
78 # -----------------------------------------------------------------------------
79
80 function bar_make_status_procs() {
81 # From man ps:
82 # D uninterruptible sleep (usually IO)
83 # R running or runnable (on run queue)
84 # S interruptible sleep (waiting for an event to complete)
85 # T stopped by job control signal
86 # t stopped by debugger during the tracing
87 # W paging (not valid since the 2.6.xx kernel)
88 # X dead (should never be seen)
89 # Z defunct ("zombie") process, terminated but not reaped by its parent
90 #
91 # Additionally, not documented in ps man page:
92 # I Idle
93 #
94 all = bar_make_status_procs_count_all()
95 r = bar_make_status_procs_count_r()
96 d = bar_make_status_procs_count_d()
97 t = bar_make_status_procs_count_t()
98 i = bar_make_status_procs_count_i()
99 z = bar_make_status_procs_count_z()
100 return sprintf("P=[%s %sr %sd %st %si %sz]", all, r, d, t, i, z)
101 }
102
103 function bar_make_status_procs_count_all() {
104 return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d")
105 }
106
107 function bar_make_status_procs_count_r( src) {
108 src = "khatus_sensor_procs"
109 return cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0")
110 }
111
112 function bar_make_status_procs_count_d( src) {
113 src = "khatus_sensor_procs"
114 return cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0")
115 }
116
117 function bar_make_status_procs_count_t( src) {
118 src = "khatus_sensor_procs"
119 return cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0")
120 }
121
122 function bar_make_status_procs_count_i( src) {
123 src = "khatus_sensor_procs"
124 return cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0")
125 }
126
127 function bar_make_status_procs_count_z( src) {
128 src = "khatus_sensor_procs"
129 return cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0")
130 }
131
132 # -----------------------------------------------------------------------------
133 # CPU
134 # -----------------------------------------------------------------------------
135
136 function bar_make_status_cpu( l, t, f) {
137 l = bar_make_status_cpu_loadavg()
138 t = bar_make_status_cpu_temperature()
139 f = bar_make_status_cpu_fan_speed()
140 return sprintf("C=[%s %s°C %srpm]", l, t, f)
141 }
142
143 function bar_make_status_cpu_loadavg( src) {
144 src = "khatus_sensor_loadavg"
145 return cache_get_fmt_def(src, "load_avg_1min", 5, "%4.2f")
146 }
147
148 function bar_make_status_cpu_temperature() {
149 return cache_get_fmt_def("khatus_sensor_temperature", "temp_c", 5, "%d")
150 }
151
152 function bar_make_status_cpu_fan_speed() {
153 return cache_get_fmt_def("khatus_sensor_fan", "speed", 5, "%4d")
154 }
155
156 # -----------------------------------------------------------------------------
157 # Disk
158 # -----------------------------------------------------------------------------
159
160 function bar_make_status_disk( u, w, r) {
161 u = bar_make_status_disk_space()
162 w = bar_make_status_disk_io_w()
163 r = bar_make_status_disk_io_r()
164 return sprintf("D=[%s%% %s▲ %s▼]", u, w, r)
165 }
166
167 function bar_make_status_disk_space( src) {
168 src = "khatus_sensor_disk_space"
169 return cache_get_fmt_def(src, "disk_usage_percentage", 10, "%s")
170 }
171
172 function bar_make_status_disk_io_w( src) {
173 src = "khatus_sensor_disk_io"
174 return cache_get_fmt_def(src, "sectors_written", 5, "%0.3f")
175 }
176
177 function bar_make_status_disk_io_r( src) {
178 src = "khatus_sensor_disk_io"
179 return cache_get_fmt_def(src, "sectors_read", 5, "%0.3f")
180 }
181
182 # -----------------------------------------------------------------------------
183 # Network
184 # -----------------------------------------------------------------------------
185
186 function bar_make_status_net( \
187 number_of_net_interfaces_to_show, \
188 net_interfaces_to_show, \
189 i, \
190 interface, \
191 label, \
192 addr, \
193 w, \
194 r, \
195 out, \
196 sep \
197 ) {
198 number_of_net_interfaces_to_show = \
199 split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",")
200 out = ""
201 sep = ""
202 for (i = number_of_net_interfaces_to_show; i > 0; i--) {
203 interface = net_interfaces_to_show[i]
204 label = substr(interface, 1, 1)
205 if (interface ~ "^w") {
206 label = label ":" bar_make_status_net_wifi(interface)
207 }
208 addr = bar_make_status_net_addr(interface)
209 w = bar_make_status_net_io_w(interface)
210 r = bar_make_status_net_io_r(interface)
211 out = out sep label ":" sprintf("%s▲ %s▼", w, r)
212 sep = " "
213 }
214 return sprintf("N[%s]", out)
215 }
216
217 function bar_make_status_net_addr(interface, src) {
218 src = "khatus_sensor_net_addr_io"
219 return cache_get_fmt_def(src, "addr" Kfs interface, 5, "%s", "")
220 }
221
222 function bar_make_status_net_io_w(interface, src) {
223 src = "khatus_sensor_net_addr_io"
224 return cache_get_fmt_def(src, "bytes_written" Kfs interface, 5, "%0.3f")
225 }
226
227 function bar_make_status_net_io_r(interface, src) {
228 src = "khatus_sensor_net_addr_io"
229 return cache_get_fmt_def(src, "bytes_read" Kfs interface, 5, "%0.3f")
230 }
231
232 function bar_make_status_net_wifi(interface, src) {
233 src = "khatus_sensor_net_wifi_status"
234 return cache_get_fmt_def(src, "status" Kfs interface, 10, "%s")
235 }
236
237 # -----------------------------------------------------------------------------
238 # Bluetooth
239 # -----------------------------------------------------------------------------
240
241 function bar_make_status_bluetooth() {
242 return sprintf("B=%s", bar_make_status_bluetooth_power())
243 }
244
245 function bar_make_status_bluetooth_power( src) {
246 src = "khatus_sensor_bluetooth_power"
247 return cache_get_fmt_def(src, "power_status", 10, "%s")
248 }
249
250 # -----------------------------------------------------------------------------
251 # Backlight
252 # -----------------------------------------------------------------------------
253
254 function bar_make_status_screen_brightness() {
255 return sprintf("*%s%%", bar_make_status_backlight_percent())
256 }
257
258 function bar_make_status_backlight_percent( src) {
259 src = "khatus_sensor_screen_brightness"
260 return cache_get_fmt_def(src, "percentage", 5, "%d")
261 }
262
263 # -----------------------------------------------------------------------------
264 # Volume
265 # -----------------------------------------------------------------------------
266
267 function bar_make_status_volume() {
268 return sprintf("(%s)", bar_make_status_volume_value())
269 }
270
271 # TODO: better name than "bar_make_status_volume_value" ... :)
272 function bar_make_status_volume_value( sink, mu, vl, vr, show) {
273 sink = Opt_Pulseaudio_Sink
274 cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5)
275 cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5)
276 cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5)
277 show = "--"
278 if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) {
279 if (mu["value"] == "yes") {show = "X"}
280 else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]}
281 else {
282 msg_out_error(\
283 "bar_make_status_volume", \
284 "Unexpected value for 'mute' field: " mu["value"] \
285 )
286 }
287 }
288 return show
289 }
290
291 # -----------------------------------------------------------------------------
292 # MPD
293 # -----------------------------------------------------------------------------
294
295 function bar_make_status_mpd() {
296 return sprintf("[%s]", bar_make_status_mpd_value())
297 }
298
299 function bar_make_status_mpd_value( state, status) {
300 cache_get(state, "khatus_sensor_mpd", "state", 5)
301 if (!state["is_expired"] && state["value"]) {
302 if (state["value"] == "play") {
303 status = bar_make_status_mpd_state_known("▶")
304 } else if (state["value"] == "pause") {
305 status = bar_make_status_mpd_state_known("❚❚")
306 } else if (state["value"] == "stop") {
307 status = bar_make_status_mpd_state_known("⬛")
308 } else {
309 msg_out_error(\
310 "bar_make_status_mpd", \
311 "Unexpected value for 'state' field: " state["value"] \
312 )
313 status = "--"
314 }
315 } else {
316 status = "--"
317 }
318 return status
319 }
320
321 function bar_make_status_mpd_state_known(symbol, s, song, time, percentage) {
322 s = "khatus_sensor_mpd"
323 song = cache_get_fmt_def(s, "song" , 5, "%s", "?")
324 time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?")
325 percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?")
326 song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
327 return sprintf("%s %s %s %s", symbol, time, percent, song)
328 }
329
330 # -----------------------------------------------------------------------------
331 # Weather
332 # -----------------------------------------------------------------------------
333
334 function bar_make_status_weather() {
335 return sprintf("%s°F", bar_make_status_weather_temp_f())
336 }
337
338 function bar_make_status_weather_temp_f( src, hour) {
339 src = "khatus_sensor_weather"
340 hour = 60 * 60
341 return cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
342 }
343
344 # -----------------------------------------------------------------------------
345 # Datetime
346 # -----------------------------------------------------------------------------
347
348 function bar_make_status_datetime( dt) {
349 return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
350 }
This page took 0.100345 seconds and 5 git commands to generate.