summaryrefslogtreecommitdiff
path: root/bin/statusbar
blob: 4dfafecf2dbaaee12004a0bc737ea81cc380acbe (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
#!/bin/sh

volume() {
	volume="$(wpctl get-volume @DEFAULT_AUDIO_SINK@)"
	volume="$(dc -e "${volume#Volume:\ } 100 * p")"
	volume="${volume%.*}"
	if [ "$volume" -eq 0 ]; then
		symbol=
	elif [ "$volume" -le 50 ]; then
		symbol=
	else
		symbol=
	fi
	printf '%s %d%%' "$symbol" "$volume"
}

network() {
	ssid="$(iwctl station wlan0 get-networks | awk -F'  ' '/>/{print $3}')"
	if [ -n "$ssid" ]; then
		printf '󰖩  %s' "$ssid"
	else
		printf '󰖪 '
	fi
}

brightness() {
	printf ' %d%%' "$(brightctl)"
}

battery() {
	cap="$(cat /sys/class/power_supply/BAT0/capacity)"
	stat="$(cat /sys/class/power_supply/BAT0/status)"

	symbol=󰁹
	index=$(((cap-1)/10+1))
	case "$stat" in
		Discharging)
			symbol="$(printf '󰂃 󰁻 󰁼 󰁽 󰁾 󰁿 󰂀 󰂁 󰂂 󰁹' | cut -d' ' -f$index)"
			;;
		Charging)
			symbol="$(printf '󰢜 󰂆 󰂇 󰂈 󰢝 󰂉 󰢞 󰂊 󰂋 󰂅' | cut -d' ' -f$index)"
			;;
		Full)
			symbol=󰂄
			;;
	esac
	printf '%s %d%%' "$symbol" "$cap"
}

clock() {
	printf ' %s' "$(date '+%b %-d %H:%M:%S')"
}

# Make the volume and brightness sections appear to update more often by
# heuristically detecting when the user is changing them.
last_vol=
last_brt=
sleep_for=1
while true; do
	vol="$(volume)"
	brt="$(brightness)"
	if [ "$last_vol" != "$vol" ] || [ "$last_brt" != "$brt" ]; then
		sleep_for=0.1
	elif [ ${sleep_for%%.*} != 1 ]; then
		sleep_for=$((sleep_for+0.1))
	else
		sleep_for=1
	fi

	printf '%s | %s | %s | %s | %s\n' \
		"$vol" \
		"$(network)" \
		"$brt" \
		"$(battery)" \
		"$(clock)"
	sleep $sleep_for
done