#!/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*'
'Not charging': '-',
}
+PATH_THERMAL = Path('/sys/class/thermal')
+GLOB_THERMAL = 'thermal_zone*/temp'
+
def int_at_path(
path: 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.'
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)