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