9be1adbcc9a60807465043bc0f6351fe26af156e
[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_percent() {
30 return cache_get_fmt_def("khatus_sensor_energy", "battery_percentage", 0, "%d")
31 }
32
33 function bar_make_status_energy_direction( state, direction_of_change) {
34 cache_get(state, "khatus_sensor_energy", "battery_state", 0)
35 if (state["value"] == "discharging") {
36 direction_of_change = "<"
37 } else if (state["value"] == "charging") {
38 direction_of_change = ">"
39 } else {
40 direction_of_change = "="
41 }
42 return direction_of_change
43 }
44
45 # -----------------------------------------------------------------------------
46 # Memory
47 # -----------------------------------------------------------------------------
48
49 function bar_make_status_mem_percent( total, used, percent, percent_str) {
50 cache_get(total, "khatus_sensor_memory", "total", 5)
51 cache_get(used , "khatus_sensor_memory", "used" , 5)
52 # Checking total["value"] to avoid division by zero when data is missing
53 if (!total["is_expired"] && \
54 !used["is_expired"] && \
55 total["value"] \
56 ) {
57 percent = util_round((used["value"] / total["value"]) * 100)
58 percent_str = sprintf("%d", percent)
59 } else {
60 percent_str = "__"
61 }
62 return percent_str
63 }
64
65 # -----------------------------------------------------------------------------
66 # Processes
67 # -----------------------------------------------------------------------------
68 # From man ps:
69 # D uninterruptible sleep (usually IO)
70 # R running or runnable (on run queue)
71 # S interruptible sleep (waiting for an event to complete)
72 # T stopped by job control signal
73 # t stopped by debugger during the tracing
74 # W paging (not valid since the 2.6.xx kernel)
75 # X dead (should never be seen)
76 # Z defunct ("zombie") process, terminated but not reaped by its parent
77 #
78 # Additionally, not documented in ps man page:
79 # I Idle
80
81 function bar_make_status_procs_count_all() {
82 return cache_get_fmt_def("khatus_sensor_procs", "total_procs", 15, "%d")
83 }
84
85 function bar_make_status_procs_count_r( src) {
86 src = "khatus_sensor_procs"
87 return cache_get_fmt_def(src, "total_per_state" Kfs "R", 15, "%d", "0")
88 }
89
90 function bar_make_status_procs_count_d( src) {
91 src = "khatus_sensor_procs"
92 return cache_get_fmt_def(src, "total_per_state" Kfs "D", 15, "%d", "0")
93 }
94
95 function bar_make_status_procs_count_t( src) {
96 src = "khatus_sensor_procs"
97 return cache_get_fmt_def(src, "total_per_state" Kfs "T", 15, "%d", "0")
98 }
99
100 function bar_make_status_procs_count_i( src) {
101 src = "khatus_sensor_procs"
102 return cache_get_fmt_def(src, "total_per_state" Kfs "I", 15, "%d", "0")
103 }
104
105 function bar_make_status_procs_count_z( src) {
106 src = "khatus_sensor_procs"
107 return cache_get_fmt_def(src, "total_per_state" Kfs "Z", 15, "%d", "0")
108 }
109
110 # -----------------------------------------------------------------------------
111 # CPU
112 # -----------------------------------------------------------------------------
113
114 function bar_make_status_cpu_loadavg( src) {
115 src = "khatus_sensor_loadavg"
116 return cache_get_fmt_def(src, "load_avg_1min", 5, "%4.2f")
117 }
118
119 function bar_make_status_cpu_temperature() {
120 return cache_get_fmt_def("khatus_sensor_temperature", "temp_c", 5, "%d")
121 }
122
123 function bar_make_status_cpu_fan_speed() {
124 return cache_get_fmt_def("khatus_sensor_fan", "speed", 5, "%4d")
125 }
126
127 # -----------------------------------------------------------------------------
128 # Disk
129 # -----------------------------------------------------------------------------
130
131 function bar_make_status_disk_space( src) {
132 src = "khatus_sensor_disk_space"
133 return cache_get_fmt_def(src, "disk_usage_percentage", 10, "%s")
134 }
135
136 function bar_make_status_disk_io_w( src) {
137 src = "khatus_sensor_disk_io"
138 return cache_get_fmt_def(src, "sectors_written", 5, "%0.3f")
139 }
140
141 function bar_make_status_disk_io_r( src) {
142 src = "khatus_sensor_disk_io"
143 return cache_get_fmt_def(src, "sectors_read", 5, "%0.3f")
144 }
145
146 # -----------------------------------------------------------------------------
147 # Network
148 # -----------------------------------------------------------------------------
149
150 function bar_make_status_net_addr(interface, src) {
151 src = "khatus_sensor_net_addr_io"
152 return cache_get_fmt_def(src, "addr" Kfs interface, 5, "%s", "")
153 }
154
155 function bar_make_status_net_io_w(interface, src) {
156 src = "khatus_sensor_net_addr_io"
157 return cache_get_fmt_def(src, "bytes_written" Kfs interface, 5, "%0.3f")
158 }
159
160 function bar_make_status_net_io_r(interface, src) {
161 src = "khatus_sensor_net_addr_io"
162 return cache_get_fmt_def(src, "bytes_read" Kfs interface, 5, "%0.3f")
163 }
164
165 function bar_make_status_net_wifi(interface, src) {
166 src = "khatus_sensor_net_wifi_status"
167 return cache_get_fmt_def(src, "status" Kfs interface, 10, "%s")
168 }
169
170 # -----------------------------------------------------------------------------
171 # Bluetooth
172 # -----------------------------------------------------------------------------
173
174 function bar_make_status_bluetooth_power( src) {
175 src = "khatus_sensor_bluetooth_power"
176 return cache_get_fmt_def(src, "power_status", 10, "%s")
177 }
178
179 # -----------------------------------------------------------------------------
180 # Backlight (screen brightness)
181 # -----------------------------------------------------------------------------
182
183 function bar_make_status_backlight_percent( src) {
184 src = "khatus_sensor_screen_brightness"
185 return cache_get_fmt_def(src, "percentage", 5, "%d")
186 }
187
188 # -----------------------------------------------------------------------------
189 # Volume
190 # -----------------------------------------------------------------------------
191
192 function bar_make_status_volume_pulseaudio_sink(sink, mu, vl, vr, show) {
193 cache_get(mu, "khatus_sensor_volume", "mute" Kfs sink, 5)
194 cache_get(vl, "khatus_sensor_volume", "vol_left" Kfs sink, 5)
195 cache_get(vr, "khatus_sensor_volume", "vol_right" Kfs sink, 5)
196 show = "--"
197 if (!mu["is_expired"] && !vl["is_expired"] && !vr["is_expired"]) {
198 if (mu["value"] == "yes") {show = "X"}
199 else if (mu["value"] == "no") {show = vl["value"] " " vr["value"]}
200 else {
201 msg_out_error(\
202 "bar_make_status_volume_pulseaudio_sink: " sink ". ", \
203 "Unexpected value for 'mute' field: " mu["value"] \
204 )
205 }
206 }
207 return show
208 }
209
210 # -----------------------------------------------------------------------------
211 # MPD
212 # -----------------------------------------------------------------------------
213
214 function bar_make_status_mpd( state, status) {
215 cache_get(state, "khatus_sensor_mpd", "state", 5)
216 if (!state["is_expired"] && state["value"]) {
217 if (state["value"] == "play") {
218 status = bar_make_status_mpd_state_known("▶")
219 } else if (state["value"] == "pause") {
220 status = bar_make_status_mpd_state_known("❚❚")
221 } else if (state["value"] == "stop") {
222 status = bar_make_status_mpd_state_known("⬛")
223 } else {
224 msg_out_error(\
225 "bar_make_status_mpd", \
226 "Unexpected value for 'state' field: " state["value"] \
227 )
228 status = "--"
229 }
230 } else {
231 status = "--"
232 }
233 return status
234 }
235
236 function bar_make_status_mpd_state_known(symbol, s, song, time, percentage) {
237 s = "khatus_sensor_mpd"
238 song = cache_get_fmt_def(s, "song" , 5, "%s", "?")
239 time = cache_get_fmt_def(s, "play_time_minimal_units", 5, "%s", "?")
240 percent = cache_get_fmt_def(s, "play_time_percentage" , 5, "%s", "?")
241 song = substr(song, 1, Opt_Mpd_Song_Max_Chars)
242 return sprintf("%s %s %s %s", symbol, time, percent, song)
243 }
244
245 # -----------------------------------------------------------------------------
246 # Weather
247 # -----------------------------------------------------------------------------
248
249 function bar_make_status_weather_temp_f( src, hour) {
250 src = "khatus_sensor_weather"
251 hour = 60 * 60
252 return cache_get_fmt_def(src, "temperature_f", 3 * hour, "%d")
253 }
254
255 # -----------------------------------------------------------------------------
256 # Datetime
257 # -----------------------------------------------------------------------------
258
259 function bar_make_status_datetime( dt) {
260 return cache_get_fmt_def("khatus_sensor_datetime", "datetime", 5, "%s")
261 }
This page took 0.098351 seconds and 3 git commands to generate.