blob: f6d44024db89886e253f1271256f82a89c4fb6e5 (
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#!/usr/bin/env bash
set -euo pipefail
kind="${1:-}"
iface="${POLYBAR_NETWORK:-eth0}"
profile="${WG_QUICK_PROFILE:-PC}"
state_dir="${XDG_RUNTIME_DIR:-/tmp}"
if [ ! -w "$state_dir" ]; then
state_dir="${XDG_CACHE_HOME:-$HOME/.cache}"
mkdir -p "$state_dir" 2>/dev/null || true
fi
if [ ! -w "$state_dir" ]; then
state_dir="/tmp"
fi
human_bytes() {
local bytes="$1"
if [ "$bytes" -ge 1048576 ]; then
awk -v v="$bytes" 'BEGIN { printf "%.1fM", v / 1048576 }'
elif [ "$bytes" -ge 1024 ]; then
awk -v v="$bytes" 'BEGIN { printf "%.0fK", v / 1024 }'
else
printf '%sB' "$bytes"
fi
}
case "$kind" in
awake)
[ -f "$state_dir/polybar-awake.state" ] && echo on || echo off
;;
cpu)
state_file="$state_dir/quickshell-cpu.state"
read -r cpu user nice system idle iowait irq softirq steal _ </proc/stat
idle_all=$((idle + iowait))
total=$((user + nice + system + idle + iowait + irq + softirq + steal))
if [ -r "$state_file" ]; then
read -r old_total old_idle <"$state_file" || true
else
old_total="$total"
old_idle="$idle_all"
fi
printf '%s %s\n' "$total" "$idle_all" >"$state_file"
diff_total=$((total - old_total))
diff_idle=$((idle_all - old_idle))
if [ "$diff_total" -le 0 ]; then
echo 0%
else
echo "$(((100 * (diff_total - diff_idle)) / diff_total))%"
fi
;;
date)
date '+%a %d %b %H:%M'
;;
disk)
df -P / | awk 'NR == 2 { print $5 }'
;;
display)
[ -f "$state_dir/polybar-display-night.state" ] && echo night || echo day
;;
dunst)
if ! command -v dunstctl >/dev/null 2>&1; then
echo off
elif [ "$(dunstctl is-paused 2>/dev/null || echo false)" = true ]; then
waiting="$(dunstctl count waiting 2>/dev/null || echo 0)"
[ "$waiting" -gt 0 ] 2>/dev/null && echo "dnd $waiting" || echo dnd
else
echo on
fi
;;
keyboard)
export DISPLAY="${DISPLAY:-:0}"
if [ -z "${XAUTHORITY:-}" ] && [ -r "$HOME/.Xauthority" ]; then
export XAUTHORITY="$HOME/.Xauthority"
fi
query="$(setxkbmap -query 2>/dev/null || true)"
layout="$(printf '%s\n' "$query" | awk '/^layout:/ { print $2; exit }')"
variant="$(printf '%s\n' "$query" | awk '/^variant:/ { print $2; exit }')"
case "${layout:-unknown}:${variant:-}" in
us:dvorak) echo "us dv" ;;
cz:*|cz:) echo cz ;;
*:) echo "${layout:-unknown}" ;;
*) echo "${layout:-unknown} ${variant}" ;;
esac
;;
mem)
awk '
/MemTotal:/ { total = $2 }
/MemAvailable:/ { avail = $2 }
END { if (total > 0) printf "%d%%\n", ((total - avail) * 100) / total; else print "n/a" }
' /proc/meminfo
;;
mic)
if ! command -v pactl >/dev/null 2>&1; then
echo n/a
else
mute="$(pactl get-source-mute @DEFAULT_SOURCE@ 2>/dev/null | awk '{ print $2 }' || true)"
[ "$mute" = yes ] && echo muted || echo on
fi
;;
music)
if ! command -v playerctl >/dev/null 2>&1; then
echo n/a
exit 0
fi
status="$(playerctl status 2>/dev/null || true)"
[ -n "$status" ] || { echo off; exit 0; }
artist="$(playerctl metadata artist 2>/dev/null || true)"
title="$(playerctl metadata title 2>/dev/null || true)"
if [ -n "$artist" ] && [ -n "$title" ]; then
printf '%s - %s\n' "$artist" "$title" | cut -c 1-34
elif [ -n "$title" ]; then
printf '%s\n' "$title" | cut -c 1-34
else
echo "$status"
fi
;;
net)
ip -o -4 addr show "$iface" 2>/dev/null | awk '{ split($4, ip, "/"); print ip[1]; found = 1; exit } END { if (!found) print "offline" }'
;;
netspeed)
rx_file="/sys/class/net/$iface/statistics/rx_bytes"
tx_file="/sys/class/net/$iface/statistics/tx_bytes"
state_file="$state_dir/quickshell-net-${iface}.state"
if [ ! -r "$rx_file" ] || [ ! -r "$tx_file" ]; then
echo n/a
exit 0
fi
now="$(date +%s)"
rx="$(cat "$rx_file")"
tx="$(cat "$tx_file")"
if [ -r "$state_file" ]; then
read -r old_now old_rx old_tx <"$state_file" || true
else
old_now="$now"
old_rx="$rx"
old_tx="$tx"
fi
printf '%s %s %s\n' "$now" "$rx" "$tx" >"$state_file"
delta=$((now - old_now))
[ "$delta" -gt 0 ] || delta=1
down=$(((rx - old_rx) / delta))
up=$(((tx - old_tx) / delta))
[ "$down" -ge 0 ] || down=0
[ "$up" -ge 0 ] || up=0
printf '%s/%s\n' "$(human_bytes "$down")" "$(human_bytes "$up")"
;;
scratch)
if command -v i3-msg >/dev/null 2>&1 && command -v jq >/dev/null 2>&1; then
i3-msg -t get_tree 2>/dev/null |
jq '[.. | objects | select(.name == "__i3_scratch") | .floating_nodes[]?] | length' 2>/dev/null || echo 0
else
echo n/a
fi
;;
temp)
if command -v sensors >/dev/null 2>&1; then
temp="$(
sensors 2>/dev/null |
awk '
/Package id 0:/ { gsub(/[^0-9.-]/, "", $4); print int($4); exit }
/Tctl:/ { gsub(/[^0-9.-]/, "", $2); print int($2); exit }
/temp1:/ && $2 ~ /^\+/ { gsub(/[^0-9.-]/, "", $2); print int($2); exit }
'
)"
else
temp=""
fi
if [ -z "$temp" ]; then
for zone in /sys/class/thermal/thermal_zone*/temp; do
[ -r "$zone" ] || continue
value="$(cat "$zone")"
if [ "$value" -gt 0 ] 2>/dev/null; then
temp=$((value / 1000))
break
fi
done
fi
[ -n "${temp:-}" ] && echo "${temp}C" || echo n/a
;;
updates)
if command -v checkupdates >/dev/null 2>&1; then
(checkupdates 2>/dev/null || true) | wc -l
elif command -v pacman >/dev/null 2>&1; then
(pacman -Qu 2>/dev/null || true) | wc -l
else
echo n/a
fi
;;
volume)
if ! command -v pactl >/dev/null 2>&1; then
echo n/a
exit 0
fi
mute="$(pactl get-sink-mute @DEFAULT_SINK@ 2>/dev/null | awk '{ print $2 }' || true)"
vol="$(pactl get-sink-volume @DEFAULT_SINK@ 2>/dev/null | awk 'NR == 1 { print $5; exit }' || true)"
[ "$mute" = yes ] && echo muted || echo "${vol:-n/a}"
;;
wg)
if [ ! -d "/sys/class/net/$profile" ]; then
echo down
exit 0
fi
if ! command -v wg >/dev/null 2>&1; then
echo up
exit 0
fi
latest="$(wg show "$profile" latest-handshakes 2>/dev/null | awk 'NF { if ($2 > newest) newest = $2 } END { print newest + 0 }' || true)"
if [ -z "$latest" ] || [ "$latest" -le 0 ] 2>/dev/null; then
echo up
exit 0
fi
age=$(($(date +%s) - latest))
if [ "$age" -lt 60 ]; then suffix="${age}s"; elif [ "$age" -lt 3600 ]; then suffix="$((age / 60))m"; elif [ "$age" -lt 86400 ]; then suffix="$((age / 3600))h"; else suffix="$((age / 86400))d"; fi
echo "up $suffix"
;;
*)
echo n/a
exit 1
;;
esac
|