Show current MPD file
[khatus.git] / bin / khatus_show
1 #! /bin/bash
2
3 set -e
4
5 BIN=$HOME/bin
6 STATUS_DIR=$HOME/var/run/status
7 STATUS_FILE__WIFI=$STATUS_DIR/wifi
8 STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF=$STATUS_DIR/notified_energy_bellow_half
9
10 load=$(awk '{printf("%4.2f", $1)}' /proc/loadavg)
11
12 fan=$(awk '/^speed:/ {printf "%4d", $2}' /proc/acpi/ibm/fan)
13
14 cpu=$($BIN/khatus_cpu_usage_from_proc_since_last_check)
15
16 memory=$(
17 free \
18 | awk '
19 function round(n) {return int(n + 0.5)}
20
21 $1 == "Mem:" {
22 total=$2;
23 used=$3;
24 cache=$6;
25 prev_file = "/home/siraaj/var/run/status/memory_used_percentage";
26 curr = round(used / total * 100);
27 getline prev < prev_file;
28 print curr > prev_file;
29 if (curr > prev) {
30 direction = ">";
31 } else if (curr < prev) {
32 direction = "<";
33 } else {
34 direction = "=";
35 }
36 printf("%s%d%%", direction, curr);
37 }')
38
39 temp=$(awk 'NR == 1 {print $1 / 1000}' /sys/class/thermal/thermal_zone0/temp)
40
41 disk_io=$(
42 awk '
43 {
44 bytes_per_sector = 512
45 bytes_per_unit = 1024 * 1024
46
47 curr_sectors_read = $3
48 curr_sectors_write = $7
49
50 prev_file_prefix = "/home/siraaj/var/run/status/disk_io"
51 prev_sectors_read_file = prev_file_prefix "_sectors_read"
52 prev_sectors_write_file = prev_file_prefix "_sectors_write"
53
54 getline prev_sectors_read < prev_sectors_read_file
55 getline prev_sectors_write < prev_sectors_write_file
56
57 diff_read_sectors = (curr_sectors_read - prev_sectors_read)
58 diff_write_sectors = (curr_sectors_write - prev_sectors_write)
59
60 diff_read_bytes = diff_read_sectors * bytes_per_sector
61 diff_write_bytes = diff_write_sectors * bytes_per_sector
62
63 diff_read = diff_read_bytes / bytes_per_unit
64 diff_write = diff_write_bytes / bytes_per_unit
65
66 print curr_sectors_read > prev_sectors_read_file
67 print curr_sectors_write > prev_sectors_write_file
68
69 printf("%0.3f▲ %0.3f▼\n", diff_write, diff_read);
70
71 }
72 ' /sys/block/dm-1/stat
73 )
74
75 disk=$(
76 df \
77 | awk -v disk_io="$disk_io" '
78 function round(n) {return int(n + 0.5)}
79
80 $1 == "/dev/mapper/kubuntu--vg-root" {
81 curr_perc = $5; sub("%$", "", curr_perc);
82 prev_perc_file = "/home/siraaj/var/run/status/disk_space_used";
83 getline prev_perc < prev_perc_file;
84 print curr_perc > prev_perc_file;
85 if (curr_perc > prev_perc) {
86 direction = ">";
87 } else if (curr_perc < prev_perc) {
88 direction = "<";
89 } else {
90 direction = "=";
91 }
92 printf("%s[%d%% %s]", direction, curr_perc, disk_io);
93 }')
94
95 network=$(
96 awk -v wifi_conn=$(cat $STATUS_FILE__WIFI) '
97 BEGIN {
98 bytes_per_unit = 1024 * 1024
99 }
100
101 NR > 2 {
102 device = $1; sub(":$", "", device);
103 curr_read = $2;
104 curr_write = $10;
105
106 prev_file_prefix = "/home/siraaj/var/run/status/io_net_" device;
107 prev_read_file = prev_file_prefix "_read";
108 prev_write_file = prev_file_prefix "_write";
109
110 getline prev_read < prev_read_file;
111 getline prev_write < prev_write_file;
112
113 diff_read = (curr_read - prev_read ) / bytes_per_unit;
114 diff_write = (curr_write - prev_write) / bytes_per_unit;
115
116 print curr_read > prev_read_file;
117 print curr_write > prev_write_file;
118
119 devices[device] = sprintf("%0.3f▲ %0.3f▼", diff_write, diff_read);
120 }
121
122 END {
123 lo = devices["lo"]
124 #eth = devices["enp0s25"]
125 wifi = devices["wlp3s0"]
126 printf("[%s | %s %s]", lo, wifi_conn, wifi)
127 }
128 ' /proc/net/dev
129 )
130
131 energy=$(
132 upower -e \
133 | grep battery \
134 | xargs upower -i \
135 | awk '
136 /^ +percentage: +/ {percentage=$2}
137 /^ +state: +/ {state=$2}
138 END {
139 if (state == "discharging") {
140 direction_of_change = "<"
141 } else if (state == "charging") {
142 direction_of_change = ">"
143 } else {
144 direction_of_change = "="
145 };
146 printf("%s%s", direction_of_change, percentage)
147 }')
148
149 datetime=$(date +'%a, %b %d, %H:%M:%S')
150
151 #volume_amixer=$(
152 # amixer get Master \
153 # | tail -1 \
154 # | awk '
155 # {
156 # level = $4;
157 # sub("^\\[", "", level);
158 # sub("\\]$", "", level);
159 # print level;
160 # }' \
161 # )
162
163 #volume_amixer=$(
164 # amixer get Master \
165 # | tail -n 1 \
166 # | awk '{print $4}' \
167 # | tr -d '[]'
168 #)
169
170 volume_pactl=$(
171 pactl list sinks \
172 | awk '
173 /^\tMute:/ {
174 printf("%s,", $0);
175 }
176 /^\tVolume:/ {
177 for (i=2; i<=NF; i++) printf(" %s", $i);
178 }' \
179 | awk -v RS=',' '
180 /^[ \t]*Mute:/ {mute = $2}
181 /^[ \t]*front-left:/ {left = $4}
182 /^[ \t]*front-right:/ {right = $4}
183 END {
184 if (mute == "yes") {
185 printf("x")
186 } else {
187 printf("%s %s", left, right)
188 }
189 }
190 '
191 )
192
193 volume="[$volume_pactl]"
194
195 screen_brightness=$(
196 max=$(cat /sys/class/backlight/acpi_video0/max_brightness)
197 cur=$(cat /sys/class/backlight/acpi_video0/brightness)
198 awk -v max=$max -v cur=$cur 'BEGIN {printf("%d%%", cur/max*100)}'
199 )
200
201 #bluetooth_status=$(
202 # grep '^status:' /proc/acpi/ibm/bluetooth \
203 # | awk '
204 # $2 == "disabled" {printf "off"}
205 # $2 == "enabled" {printf "on"}
206 # '
207 #)
208
209 bluetooth_power=$(
210 echo -e 'show \n quit' \
211 | bluetoothctl \
212 | awk '
213 /^Controller / {
214 controller = $2;
215 controllers[++ctrl_count] = controller;
216 }
217 /^\t[A-Z][A-Za-z]+:/ {
218 key = $1;
219 sub(":$", "", key);
220 val = $2;
221 for (i=3; i<=NF; i++) {
222 val = val " " $i};
223 data[controller, key] = val;
224 }
225 END {
226 # Using the 1st seen controller. Should we select specific instead?
227 power_status = data[controllers[1], "Powered"];
228 if (ctrl_count > 0) {
229 if (power_status == "no") {
230 power_status = "off"
231 } else if (power_status == "yes") {
232 power_status = "on"
233 } else {
234 printf("Unexpected bluetooth power status: %s\n", power_status)\
235 > "/dev/stderr";
236 power_status = "ERROR"
237 }
238 } else {
239 power_status = "off" # TODO: Perhaps use differentiated marker?
240 }
241 printf("%s", power_status);
242 }'
243 )
244
245 #touchpad_status=$(
246 # xinput list-props 12 \
247 # | awk '
248 # /^\tDevice Enabled \([0-9]+\):/ {
249 # status = $4;
250 # printf("%s", status);
251 # }'
252 #)
253
254 #color_off='\033[0m'
255 #color_on_bg_gray='\033[m\033[40m'
256
257 energy_direction=$(echo "$energy" | cut -b 1)
258 energy_percentage=$(echo "$energy" | tr -d '<>=%')
259 if [[ "$energy_direction" = '<' ]]
260 then
261 if [[ $energy_percentage -le 5 ]]
262 then
263 DISPLAY=:0.0 notify-send \
264 -u critical \
265 "Energy CRITICALLY low: $energy" \
266 'CHARGE NOW!!! GO GO GO!!!'
267 elif [[ $energy_percentage -le 10 ]]
268 then
269 DISPLAY=:0.0 notify-send \
270 -u critical \
271 "Energy VERY low: $energy" \
272 'Plug it in ASAP.'
273 elif [[ $energy_percentage -le 15 ]]
274 then
275 DISPLAY=:0.0 notify-send \
276 -u critical \
277 "Energy low: $energy" \
278 'Get the charger.'
279 elif [[ $energy_percentage -le 50 ]]
280 then
281 if [[ ! -a "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF" ]]
282 then
283 DISPLAY=:0.0 notify-send \
284 -u normal \
285 "Energy bellow half: $energy" \
286 'Where is the charger?'
287 touch "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF"
288 fi
289 fi
290 else
291 rm -f "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF"
292 fi
293
294 weather=$(awk 'NR == 1 {printf("%d°F", $1)}' ~/var/run/metar-KJFK-decoded-temp-fahrenheit)
295
296 signal_last_msg_age=$(
297 ls -lt --time-style=+%s $HOME/var/lib/signal/latest_message.json \
298 | awk -v now_seconds=$(date +%s) \
299 '{
300 mtime_seconds = $6;
301 seconds = now_seconds - mtime_seconds;
302 minutes = seconds / 60;
303 hours = minutes / 60;
304 days = hours / 24;
305 weeks = days / 7;
306 months = days / 30;
307 #fmt = "%.1f";
308 fmt = "%d";
309 #printf(fmt " s\n", seconds);
310 printf(fmt " m\n", minutes);
311 printf(fmt " h\n", hours);
312 printf(fmt " d\n", days);
313 printf(fmt " w\n", weeks);
314 printf(fmt " mo\n", months);
315 }' \
316 | awk '$1 >= 1' \
317 | sort -n -k 1 \
318 | head -1 \
319 | tr -d ' '
320 )
321
322 mpd_state=$(
323 echo 'status' \
324 | nc 127.0.0.1 6600 \
325 | awk '
326 {
327 status[$1] = $2
328 }
329
330 END {
331 state = status["state:"]
332 symbol = "-"
333 if (state == "play") {
334 symbol = "▶"
335 } else if (state == "pause") {
336 symbol = "❚❚"
337 } else if (state == "stop") {
338 symbol = "⬛"
339 }
340 printf("%s", symbol)
341 }
342 '
343 )
344
345 mpd_current_file=$(
346 echo 'currentsong' \
347 | nc 127.0.0.1 6600 \
348 | awk -v max_chars=10 '
349 /^file:/ {
350 file = $2
351 for (i=3; i<=NF; i++) {file = file " " $i}
352 last = split(file, parts, "/")
353 print substr(parts[last], 1, max_chars)
354 }
355 '
356 )
357
358 echo \
359 "\
360 E$energy\
361 \
362 \
363 M$memory\
364 \
365 \
366 C=[$cpu ${temp}°C ${fan}rpm]\
367 \
368 \
369 L=$load\
370 \
371 \
372 D$disk\
373 \
374 \
375 N:$network\
376 \
377 \
378 B:$bluetooth_power\
379 \
380 \
381 S=$screen_brightness\
382 \
383 \
384 V=$volume\
385 \
386 [$mpd_state $mpd_current_file]\
387 \
388 $signal_last_msg_age\
389 \
390 $weather\
391 \
392 $datetime \
393 "
This page took 0.13275 seconds and 4 git commands to generate.