From e1ed2be8490e245c93a9a953a191864508f56574 Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Sun, 23 Jul 2023 21:16:43 -0400 Subject: statusbar: rewrite to perl --- bin/statusbar | 158 ++++++++++++++++++++++++++-------------------------------- 1 file 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 "$text"; +} + +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'{" ".join(args)}' - - -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) -- cgit v1.2.3