diff options
Diffstat (limited to 'contrib/autoname-workspaces.py')
-rwxr-xr-x | contrib/autoname-workspaces.py | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/contrib/autoname-workspaces.py b/contrib/autoname-workspaces.py deleted file mode 100755 index 3ec39928..00000000 --- a/contrib/autoname-workspaces.py +++ /dev/null | |||
@@ -1,124 +0,0 @@ | |||
1 | #!/usr/bin/python | ||
2 | |||
3 | # This script requires i3ipc-python package (install it from a system package manager | ||
4 | # or pip). | ||
5 | # It adds icons to the workspace name for each open window. | ||
6 | # Set your keybindings like this: set $workspace1 workspace number 1 | ||
7 | # Add your icons to WINDOW_ICONS. | ||
8 | # Based on https://github.com/maximbaz/dotfiles/blob/master/bin/i3-autoname-workspaces | ||
9 | |||
10 | import argparse | ||
11 | import i3ipc | ||
12 | import logging | ||
13 | import re | ||
14 | import signal | ||
15 | import sys | ||
16 | |||
17 | WINDOW_ICONS = { | ||
18 | "firefox": "", | ||
19 | } | ||
20 | |||
21 | DEFAULT_ICON = "" | ||
22 | |||
23 | |||
24 | def icon_for_window(window): | ||
25 | name = None | ||
26 | if window.app_id is not None and len(window.app_id) > 0: | ||
27 | name = window.app_id.lower() | ||
28 | elif window.window_class is not None and len(window.window_class) > 0: | ||
29 | name = window.window_class.lower() | ||
30 | |||
31 | if name in WINDOW_ICONS: | ||
32 | return WINDOW_ICONS[name] | ||
33 | |||
34 | logging.info("No icon available for window with name: %s" % str(name)) | ||
35 | return DEFAULT_ICON | ||
36 | |||
37 | def rename_workspaces(ipc): | ||
38 | for workspace in ipc.get_tree().workspaces(): | ||
39 | name_parts = parse_workspace_name(workspace.name) | ||
40 | icon_tuple = () | ||
41 | for w in workspace: | ||
42 | if w.app_id is not None or w.window_class is not None: | ||
43 | icon = icon_for_window(w) | ||
44 | if not ARGUMENTS.duplicates and icon in icon_tuple: | ||
45 | continue | ||
46 | icon_tuple += (icon,) | ||
47 | name_parts["icons"] = " ".join(icon_tuple) + " " | ||
48 | new_name = construct_workspace_name(name_parts) | ||
49 | ipc.command('rename workspace "%s" to "%s"' % (workspace.name, new_name)) | ||
50 | |||
51 | |||
52 | def undo_window_renaming(ipc): | ||
53 | for workspace in ipc.get_tree().workspaces(): | ||
54 | name_parts = parse_workspace_name(workspace.name) | ||
55 | name_parts["icons"] = None | ||
56 | new_name = construct_workspace_name(name_parts) | ||
57 | ipc.command('rename workspace "%s" to "%s"' % (workspace.name, new_name)) | ||
58 | ipc.main_quit() | ||
59 | sys.exit(0) | ||
60 | |||
61 | |||
62 | def parse_workspace_name(name): | ||
63 | return re.match( | ||
64 | "(?P<num>[0-9]+):?(?P<shortname>\w+)? ?(?P<icons>.+)?", name | ||
65 | ).groupdict() | ||
66 | |||
67 | |||
68 | def construct_workspace_name(parts): | ||
69 | new_name = str(parts["num"]) | ||
70 | if parts["shortname"] or parts["icons"]: | ||
71 | new_name += ":" | ||
72 | |||
73 | if parts["shortname"]: | ||
74 | new_name += parts["shortname"] | ||
75 | |||
76 | if parts["icons"]: | ||
77 | new_name += " " + parts["icons"] | ||
78 | |||
79 | return new_name | ||
80 | |||
81 | |||
82 | if __name__ == "__main__": | ||
83 | parser = argparse.ArgumentParser( | ||
84 | description="This script automatically changes the workspace name in sway depending on your open applications." | ||
85 | ) | ||
86 | parser.add_argument( | ||
87 | "--duplicates", | ||
88 | "-d", | ||
89 | action="store_true", | ||
90 | help="Set it when you want an icon for each instance of the same application per workspace.", | ||
91 | ) | ||
92 | parser.add_argument( | ||
93 | "--logfile", | ||
94 | "-l", | ||
95 | type=str, | ||
96 | default="/tmp/sway-autoname-workspaces.log", | ||
97 | help="Path for the logfile.", | ||
98 | ) | ||
99 | args = parser.parse_args() | ||
100 | global ARGUMENTS | ||
101 | ARGUMENTS = args | ||
102 | |||
103 | logging.basicConfig( | ||
104 | level=logging.INFO, | ||
105 | filename=ARGUMENTS.logfile, | ||
106 | filemode="w", | ||
107 | format="%(message)s", | ||
108 | ) | ||
109 | |||
110 | ipc = i3ipc.Connection() | ||
111 | |||
112 | for sig in [signal.SIGINT, signal.SIGTERM]: | ||
113 | signal.signal(sig, lambda signal, frame: undo_window_renaming(ipc)) | ||
114 | |||
115 | def window_event_handler(ipc, e): | ||
116 | if e.change in ["new", "close", "move"]: | ||
117 | rename_workspaces(ipc) | ||
118 | |||
119 | ipc.on("window", window_event_handler) | ||
120 | |||
121 | rename_workspaces(ipc) | ||
122 | |||
123 | ipc.main() | ||
124 | |||