diff options
| author | Sam Nystrom <sam@samnystrom.dev> | 2023-07-23 21:16:43 -0400 |
|---|---|---|
| committer | Sam Nystrom <sam@samnystrom.dev> | 2023-07-23 21:16:43 -0400 |
| commit | e1ed2be8490e245c93a9a953a191864508f56574 (patch) | |
| tree | 67bab3b5486711114d81125eb57de5fef1081057 | |
| parent | b1df2d2128637c706c400ae41fcb17433ec1af07 (diff) | |
statusbar: rewrite to perl
| -rwxr-xr-x | bin/statusbar | 158 |
1 files changed, 70 insertions, 88 deletions
diff --git a/bin/statusbar b/bin/statusbar index f08db3e..66f0b49 100755 --- a/bin/statusbar +++ b/bin/statusbar @@ -1,89 +1,71 @@ -#!/usr/bin/env python3 +#!/usr/bin/env perl + +use 5.038; +use utf8; + +use Time::Piece; + +sub span ($color, $text) { + return "<span foreground=\"$color\">$text</span>"; +} + +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%"); +} + +sub network { + chomp(my $ssid = `iwgetid -r`); + my $symbol = $ssid ? ' ' : ' '; + return span('#f5a97f', "$symbol $ssid"); +} + +sub brightness { + my @brightness = split(/,/, `brightnessctl -m`); + return span('#eed49f', $brightness[3]); +} + +sub battery { + open my $cap_file, '<', '/sys/class/power_supply/BAT0/capacity' or die; + open my $status_file, '<', '/sys/class/power_supply/BAT0/capacity' 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%"); +} + +sub clock { + return span('#7dc4e4', ' ' . localtime()->strftime('%b %d %H:%M')); +} + +while (1) { + my $status = join(' | ', volume, network, brightness, battery, clock); + $status = span('#cad3f5', $status); + say $status; + system('somebar', '-c', 'status', $status); + sleep 1; +} -import subprocess -import time -import traceback -import re - - -def span(color, *args): - return f'<span foreground="{color}">{" ".join(args)}</span>' - - -def volume(): - sink_info = subprocess.check_output( - ['pactl', 'get-sink-volume', '@DEFAULT_SINK@']).decode('utf-8') - volume = int(re.search('\s(\d+)%', sink_info).group(1)) - mute = subprocess.check_output( - ['pactl', 'get-sink-mute', '@DEFAULT_SINK@']).decode('utf-8') - - symbol = '' - if mute == 'Mute: yes': - symbol = '' - elif volume == 0: - symbol = '' - elif volume <= 50: - symbol = '' - - return span('#ed8796', symbol, str(volume) + '%') - - -def network(): - ssid = subprocess.check_output(['iwgetid', '-r']).decode('utf-8').strip() - symbol = ' ' if ssid == '' else ' ' - return span('#f5a97f', symbol, ssid) - - -def brightness(): - brightness = subprocess.check_output(['brightnessctl', - '-m']).decode('utf-8').split(',')[3] - return span('#eed49f', '', brightness) - - -def battery(): - with open('/sys/class/power_supply/BAT0/capacity', 'r') as file: - capacity = int(file.read()) - with open('/sys/class/power_supply/BAT0/status', 'r') as file: - status = file.read().strip() - - symbol = '' - index = (capacity - 1) // 10 - if status == 'Discharging': - symbol = [ - span('#ed8796', ''), '', '', '', '', '', '', '', '', '' - ][index] - elif status == 'Charging': - symbol = ['', '', '', '', '', '', '', '', '', ''][index] - elif status == 'Full': - symbol = '' - return span('#a6da95', symbol, str(capacity) + '%') - - -def clock(): - return span('#7dc4e4', '', time.strftime("%b %d %H:%M")) - -prev_vol = '' -prev_br = '' -prev = '' -i = 0 -while True: - try: - vol = volume() - br = brightness() - if vol != prev_vol or br != prev_br: - i = 20 - prev_vol = vol - prev_br = br - status = ' | '.join([vol, network(), br, battery(), clock()]) - status = span('#cad3f5', status) - if status != prev: - subprocess.run(['somebar', '-c', 'status', status]) - prev = status - except Exception as e: - print(''.join(traceback.format_exception(None, e, e.__traceback__))) - - if i > 0: - time.sleep(0.05) - i -= 1 - else: - time.sleep(0.5) |
