fc4415a58d95589b46137b6a483a3659a5384fdf
[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=$(cat /proc/loadavg | awk '{printf "%4.2f %4.2f %4.2f", $1, $2, $3}')
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=$(
42 df \
43 | awk '
44 function round(n) {return int(n + 0.5)}
45
46 $1 == "/dev/mapper/kubuntu--vg-root" {
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) {
60 direction = ">";
61 } else if (curr_perc < prev_perc) {
62 direction = "<";
63 } else {
64 direction = "=";
65 }
66 diff_blocks = curr_blocks - prev_blocks;
67 printf("%s[%d%% %d]", direction, curr_perc, diff_blocks);
68 }')
69
70 io_net=$(
71 awk '
72 BEGIN {
73 bytes_per_unit = 1024 * 1024
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
94 printf("%s %0.3f▲ %0.3f▼\n", device, diff_write, diff_read);
95 }
96 ' /proc/net/dev
97 )
98
99 energy=$(
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
117 datetime=$(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
138 volume_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
161 volume="[$volume_pactl]"
162
163 wifi=$(cat $STATUS_FILE__WIFI)
164
165 screen_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
179 bluetooth_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
227 energy_direction=$(echo "$energy" | cut -b 1)
228 energy_percentage=$(echo "$energy" | tr -d '<>=%')
229 if [[ "$energy_direction" = '<' ]]
230 then
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
260 else
261 rm -f "$STATUS_FILE__ENERGY_NOTIFIED_BELLOW_HALF"
262 fi
263
264 weather=$(awk 'NR == 1 {printf("%d°F", $1)}' ~/var/run/metar-KJFK-decoded-temp-fahrenheit)
265
266 signal_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
292 mpd_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
315 echo \
316 "\
317 E$energy\
318 \
319 \
320 M$memory\
321 \
322 \
323 D$disk\
324 \
325 \
326 C=[$cpu ${temp}°C ${fan}rpm]\
327 \
328 \
329 S=$screen_brightness\
330 \
331 \
332 V=$volume\
333 \
334 \
335 B:$bluetooth_power\
336 \
337 \
338 W:[$wifi $(echo "$io_net" | awk '/^wlp3s0/ {print $2, $3}')]\
339 \
340 \
341 $mpd_state\
342 \
343 $signal_last_msg_age\
344 \
345 $weather\
346 \
347 $datetime \
348 "
This page took 0.096589 seconds and 3 git commands to generate.