007ae4793f8803e90470f6d05b652dbd92924e5f

Author
Pavle Portic <git@theedgeofrage.com>
Committer
Pavle Portic <git@theedgeofrage.com>
Date

Message

Update i3 autoname script

Diff

This diff is truncated to protect this page.

  1diff --git a/dot_config/i3/i3-autoname-workspaces.py b/dot_config/i3/i3-autoname-workspaces.py
  2index 1e0fd19543a6789c0208674b87d7676dffb2158e..ed87d1616c2b879f41a951cb29667f552e74e514 100644
  3--- a/dot_config/i3/i3-autoname-workspaces.py
  4+++ b/dot_config/i3/i3-autoname-workspaces.py
  5@@ -21,7 +21,8 @@
  6 import re
  7 import signal
  8 import subprocess as proc
  9-import sys
 10+from sys import exit
 11+from typing import Any, NoReturn
 12 
 13 import fontawesome as fa  # type: ignore
 14 import i3ipc  # type: ignore
 15@@ -36,7 +37,7 @@ import i3ipc  # type: ignore
 16 # If you're not sure what the WM_CLASS is for your application, you can use
 17 # xprop (https://linux.die.net/man/1/xprop). Run `xprop | grep WM_CLASS`
 18 # then click on the application you want to inspect.
 19-WINDOW_ICONS = {
 20+WINDOW_ICONS: dict[str, str] = {
 21     'alacritty': fa.icons['terminal'],
 22     'blender': fa.icons['cube'],
 23     'blueman-manager': fa.icons['bluetooth'],
 24@@ -106,18 +107,21 @@ DEFAULT_ICON = '*'
 25 # Used so that we can keep the workspace's name when we add icons to it.
 26 # Returns a dictionary with the following keys: 'num', 'shortname', and 'icons'
 27 # Any field that's missing in @name will be None in the returned dict
 28-def parse_workspace_name(name):
 29+def parse_workspace_name(name) -> dict[str, Any]:
 30     match = re.match(
 31         r'(?P<num>\d+):?(?P<shortname>\w+)? ?(?P<icons>.+)?',
 32         name,
 33     )
 34+    if match is None:
 35+        return {}
 36+
 37     return match.groupdict()
 38 
 39 
 40 # Given a dictionary with 'num', 'shortname', 'icons',
 41 # return the formatted name by concatenating them together.
 42-def construct_workspace_name(parts):
 43-    new_name = str(parts['num'])
 44+def construct_workspace_name(parts) -> str:
 45+    new_name: str = str(parts['num'])
 46     if parts['shortname'] or parts['icons']:
 47         new_name += ':'
 48 
 49@@ -146,27 +150,23 @@ def xprop(win_id, property):
 50         return None
 51 
 52 
 53-def icon_for_window(window):
 54-    classes = xprop(window.window, 'WM_CLASS')
 55-    if classes is not None and len(classes) > 0:
 56-        for cls in classes:
 57-            cls = cls.lower()  # case-insensitive matching
 58-            if cls in WINDOW_ICONS:
 59-                return WINDOW_ICONS[cls]
 60-
 61+def icon_for_window(window) -> str:
 62+    classes: list[str] | None = xprop(window.window, 'WM_CLASS')
 63+    if classes is None or len(classes) == 0:
 64         print(f'No icon available for: {classes}')
 65+        return DEFAULT_ICON
 66+
 67+    for cls in map(lambda x: x.lower(), classes):
 68+        if cls in WINDOW_ICONS:
 69+            return WINDOW_ICONS[cls]
 70 
 71     return DEFAULT_ICON
 72 
 73 
 74 # renames all workspaces based on the windows present
 75-def rename_workspaces(i3):
 76+def rename_workspaces(i3) -> None:
 77     for workspace in i3.get_tree().workspaces():
 78-        if (
 79-            workspace.name == ''
 80-            or workspace.name == ''
 81-            or workspace.name == ''
 82-        ):
 83+        if workspace.name in ['', '', '']:
 84             continue
 85 
 86         name_parts = parse_workspace_name(workspace.name)
 87@@ -179,7 +179,7 @@ def rename_workspaces(i3):
 88 
 89 # rename workspaces to just numbers and shortnames.
 90 # called on exit to indicate that this script is no longer running.
 91-def undo_window_renaming(i3):
 92+def undo_window_renaming(i3) -> NoReturn:
 93     for workspace in i3.get_tree().workspaces():
 94         name_parts = parse_workspace_name(workspace.name)
 95         name_parts['icons'] = None
 96@@ -187,7 +187,7 @@ def undo_window_renaming(i3):
 97         i3.command(f'rename workspace "{workspace.name}" to "{new_name}"')
 98 
 99     i3.main_quit()
100-    sys.exit(0)
101+    exit(0)
102 
103 
104 if __name__ == '__main__':