Break out status component values from formatting
[khatus.git] / src / awk / exe / bar.awk
CommitLineData
c4fd5e7d
SK
1# Naming convention:
2# Variables:
3# - global, builtin : ALLCAPS
b1c097d6 4# - global, public : Camel_Snake_Man_Bear_Pig
c4fd5e7d
SK
5# - global, private : _snake_case_prefixed_underscore
6# - local : snake_case
7# Functions:
8# - global, public : snake_case
9
75b23ff8
SK
10# -----------------------------------------------------------------------------
11# Input
12# -----------------------------------------------------------------------------
13$1 == "OK" {
c4fd5e7d 14 cache_update()
75b23ff8
SK
15}
16
17$1 == "OK" && \
18$2 == "khatus_sensor_datetime" {
03c229bf 19 # Code for bar_make_status is expected to be passed as an
ab9fe663 20 # additional source file, using -f flag.
03c229bf 21 msg_out_ok("status_bar", bar_make_status())
75b23ff8
SK
22}
23
2d07b264 24
75b23ff8 25# -----------------------------------------------------------------------------
e62110ca 26# Energy
75b23ff8
SK
27# -----------------------------------------------------------------------------
28
e62110ca
SK
29function 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
35function bar_make_status_energy_percent( charge) {
c4fd5e7d 36 cache_get(charge, "khatus_sensor_energy", "battery_percentage", 0)
e62110ca
SK
37 return charge["value"]
38}
75b23ff8 39
e62110ca
SK
40function bar_make_status_energy_direction( state, direction_of_change) {
41 cache_get(state, "khatus_sensor_energy", "battery_state", 0)
269df6fa 42 if (state["value"] == "discharging") {
75b23ff8 43 direction_of_change = "<"
269df6fa 44 } else if (state["value"] == "charging") {
75b23ff8
SK
45 direction_of_change = ">"
46 } else {
47 direction_of_change = "="
48 }
e62110ca
SK
49 return direction_of_change
50}
75b23ff8 51
e62110ca
SK
52# -----------------------------------------------------------------------------
53# Memory
54# -----------------------------------------------------------------------------
55
56function bar_make_status_mem() {
57 return sprintf("M=%s%%", bar_make_status_mem_percent())
75b23ff8
SK
58}
59
e62110ca 60function bar_make_status_mem_percent( total, used, percent, percent_str) {
c4fd5e7d
SK
61 cache_get(total, "khatus_sensor_memory", "total", 5)
62 cache_get(used , "khatus_sensor_memory", "used" , 5)
269df6fa
SK
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 ) {
03c229bf 68 percent = util_round((used["value"] / total["value"]) * 100)
e62110ca 69 percent_str = sprintf("%d", percent)
75b23ff8 70 } else {
e62110ca 71 percent_str = "__"
75b23ff8 72 }
e62110ca 73 return percent_str
75b23ff8
SK
74}
75
e62110ca
SK
76# -----------------------------------------------------------------------------
77# Processes
78# -----------------------------------------------------------------------------
79
03c229bf 80function bar_make_status_procs() {
2e820ad5
SK
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 #
e62110ca
SK
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()
b9d5cfd4
SK
100 return sprintf("P=[%s %sr %sd %st %si %sz]", all, r, d, t, i, z)
101}
102
e62110ca
SK
103function bar_make_status_procs_count_all() {
104 return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d")
105}
106
107function 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
112function 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
117function 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
122function 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
127function 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
03c229bf 136function bar_make_status_cpu( l, t, f) {
e62110ca
SK
137 l = bar_make_status_cpu_loadavg()
138 t = bar_make_status_cpu_temperature()
139 f = bar_make_status_cpu_fan_speed()
269df6fa 140 return sprintf("C=[%s %s°C %srpm]", l, t, f)
75b23ff8
SK
141}
142
e62110ca
SK
143function 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
148function bar_make_status_cpu_temperature() {
149 return cache_get_fmt_def("khatus_sensor_temperature", "temp_c", 5, "%d")
150}
151
152function 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
160function 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()
2d07b264 164 return sprintf("D=[%s%% %s▲ %s▼]", u, w, r)
75b23ff8
SK
165}
166
e62110ca
SK
167function 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
172function 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
177function 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
03c229bf 186function bar_make_status_net( \
75b23ff8
SK
187 number_of_net_interfaces_to_show, \
188 net_interfaces_to_show, \
75b23ff8
SK
189 i, \
190 interface, \
191 label, \
192 addr, \
193 w, \
194 r, \
2d07b264
SK
195 out, \
196 sep \
75b23ff8
SK
197) {
198 number_of_net_interfaces_to_show = \
b1c097d6 199 split(Opt_Net_Interfaces_To_Show, net_interfaces_to_show, ",")
75b23ff8
SK
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)
75b23ff8 205 if (interface ~ "^w") {
e62110ca 206 label = label ":" bar_make_status_net_wifi(interface)
75b23ff8 207 }
e62110ca
SK
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)
75b23ff8
SK
212 sep = " "
213 }
75b23ff8
SK
214 return sprintf("N[%s]", out)
215}
216
e62110ca
SK
217function 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
222function 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
227function 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
232function 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
241function bar_make_status_bluetooth() {
242 return sprintf("B=%s", bar_make_status_bluetooth_power())
243}
244
245function bar_make_status_bluetooth_power( src) {
269df6fa 246 src = "khatus_sensor_bluetooth_power"
e62110ca 247 return cache_get_fmt_def(src, "power_status", 10, "%s")
75b23ff8
SK
248}
249
e62110ca
SK
250# -----------------------------------------------------------------------------
251# Backlight
252# -----------------------------------------------------------------------------
253
254function bar_make_status_screen_brightness() {
255 return sprintf("*%s%%", bar_make_status_backlight_percent())
256}
257
258function bar_make_status_backlight_percent( src) {
269df6fa 259 src = "khatus_sensor_screen_brightness"
e62110ca 260 return cache_get_fmt_def(src, "percentage", 5, "%d")
75b23ff8
SK
261}
262
e62110ca
SK
263# -----------------------------------------------------------------------------
264# Volume
265# -----------------------------------------------------------------------------
266
267function 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" ... :)
272function bar_make_status_volume_value( sink, mu, vl, vr, show) {
b1c097d6 273 sink = Opt_Pulseaudio_Sink
fd710ac0
SK
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)
767766df 277 show = "--"
fd710ac0
SK
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"]}
75b23ff8 281 else {
03c229bf
SK
282 msg_out_error(\
283 "bar_make_status_volume", \
fd710ac0 284 "Unexpected value for 'mute' field: " mu["value"] \
75b23ff8
SK
285 )
286 }
75b23ff8 287 }
e62110ca 288 return show
75b23ff8
SK
289}
290
e62110ca
SK
291# -----------------------------------------------------------------------------
292# MPD
293# -----------------------------------------------------------------------------
294
295function bar_make_status_mpd() {
296 return sprintf("[%s]", bar_make_status_mpd_value())
297}
298
299function bar_make_status_mpd_value( state, status) {
c4fd5e7d 300 cache_get(state, "khatus_sensor_mpd", "state", 5)
269df6fa
SK
301 if (!state["is_expired"] && state["value"]) {
302 if (state["value"] == "play") {
03c229bf 303 status = bar_make_status_mpd_state_known("▶")
269df6fa 304 } else if (state["value"] == "pause") {
03c229bf 305 status = bar_make_status_mpd_state_known("❚❚")
269df6fa 306 } else if (state["value"] == "stop") {
03c229bf 307 status = bar_make_status_mpd_state_known("⬛")
75b23ff8 308 } else {
03c229bf
SK
309 msg_out_error(\
310 "bar_make_status_mpd", \
269df6fa 311 "Unexpected value for 'state' field: " state["value"] \
75b23ff8
SK
312 )
313 status = "--"
314 }
315 } else {
316 status = "--"
317 }
e62110ca 318 return status
75b23ff8
SK
319}
320
03c229bf 321function bar_make_status_mpd_state_known(symbol, s, song, time, percentage) {
75b23ff8 322 s = "khatus_sensor_mpd"
c4fd5e7d
SK
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", "?")
b1c097d6 326 song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
75b23ff8
SK
327 return sprintf("%s %s %s %s", symbol, time, percent, song)
328}
329
e62110ca
SK
330# -----------------------------------------------------------------------------
331# Weather
332# -----------------------------------------------------------------------------
333
334function bar_make_status_weather() {
335 return sprintf("%s°F", bar_make_status_weather_temp_f())
336}
337
338function bar_make_status_weather_temp_f( src, hour) {
fd710ac0 339 src = "khatus_sensor_weather"
75b23ff8 340 hour = 60 * 60
e62110ca 341 return cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
75b23ff8
SK
342}
343
e62110ca
SK
344# -----------------------------------------------------------------------------
345# Datetime
346# -----------------------------------------------------------------------------
347
03c229bf 348function bar_make_status_datetime( dt) {
c4fd5e7d 349 return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
75b23ff8 350}
This page took 0.046546 seconds and 4 git commands to generate.