#!/usr/bin/env python3 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 + 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(''.join(traceback.format_exception(None, e, e.__traceback__))) time.sleep(1 - time.time() % 1)