blob: 5cab9ffa0d27d965dc8063bb115a94701d11e2b3 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
|
#!/bin/sh
# steal from pystardust, GPL3 license: https://github.com/pystardust/sbar
# modified to fit my own need
# INIT
vpn_fifo="$XDG_CACHE_HOME/sbar_vpn_fifo"
if ! [ -e "$vpn_fifo" ]; then
mkfifo "$vpn_fifo"
elif ! [ -p "$vpn_fifo" ]; then
exit 1
fi
printf '%s' "$$" > "$XDG_CACHE_HOME/pidofbar"
sec=0
bool () {
read str
if [ "$str" = "on" ] || [ "$str" = "up" ] || [ "$str" = "Connected" ]; then
echo 1
else
echo 0
fi
}
# MODULES
update_time () {
time="$(date '+%a %m/%d %H:%M') $(TZ=Asia/Shanghai date '+/%d %H:')"
}
update_cap () {
cap="$(if xset q | grep -q "Caps Lock: *on"; then echo A; else echo a; fi)"
}
update_net () {
net="$(bool < /sys/class/net/wlp6s0/operstate)"
}
update_vpn () {
vpn="$( < "$vpn_fifo" awk '{print $3}' | bool)"
}
update_vol () {
# $(NF-1) for both alsa and pulseaudio
vol="$(amixer get Master | awk -F'[][]' 'END{printf("%d %s",($(NF-1)=="on")?1:0,$2)}')"
}
update_mic () {
# $(NF-1) for both alsa and pulseaudio
mic="$(amixer get Capture | awk -F'[][]' 'END{print $(NF-1)}' | bool)"
}
update_bat () {
bat="$(cat /sys/class/power_supply/BAT1/capacity)%"
}
# For calcurse users, refer https://github.com/pystardust/automeet
#update_event () {
# event="$(calcurse -n | sed 1d | \
# sed -E "s_^ *\[(.*):(.*)\] ([^\t]*)\t?.*_[\1h \2m->\3]_")"
# [ "[]" = "$event" ] && event=""
#}
display () {
xsetroot -name "$time | N $net V $vpn | M $vol C $mic | $cap | $bat"
}
# modules that don't update on their own need to be run at the start for getting their initial value
update_vol
update_mic
# SIGNALLING
# trap "<function>;display" "RTMIN+n"
trap "update_mic;display" "RTMIN"
trap "update_vol;display" "RTMIN+1"
# xev can't read my toggle internet keyboard key, don't know what key to use in sxhkd to send signal
#trap "update_net;display" "RTMIN+2"
trap "update_cap;display" "RTMIN+3"
trap "update_vpn;display" "RTMIN+4"
# to update it from external commands
## kill -m "$(cat "$XDG_CACHE_HOME/pidofbar")"
# where m = 34 + n
# `mullvad status` write logs everytime it is called
# so I use `mullvad status listen`, this implementation is kinda bloat, maybe there's better way
sh -c '
mullvad status listen | while read -r line; do
kill -RTMIN+4 "$(cat "$XDG_CACHE_HOME/pidofbar")"
echo "$line" > "$1"
done &
' shell "$vpn_fifo"
while :; do
sleep 1 &
wait
[ $((sec % 5 )) -eq 0 ] && update_time # update time every 5 seconds
[ $((sec % 5 )) -eq 0 ] && update_net
update_cap
[ $((sec % 60)) -eq 0 ] && update_bat
#[ $((sec % 300)) -eq 1 ] && update_event
# how often the display updates ( 5 seconds )
[ $((sec % 5 )) -eq 0 ] && display
sec=$((sec + 1))
done
|