From 2a1ada12b2af913d74bb90d0e2e87f58d8327029 Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Sun, 4 Feb 2024 10:26:57 -0500 Subject: Adapt statusbar script for swaybar --- bin/statusbar | 123 +++++++++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 58 deletions(-) (limited to 'bin') 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 "$text"; +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 -- cgit v1.2.3