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