-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshstatus
executable file
·372 lines (311 loc) · 12.2 KB
/
shstatus
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env bash
# shstatus - a simple statusline generator for i3bar and swaybar
# https://github.com/acuteenvy/shstatus
# License: MIT
readonly VERSION="2.0.1"
# Display a red message in the bar and sleep forever.
err() {
printf '[{"color":"#ff0000","full_text":"%s"}]\n' "$*"
snore
}
# Set variables to display a red message in a specific block.
berr() {
full_text="$*"
color="#ff0000"
}
check_args() {
[[ $1 -ne $2 ]] && berr "expected $1 args, got $2" && return 1
return 0
}
# Open a file descriptor that does not output anything.
exec {snore_fd}<> <(:)
# Sleep function in pure bash.
# Usage: snore <time in seconds>
# If called without arguments, it will sleep forever.
snore() {
# Read the fd and stop after $1 seconds.
read ${1:+"-t$1"} -ru "$snore_fd" || :
}
# Unset all variables used to construct blocks.
unset_block_vars() {
unset full_text short_text color background
unset border border_top border_bottom border_left border_right
unset min_width align urgent
unset separator separator_block_width
}
# Block generator.
# This function should always be called without any arguments.
# Commas are added in main(), because trailing commas are not supported by i3's JSON parser,
# and probably won't ever be: https://github.com/i3/i3/issues/3498
block() {
# full_text is required. If it's not set, reset all other variables
# and display an error in red instead.
[[ -z $full_text ]] && unset_block_vars && berr "full_text is not set"
# Begin the block.
printf "{"
# Print all variables.
printf '"full_text":"%s"' "$full_text"
[[ -n $short_text ]] && printf ',"short_text":"%s"' "$short_text"
[[ -n $color ]] && printf ',"color":"%s"' "$color"
[[ -n $background ]] && printf ',"background":"%s"' "$background"
[[ -n $border ]] && printf ',"border":"%s"' "$border"
[[ -n $border_top ]] && printf ',"border_top":%s' "$border_top"
[[ -n $border_bottom ]] && printf ',"border_bottom":%s' "$border_bottom"
[[ -n $border_left ]] && printf ',"border_left":%s' "$border_left"
[[ -n $border_right ]] && printf ',"border_right":%s' "$border_right"
[[ -n $min_width ]] && printf ',"min_width":"%s"' "$min_width"
[[ -n $align ]] && printf ',"align":"%s"' "$align"
[[ -n $urgent ]] && printf ',"urgent":%s' "$urgent"
[[ -n $separator ]] && printf ',"separator":%s' "$separator"
[[ -n $separator_block_width ]] && printf ',"separator_block_width":%s' "$separator_block_width"
# End the block.
printf '}'
# Unset the variables to prevent using the same config
# in the next block.
unset_block_vars
}
block_backlight() {
check_args 2 "$#" || return
local -r path="/sys/class/backlight"
local current max percentage
! read -r current < "$path/$1/$2" && berr "could not read $path/$1/$2" && return
! read -r max < "$path/$1/max_brightness" && berr "could not read $path/$1/max_brightness" && return
local percentage=$((current * 100 / max))
if [[ -n $full_text ]]; then
full_text="${full_text/\%current/$current}"
full_text="${full_text/\%max/$max}"
full_text="${full_text/\%percentage/$percentage%}"
fi
if [[ -n $short_text ]]; then
short_text="${short_text/\%current/$current}"
short_text="${short_text/\%max/$max}"
short_text="${short_text/\%percentage/$percentage%}"
fi
}
block_battery() {
check_args 4 "$#" || return
local -r path="/sys/class/power_supply"
local percentage status
! read -r percentage < "$path/$1/capacity" && berr "could not read $path/$1/capacity" && return
! read -r status < "$path/$1/status" && berr "could not read $path/$1/status" && return
case $status in
Charging) status="$2";;
Discharging) status="$3";;
Full) status="$4";;
esac
if [[ -n $full_text ]]; then
full_text="${full_text/\%percentage/$percentage%}"
full_text="${full_text/\%status/$status}"
fi
if [[ -n $short_text ]]; then
short_text="${short_text/\%percentage/$percentage%}"
short_text="${short_text/\%status/$status}"
fi
}
block_disk() {
check_args 1 "$#" || return
local disk size used avail usepercent mountpoint dfinfo
! dfinfo="$(df -h "$1" 2>&1)" && berr "$dfinfo" && return
# Remove the header.
dfinfo="${dfinfo#*$'\n'}"
IFS=" " read -r disk size used avail usepercent mountpoint <<< "$dfinfo"
if [[ -n $full_text ]]; then
full_text="${full_text/\%disk/$disk}"
full_text="${full_text/\%size/$size}"
full_text="${full_text/\%used/$used}"
full_text="${full_text/\%avail/$avail}"
full_text="${full_text/\%usepercent/$usepercent}"
full_text="${full_text/\%mountpoint/$mountpoint}"
fi
if [[ -n $short_text ]]; then
short_text="${short_text/\%disk/$disk}"
short_text="${short_text/\%size/$size}"
short_text="${short_text/\%used/$used}"
short_text="${short_text/\%avail/$avail}"
short_text="${short_text/\%usepercent/$usepercent}"
short_text="${short_text/\%mountpoint/$mountpoint}"
fi
}
block_loadavg() {
local load1 load5 load15
! IFS=" " read -r load1 load5 load15 _ _ < /proc/loadavg && berr "could not read /proc/loadavg" && return
if [[ -n $full_text ]]; then
full_text="${full_text/\%load1/$load1}"
full_text="${full_text/\%load5/$load5}"
full_text="${full_text/\%load15/$load15}"
fi
if [[ -n $short_text ]]; then
short_text="${short_text/\%load1/$load1}"
short_text="${short_text/\%load5/$load5}"
short_text="${short_text/\%load15/$load15}"
fi
}
block_uptime() {
check_args 5 "$#" || return
local ups uptime
! IFS="." read -r ups _ _ _ _ < /proc/uptime && berr "could not read /proc/uptime" && return
local upd=$(( ups / 86400 ))
local uph_s=$(( ups % 86400 ))
local uph=$(( uph_s / 3600 ))
local upm_s=$(( uph_s % 3600 ))
local upm=$(( upm_s / 60))
local ups=$(( upm_s % 60 ))
if [[ $upd -ne 0 ]]; then
uptime="$upd$1$5$uph$2$5$upm$3$5$ups$4"
elif [[ $uph -ne 0 ]]; then
uptime="$uph$2$5$upm$3$5$ups$4"
elif [[ $upm -ne 0 ]]; then
uptime="$upm$3$5$ups$4"
else
uptime="$ups$4"
fi
[[ $4 == "DISABLE" ]] && uptime="${uptime%"$ups$4"}" && uptime="${uptime%"$5"}"
[[ -z $uptime ]] && uptime="<1$3"
[[ -n $full_text ]] && full_text="${full_text/\%uptime/$uptime}"
[[ -n $short_text ]] && short_text="${short_text/\%uptime/$uptime}"
return 0
}
PREV_TOTAL=0
PREV_IDLE=0
block_cpu() {
local stat user nice system idle iowait irq softirq steal guest guest_nice
# Get the first line from /proc/stat
! read -r stat < /proc/stat && berr "could not read /proc/stat" && return
IFS=" " read -r _ user nice system idle iowait irq softirq steal guest guest_nice <<< "$stat"
local total diff_total diff_idle usage
total=$((user + nice + system + idle + iowait + irq + softirq + steal + guest + guest_nice))
diff_total=$((total - PREV_TOTAL))
diff_idle=$((idle - PREV_IDLE))
usage=$(( 100 * (diff_total - diff_idle) / diff_total ))
PREV_TOTAL="$total"
PREV_IDLE="$idle"
[[ -n $full_text ]] && full_text="${full_text/\%usage/$usage}"
[[ -n $short_text ]] && short_text="${short_text/\%usage/$usage}"
return 0
}
block_mem() {
local free memtotal memused memavail swaptotal swapused swapavail
! free="$(free -h 2>&1)" && berr "${free%%$'\n'*}" && return
# Remove the header.
free="${free#*$'\n'}"
# Read the first line, remove it, then read the second line.
IFS=" " read -r _ memtotal memused _ _ _ memavail <<< "$free"
free="${free#*$'\n'}"
IFS=" " read -r _ swaptotal swapused swapavail <<< "$free"
if [[ -n $full_text ]]; then
full_text="${full_text/\%memtotal/$memtotal}"
full_text="${full_text/\%memused/$memused}"
full_text="${full_text/\%memavail/$memavail}"
full_text="${full_text/\%swaptotal/$swaptotal}"
full_text="${full_text/\%swapused/$swapused}"
full_text="${full_text/\%swapavail/$swapavail}"
fi
if [[ -n $short_text ]]; then
short_text="${short_text/\%memtotal/$memtotal}"
short_text="${short_text/\%memused/$memused}"
short_text="${short_text/\%memavail/$memavail}"
short_text="${short_text/\%swaptotal/$swaptotal}"
short_text="${short_text/\%swapused/$swapused}"
short_text="${short_text/\%swapavail/$swapavail}"
fi
}
block_pavol() {
check_args 2 "$#" || return
local vol mutestatus
! vol="$(pactl get-sink-volume "$1" 2>&1)" && berr "pactl: $vol" && return
IFS=" " read -r _ _ _ _ vol <<< "$vol"
vol="${vol%% *}"
vol="${vol//[[:space:]]/}"
! mutestatus="$(pactl get-sink-mute "$1" 2>&1)" && berr "pactl: $mutestatus" && return
if [[ $mutestatus == *yes* ]]; then
mutestatus="$2"
else
mutestatus=""
fi
if [[ -n $full_text ]]; then
full_text="${full_text/\%vol/$vol}"
full_text="${full_text/\%mutestatus/$mutestatus}"
fi
if [[ -n $short_text ]]; then
short_text="${short_text/\%vol/$vol}"
short_text="${short_text/\%mutestatus/$mutestatus}"
fi
}
block_pwvol() {
check_args 2 "$#" || return
local vol mutestatus
! vol="$(wpctl get-volume "$1" 2>&1)" && berr "wpctl: ${vol%%$'\n'*}" && return
IFS=" " read -r _ vol mutestatus <<< "$vol"
vol="${vol/./}"
[[ ! $vol =~ ^[0-9]+$ ]] && berr "failed to parse wpctl output" && return
# Strip leading zeros.
vol=$((10#$vol))
if [[ $mutestatus == "[MUTED]" ]]; then
mutestatus="$2"
else
mutestatus=""
fi
if [[ -n $full_text ]]; then
full_text="${full_text/\%vol/$vol%}"
full_text="${full_text/\%mutestatus/$mutestatus}"
fi
if [[ -n $short_text ]]; then
short_text="${short_text/\%vol/$vol%}"
short_text="${short_text/\%mutestatus/$mutestatus}"
fi
}
block_datetime() {
check_args 2 "$#" || return
local date
case $2 in
printf) printf -v date "%($1)T" "-1";;
date) date="$(date "+$1")";;
*) berr "expected 'date' or 'printf' as the second arg, got '$2'" && return;;
esac
[[ -n $full_text ]] && full_text="${full_text/\%date/$date}"
[[ -n $short_text ]] && short_text="${short_text/\%date/$date}"
return 0
}
main() {
case $1 in
"help" | "-h" | "--help")
printf '%b' "Usage: ${0##*/} [-hv] [config-file]\nSee 'man shstatus' for more information\n"
exit 0
;;
"-v" | "--version") printf "shstatus v%s\n" "$VERSION"; exit 0;;
esac
# Print the header and begin an infinite array.
printf '{"version":1}\n[\n'
local config="$XDG_CONFIG_HOME/shstatus/config.sh"
[[ ! -f $config ]] && config="$HOME/.config/shstatus/config.sh"
[[ ! -f $config ]] && config="/etc/shstatus/config.sh"
[[ -n $1 ]] && config="$1"
[[ ! -f $config ]] && err "'$config': not a file"
# shellcheck disable=1090
source "$config"
[[ -z $interval ]] && err "interval not specified"
[[ -z ${blocks[*]} ]] && err "the blocks array is empty"
[[ ! "$(declare -p blocks)" =~ "declare -a" ]] && err "blocks is not an array"
for ((i=0; i < ${#blocks[@]}; i++)); do
bl="${blocks[$i]}"
[[ $(type -t "$bl") != "function" ]] && err "$bl (block number $((i+1))) is not a function"
done
while true; do
# Begin an array of blocks.
printf "[\n"
# Print the first block without a comma.
"${blocks[0]}"; block
for prepare_fn in "${blocks[@]:1}"; do
# Separate subsequent blocks. See the comment above the definition of block().
printf ','
# These functions set variables needed for block() to construct a block.
"$prepare_fn"
# Print the block.
block
done
# End the array.
printf "\n],\n"
snore "$interval"
done
}
main "$@"