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
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.'
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)
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(
# 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,