From ae0a9f6b097ad503c0711bc476b543fbe71f8fc7 Mon Sep 17 00:00:00 2001 From: Plom Heller Date: Thu, 17 Sep 2026 12:23:19 +0200 Subject: [PATCH] To sway status bar add audio devices status. --- home/user/.config/sway/status.py | 34 ++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/home/user/.config/sway/status.py b/home/user/.config/sway/status.py index 303a5b3..1fdee79 100755 --- a/home/user/.config/sway/status.py +++ b/home/user/.config/sway/status.py @@ -1,16 +1,17 @@ #!/usr/bin/env python3 'Sway bar status line generator.' -from json import dumps as json_dumps +from json import dumps as json_dumps, loads as json_loads from datetime import datetime from pathlib import Path from re import compile as re_compile from subprocess import run as subprocess_run from time import sleep +from typing import Any METADATA = {'version': 1} -PATH_THERMAL = Path('/sys/class/thermal') -GLOB_THERMAL = 'thermal_zone*/temp' +AUDIO_DEVS_LABELS = (('sink', 'vol'), + ('source', '--mic')) PATH_POWER_SUPPLY = Path('/sys/class/power_supply') GLOB_BATTERY = 'BAT*' @@ -20,6 +21,9 @@ BATTERY_STATUS_SYMBOLS = { 'Not charging': '-', } +PATH_THERMAL = Path('/sys/class/thermal') +GLOB_THERMAL = 'thermal_zone*/temp' + def int_at_path( path: Path @@ -30,6 +34,27 @@ def int_at_path( return int(stripped_text) +def info_audio( + ) -> str: + "Produce audio devices' volume, muteness." + def pactl_json( + *args: str + ) -> Any: + return json_loads(subprocess_run( + ('pactl', '-f', 'json', *args), + check=True, capture_output=True, text=True + ).stdout) + defaults = pactl_json('info') + texts = [] + for dev_type, label in AUDIO_DEVS_LABELS: + dev = next(dev for dev in pactl_json('list', f'{dev_type}s') + if dev['name'] == defaults[f'default_{dev_type}_name']) + items_pc = {item['value_percent'] for item in dev['volume'].values()} + assert len(items_pc) == 1 + texts += [f'{label} {items_pc.pop()}' + 'M' * int(dev['mute'])] + return ' '.join(texts) + + def info_battery( ) -> str: 'Produce battery charge, status, health status bar text.' @@ -125,6 +150,7 @@ if __name__ == '__main__': print(json_dumps([{'full_text': text} for text in info_network() + (info_battery(), info_thermal(), - info_datetime())]), + info_datetime(), + info_audio())]), end=',\n', flush=True) sleep(1) -- 2.30.2