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