home · contact · privacy
Minor code reorganization. master
authorPlom Heller <plom@plomlompom.com>
Thu, 17 Sep 2026 09:00:52 +0000 (11:00 +0200)
committerPlom Heller <plom@plomlompom.com>
Thu, 17 Sep 2026 09:00:52 +0000 (11:00 +0200)
home/user/.config/sway/status.py

index 7abe81f1520a324ec8516c46c8683a07b276a27a..303a5b382ab7a1f2d8d0d74c747f9b934bf3dd51 100755 (executable)
@@ -30,7 +30,33 @@ def int_at_path(
     return int(stripped_text)
 
 
-def network_info(
+def info_battery(
+        ) -> str:
+    'Produce battery charge, status, health status bar text.'
+    path_battery = next(PATH_POWER_SUPPLY.glob(GLOB_BATTERY))
+    status = BATTERY_STATUS_SYMBOLS.get(
+        (path_battery / 'status').read_text(encoding='utf8').strip(),
+        '?')
+    charge_pc, health_pc = -1, -1
+    for prefix in ('charge', 'energy'):
+        path_full = path_battery / f'{prefix}_full'
+        if path_full.exists():
+            full = int_at_path(path_full)
+            full_design = int_at_path(path_battery / f'{prefix}_full_design')
+            now = int_at_path(path_battery / f'{prefix}_now')
+            charge_pc = round(now / full * 100)
+            health_pc = round(full / full_design * 100)
+            break
+    return f'{charge_pc}%{status}{health_pc}%'
+
+
+def info_datetime(
+        ) -> str:
+    'Produce current date and time.'
+    return str(datetime.now().isoformat(' ', 'seconds'))
+
+
+def info_network(
         ) -> tuple[str, ...]:
     'Produce one status block full_text per connected ethernet/wifi interface.'
     def nmcli_fields(
@@ -84,40 +110,21 @@ def network_info(
     return texts if texts else ('offline', )
 
 
-def battery_info(
+def info_thermal(
         ) -> str:
-    'Produce battery charge, status, health status bar text.'
-    path_battery = next(PATH_POWER_SUPPLY.glob(GLOB_BATTERY))
-    status = BATTERY_STATUS_SYMBOLS.get(
-        (path_battery / 'status').read_text(encoding='utf8').strip(),
-        '?')
-    charge_pc, health_pc = -1, -1
-    for prefix in ('charge', 'energy'):
-        path_full = path_battery / f'{prefix}_full'
-        if path_full.exists():
-            full = int_at_path(path_full)
-            full_design = int_at_path(path_battery / f'{prefix}_full_design')
-            now = int_at_path(path_battery / f'{prefix}_now')
-            charge_pc = round(now / full * 100)
-            health_pc = round(full / full_design * 100)
-            break
-    return f'{charge_pc}%{status}{health_pc}%'
+    'Produce Celsius of current device hotness.'
+    return str(round(max(int_at_path(path)
+                         for path in PATH_THERMAL.glob(GLOB_THERMAL)) / 1000)
+               ) + '°C'
 
 
 if __name__ == '__main__':
     print(json_dumps(METADATA))
     print('[', end='')
-    full_texts: list[str] = []
     while True:
-        full_texts.clear()
-        full_texts += list(network_info())
-        full_texts += [battery_info()]
-        full_texts += [
-                str(round(max(int_at_path(path)
-                              for path in PATH_THERMAL.glob(GLOB_THERMAL)
-                              ) / 1000))
-                + '°C']
-        full_texts += [str(datetime.now().isoformat(' ', 'seconds'))]
-        print(json_dumps([{'full_text': text} for text in full_texts]),
+        print(json_dumps([{'full_text': text}
+                          for text in info_network() + (info_battery(),
+                                                        info_thermal(),
+                                                        info_datetime())]),
               end=',\n', flush=True)
         sleep(1)