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