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