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