From 2c98bbacc8db3b251e1679f9da84cf1f5ed5726a Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Tue, 15 Oct 2024 13:29:10 -0400 Subject: update ~/bin --- bin/statusline.sh | 79 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100755 bin/statusline.sh (limited to 'bin/statusline.sh') diff --git a/bin/statusline.sh b/bin/statusline.sh new file mode 100755 index 0000000..61eb799 --- /dev/null +++ b/bin/statusline.sh @@ -0,0 +1,79 @@ +#!/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 in real time by +# polling more frequently when the user is changing them. +last_vol= +last_brt= +sleep_for=10 +while true; do + vol="$(volume)" + brt="$(brightness)" + if [ "$last_vol" != "$vol" ] || [ "$last_brt" != "$brt" ]; then + sleep_for=1 + elif [ $sleep_for != 10 ]; then + sleep_for=$((sleep_for+1)) + else + sleep_for=10 + fi + last_vol="$vol" + last_brt="$brt" + + printf '%s | %s | %s | %s | %s\n' \ + "$vol" \ + "$(network)" \ + "$brt" \ + "$(battery)" \ + "$(clock)" + sleep $((sleep_for/10)).$((sleep_for%10)) +done -- cgit v1.2.3