home · contact · privacy
Some refactoring. master
authorPlom Heller <plom@plomlompom.com>
Sat, 19 Sep 2026 13:14:36 +0000 (15:14 +0200)
committerPlom Heller <plom@plomlompom.com>
Sat, 19 Sep 2026 13:14:36 +0000 (15:14 +0200)
home/user/.config/sway/status.py

index d57cde99129c9996467b6db4ad9b1ebbbef1c3b7..70f44739fe8484cc86eb796373f0b482b0964687 100755 (executable)
@@ -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 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
 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)
 
 
     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.'
 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)
     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)
 
 
     return ' '.join(texts)
 
 
@@ -107,13 +124,10 @@ def info_datetime(
 def info_layout(
         ) -> str:
     'Produce two-character name of active keyboard layout.'
 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(
 
 
 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'(?<!\\):').split(line))
         # nmcli -t separates by ":" – except where escaped as "\:" …
         return tuple(tuple(field.replace('\\:', ':').replace('\\\\', '\\')
                            for field in re_compile(r'(?<!\\):').split(line))
-                     for line in subprocess_run(('nmcli', '-t', *args),
-                                                capture_output=True,
-                                                text=True,
-                                                check=True
-                                                ).stdout.splitlines())
+                     for line in run_stdout('nmcli', '-t', *args).splitlines())
 
     def dev_info(
             device: str,
 
     def dev_info(
             device: str,