diff options
| author | Sam Nystrom <sam@samnystrom.dev> | 2024-02-04 10:26:57 -0500 |
|---|---|---|
| committer | Sam Nystrom <sam@samnystrom.dev> | 2024-02-04 10:26:57 -0500 |
| commit | 2a1ada12b2af913d74bb90d0e2e87f58d8327029 (patch) | |
| tree | 9f28fafb2447fd03a8b7c08b9519b3e34a7e8b19 /bin/statusbar | |
| parent | 6fc3bdba27bc854c01108cc4c54086407a2daeb9 (diff) | |
Adapt statusbar script for swaybar
Diffstat (limited to 'bin/statusbar')
| -rwxr-xr-x | bin/statusbar | 123 |
1 files changed, 65 insertions, 58 deletions
diff --git a/bin/statusbar b/bin/statusbar index bc74cae..4dfafec 100755 --- a/bin/statusbar +++ b/bin/statusbar @@ -1,70 +1,77 @@ -#!/usr/bin/env perl +#!/bin/sh -use 5.038; -use utf8; - -use Time::Piece; - -sub span ($color, $text) { - return "<span foreground=\"$color\">$text</span>"; +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" } -sub volume { - my ($volume) = `pactl get-sink-volume \@DEFAULT_SINK@` =~ /\s(\d+)%/; - chomp(my $mute = `pactl get-sink-mute \@DEFAULT_SINK@`); - - my $symbol = ''; - if ($mute eq 'Mute: yes') { - $symbol = ''; - } elsif ($volume == 0) { - $symbol = ''; - } elsif ($volume <= 50) { - $symbol = ''; - } - return span('#ed8796', "$symbol $volume%"); +network() { + ssid="$(iwctl station wlan0 get-networks | awk -F' ' '/>/{print $3}')" + if [ -n "$ssid" ]; then + printf ' %s' "$ssid" + else + printf ' ' + fi } -sub network { - chomp(my $ssid = `iwgetid -r`); - my $symbol = $ssid ? ' ' : ' '; - return span('#f5a97f', "$symbol $ssid"); +brightness() { + printf ' %d%%' "$(brightctl)" } -sub brightness { - my @brightness = split(/,/, `brightnessctl -m`); - return span('#eed49f', ' ' . $brightness[3]); -} +battery() { + cap="$(cat /sys/class/power_supply/BAT0/capacity)" + stat="$(cat /sys/class/power_supply/BAT0/status)" -sub battery { - open my $cap_file, '<', '/sys/class/power_supply/BAT0/capacity' or die; - open my $status_file, '<', '/sys/class/power_supply/BAT0/status' or die; - chomp(my $capacity = <$cap_file>); - chomp(my $status = <$status_file>); - close $status_file; - close $cap_file; - - my $symbol = ''; - my $index = ($capacity - 1) / 10; - if ($status eq 'Discharging') { - my @symbols = (span('#ed8796', ''), '', '', '', '', '', '', '', '', ''); - $symbol = $symbols[$index]; - } elsif ($status eq 'Charging') { - my @symbols = ('', '', '', '', '', '', '', '', '', ''); - $symbol = $symbols[$index]; - } elsif ($status eq 'Full') { - $symbol = ''; - } - return span('#a6da95', "$symbol $capacity%"); + 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" } -sub clock { - return span('#7dc4e4', ' ' . localtime()->strftime('%b %d %H:%M')); +clock() { + printf ' %s' "$(date '+%b %-d %H:%M:%S')" } -while (1) { - my $status = join(' | ', volume, network, brightness, battery, clock); - $status = span('#cad3f5', $status); - say $status; - system('somebar', '-c', 'status', $status); - sleep 1; -} +# 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 |
