7a6cb1f28aa052fccf14a47e7f211dbe57299003

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

Message

Update i3 autoname script

Diff

 1diff --git a/dot_config/i3/i3-autoname-workspaces.py b/dot_config/i3/i3-autoname-workspaces.py
 2index 695929c31aeeb91b20020c0c19b1d36f6bd941aa..deab332f60b50561a7ae2b9c153112b313e37f24 100644
 3--- a/dot_config/i3/i3-autoname-workspaces.py
 4+++ b/dot_config/i3/i3-autoname-workspaces.py
 5@@ -62,6 +62,7 @@ WINDOW_ICONS: dict[str, str] = {
 6     'gimp-2.10': nf.icons['fa_paint_brush'],
 7     'gparted': nf.icons['fa_hdd_o'],
 8     'gsmartcontrol': nf.icons['fa_hdd_o'],
 9+    'hoppscotch-app': nf.icons['md_ufo'],
10     'inkscape': nf.icons['md_fountain_pen_tip'],
11     'jetbrains-idea-ce': nf.icons['fa_code'],
12     'jetbrains-studio': nf.icons['fa_code'],
13@@ -109,11 +110,11 @@ DEFAULT_ICON = '*'
14 
15 
16 # Used so that we can keep the workspace's name when we add icons to it.
17-# Returns a dictionary with the following keys: 'num', 'shortname', and 'icons'
18+# Returns a dictionary with the 'num' and 'icons' keys
19 # Any field that's missing in @name will be None in the returned dict
20 def parse_workspace_name(name) -> dict[str, Any]:
21     match = re.match(
22-        r'(?P<num>\d+):?(?P<shortname>\w+)? ?(?P<icons>.+)?',
23+        r'(?P<num>\d+):? ?(?P<icons>.+)?',
24         name,
25     )
26     if match is None:
27@@ -122,20 +123,16 @@ def parse_workspace_name(name) -> dict[str, Any]:
28     return match.groupdict()
29 
30 
31-# Given a dictionary with 'num', 'shortname', 'icons',
32+# Given a dictionary with 'num', 'icons',
33 # return the formatted name by concatenating them together.
34 def construct_workspace_name(parts) -> str:
35     new_name: str = str(parts['num'])
36-    if parts['shortname'] or parts['icons']:
37+    if parts['icons']:
38         new_name += ':'
39+        for icon in parts['icons']:
40+            new_name += ' ' + icon
41 
42-        if parts['shortname']:
43-            new_name += parts['shortname']
44-
45-        if parts['icons']:
46-            new_name += ' ' + parts['icons']
47-
48-    if len(new_name) > 1:
49+    if len(new_name) > 1 and not new_name.endswith('*'):
50         new_name += ' '
51 
52     return new_name
53@@ -160,12 +157,12 @@ def xprop(win_id, property):
54 def icon_for_window(window) -> str:
55     classes: list[str] | None = xprop(window.window, 'WM_CLASS')
56     if classes is None or len(classes) == 0:
57-        print(f'No icon available for: {classes}')
58+        print(f'No class set for window: {xprop(window.window, "WM_NAME")}')
59         return DEFAULT_ICON
60 
61     for cls in map(lambda x: x.lower(), classes):
62         if 'minecraft' in cls:
63-            return WINDOW_ICONS['minecraft']
64+            return WINDOW_ICONS["minecraft"]
65 
66         if cls in WINDOW_ICONS:
67             return WINDOW_ICONS[cls]
68@@ -182,14 +179,14 @@ def rename_workspaces(i3) -> None:
69             continue
70 
71         name_parts = parse_workspace_name(workspace.name)
72-        name_parts['icons'] = ' '.join(
73+        name_parts['icons'] = [
74             icon_for_window(w) for w in workspace.leaves()
75-        )
76+        ]
77         new_name = construct_workspace_name(name_parts)
78         i3.command(f'rename workspace "{workspace.name}" to "{new_name}"')
79 
80 
81-# rename workspaces to just numbers and shortnames.
82+# rename workspaces to just numbers
83 # called on exit to indicate that this script is no longer running.
84 def undo_window_renaming(i3) -> NoReturn:
85     for workspace in i3.get_tree().workspaces():