From: Plom Heller Date: Sat, 19 Sep 2026 13:14:36 +0000 (+0200) Subject: Some refactoring. X-Git-Url: https://plomlompom.com/repos/new_day?a=commitdiff_plain;p=confplom Some refactoring. --- diff --git a/home/user/.config/sway/status.py b/home/user/.config/sway/status.py index d57cde9..70f4473 100755 --- a/home/user/.config/sway/status.py +++ b/home/user/.config/sway/status.py @@ -7,6 +7,7 @@ from re import compile as re_compile from subprocess import run as subprocess_run from sys import path as sys_path from time import sleep +from typing import Iterable sys_path.append( str(Path(__file__).resolve().parents[2] / '.local' / 'bin' / 'lib')) from pactl import pactl_info_default_dev @@ -43,6 +44,23 @@ def int_at_path( return int(stripped_text) +def run_stdout( + *cli_args: str + ) -> str: + 'Call command via cli_args, return its stdout.' + return subprocess_run(cli_args, check=True, capture_output=True, text=True + ).stdout + + +def first_among_equals[T]( + to_produce_equals: Iterable[T] + ) -> T: + 'Assuming input to produce only equal items, return one of the identicals.' + items = set(to_produce_equals) + assert len(items) == 1 + return items.pop() + + def info_clipboard( ) -> tuple[str, ...]: 'Produce clipboard contents summary.' @@ -71,10 +89,9 @@ def info_audio( texts = [] for dev_type, label in AUDIO_DEVS_LABELS: dev_info = pactl_info_default_dev(dev_type) - items_pc = {item['value_percent'] - for item in dev_info['volume'].values()} - assert len(items_pc) == 1 - texts += [f'{label} {items_pc.pop()}' + 'M' * int(dev_info['mute'])] + percentage = first_among_equals( + item['value_percent'] for item in dev_info['volume'].values()) + texts += [f'{label} {percentage}' + 'M' * int(dev_info['mute'])] return ' '.join(texts) @@ -107,13 +124,10 @@ def info_datetime( def info_layout( ) -> str: 'Produce two-character name of active keyboard layout.' - inputs = json_loads(subprocess_run(('swaymsg', '-t', 'get_inputs', '-r'), - capture_output=True, - check=True).stdout) - indices = {dev['xkb_active_layout_index'] - for dev in inputs if 'xkb_active_layout_index' in dev} - assert len(indices) == 1 - return KB_LAYOUTS[indices.pop()] + return KB_LAYOUTS[first_among_equals( + dev['xkb_active_layout_index'] + for dev in json_loads(run_stdout('swaymsg', '-t', 'get_inputs', '-r')) + if 'xkb_active_layout_index' in dev)] def info_network( @@ -125,11 +139,7 @@ def info_network( # nmcli -t separates by ":" – except where escaped as "\:" … return tuple(tuple(field.replace('\\:', ':').replace('\\\\', '\\') for field in re_compile(r'(?