From 6ad29f5700ab0ab0afed346875e48e977eacae4f Mon Sep 17 00:00:00 2001 From: Sam Nystrom Date: Mon, 10 Jul 2023 20:15:31 -0400 Subject: statusbar: rewrite to python Mostly for maintainability --- bin/statusbar | 133 ++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 74 insertions(+), 59 deletions(-) mode change 100755 => 100644 bin/statusbar (limited to 'bin') diff --git a/bin/statusbar b/bin/statusbar old mode 100755 new mode 100644 index a0b61d5..8d5dc6f --- a/bin/statusbar +++ b/bin/statusbar @@ -1,59 +1,74 @@ -#!/bin/sh -u - -volume() { - volume=$(pactl get-sink-volume @DEFAULT_SINK@ | sed -nE 's/.*\s([0-9]+)%.*/\1/p') - if [ "$(pactl get-sink-mute @DEFAULT_SINK@)" = "Mute: yes" ]; then - volume_symbol="󰝟" - elif [ "$volume" -eq 0 ]; then - volume_symbol="" - elif [ "$volume" -le 50 ]; then - volume_symbol="" - else - volume_symbol="" - fi - printf '%s %d%%' "$volume_symbol" "$volume" -} - -network() { - ssid=$(iwgetid -r) - if [ -n "$ssid" ]; then - symbol="󰖩 " - else - symbol="󰖪" - fi - printf '%s %s' "$symbol" "$ssid" -} - -brightness() { - brightness=$(brightnessctl -m | awk -F, '{ print $4 }') - printf ' %s' "$brightness" -} - -battery() { - status=$(cat /sys/class/power_supply/BAT0/status) - capacity=$(cat /sys/class/power_supply/BAT0/capacity) - - idx=$(((capacity - 1) / 10 + 1)) - case "$status" in - "Discharging") - symbol=$(echo '󰂃,󰁻,󰁼,󰁽,󰁾,󰁿,󰂀,󰂁,󰂂,󰁹' | - awk -F, "{ print \$$idx }") - ;; - "Charging") - symbol=$(echo "󰢜 󰂆 󰂇 󰂈 󰢝 󰂉 󰢞 󰂊 󰂋 󰂅" | awk "{ print \$$idx }") - ;; - "Full") - symbol="󰂄" - ;; - esac - printf '%s %d%%' "$symbol" "$capacity" -} - -clock() { - printf ' %s' "$(date '+%b %d %H:%M')" -} - -while true; do - somebar -c status "$(volume) | $(network) | $(brightness) | $(battery) | $(clock)" - sleep 0.2 -done +#!/usr/bin/env python3 + +import subprocess +import time +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 + 1 + 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 = '' +while True: + try: + status = ' | '.join([volume(), network(), brightness(), battery(), clock()]) + status = span('#cad3f5', status) + if status != prev: + subprocess.run(['somebar', '-c', 'status', status]) + prev = status + except Exception as e: + print(e) + time.sleep(1 - time.time() % 1) -- cgit v1.2.3