diff options
author | Thorben Günther <admin@xenrox.net> | 2019-12-05 13:21:07 +0100 |
---|---|---|
committer | Drew DeVault <sir@cmpwn.com> | 2019-12-06 15:52:33 -0500 |
commit | 1cad8624580eadf5b876b23727f2b9ea0b9bc057 (patch) | |
tree | 10770a03c5d842dbcf29c4ad6b61c811402c4cd4 /contrib | |
parent | Improve transparency script (diff) | |
download | sway-1cad8624580eadf5b876b23727f2b9ea0b9bc057.tar.gz sway-1cad8624580eadf5b876b23727f2b9ea0b9bc057.tar.zst sway-1cad8624580eadf5b876b23727f2b9ea0b9bc057.zip |
A Script to change sway workspace name.
This script automatically changes the workspace name when an application
gets closed, moved or openend.
Diffstat (limited to 'contrib')
-rwxr-xr-x | contrib/autoname-workspaces.py | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/contrib/autoname-workspaces.py b/contrib/autoname-workspaces.py new file mode 100755 index 00000000..297d91b2 --- /dev/null +++ b/contrib/autoname-workspaces.py | |||
@@ -0,0 +1,130 @@ | |||
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 | app_id = window.app_id | ||
26 | if app_id is not None and len(app_id) > 0: | ||
27 | app_id = app_id.lower() | ||
28 | if app_id in WINDOW_ICONS: | ||
29 | return WINDOW_ICONS[app_id] | ||
30 | logging.info("No icon available for window with app_id: %s" % str(app_id)) | ||
31 | else: | ||
32 | # xwayland support | ||
33 | class_name = window.window_class | ||
34 | if len(class_name) > 0: | ||
35 | class_name = class_name.lower() | ||
36 | if class_name in WINDOW_ICONS: | ||
37 | return WINDOW_ICONS[class_name] | ||
38 | logging.info( | ||
39 | "No icon available for window with class_name: %s" % str(class_name) | ||
40 | ) | ||
41 | return DEFAULT_ICON | ||
42 | |||
43 | |||
44 | def rename_workspaces(ipc): | ||
45 | for workspace in ipc.get_tree().workspaces(): | ||
46 | name_parts = parse_workspace_name(workspace.name) | ||
47 | icon_tuple = () | ||
48 | for w in workspace: | ||
49 | if w.app_id is not None or w.window_class is not None: | ||
50 | icon = icon_for_window(w) | ||
51 | if not ARGUMENTS.duplicates and icon in icon_tuple: | ||
52 | continue | ||
53 | icon_tuple += (icon,) | ||
54 | name_parts["icons"] = " ".join(icon_tuple) + " " | ||
55 | new_name = construct_workspace_name(name_parts) | ||
56 | ipc.command('rename workspace "%s" to "%s"' % (workspace.name, new_name)) | ||
57 | |||
58 | |||
59 | def undo_window_renaming(ipc): | ||
60 | for workspace in ipc.get_tree().workspaces(): | ||
61 | name_parts = parse_workspace_name(workspace.name) | ||
62 | name_parts["icons"] = None | ||
63 | new_name = construct_workspace_name(name_parts) | ||
64 | ipc.command('rename workspace "%s" to "%s"' % (workspace.name, new_name)) | ||
65 | ipc.main_quit() | ||
66 | sys.exit(0) | ||
67 | |||
68 | |||
69 | def parse_workspace_name(name): | ||
70 | return re.match( | ||
71 | "(?P<num>[0-9]+):?(?P<shortname>\w+)? ?(?P<icons>.+)?", name | ||
72 | ).groupdict() | ||
73 | |||
74 | |||
75 | def construct_workspace_name(parts): | ||
76 | new_name = str(parts["num"]) | ||
77 | if parts["shortname"] or parts["icons"]: | ||
78 | new_name += ":" | ||
79 | |||
80 | if parts["shortname"]: | ||
81 | new_name += parts["shortname"] | ||
82 | |||
83 | if parts["icons"]: | ||
84 | new_name += " " + parts["icons"] | ||
85 | |||
86 | return new_name | ||
87 | |||
88 | |||
89 | if __name__ == "__main__": | ||
90 | parser = argparse.ArgumentParser( | ||
91 | description="This script automatically changes the workspace name in sway depending on your open applications." | ||
92 | ) | ||
93 | parser.add_argument( | ||
94 | "--duplicates", | ||
95 | "-d", | ||
96 | action="store_true", | ||
97 | help="Set it when you want an icon for each instance of the same application per workspace.", | ||
98 | ) | ||
99 | parser.add_argument( | ||
100 | "--logfile", | ||
101 | "-l", | ||
102 | type=str, | ||
103 | default="/tmp/sway-autoname-workspaces.log", | ||
104 | help="Path for the logfile.", | ||
105 | ) | ||
106 | args = parser.parse_args() | ||
107 | global ARGUMENTS | ||
108 | ARGUMENTS = args | ||
109 | |||
110 | logging.basicConfig( | ||
111 | level=logging.INFO, | ||
112 | filename=ARGUMENTS.logfile, | ||
113 | filemode="w", | ||
114 | format="%(message)s", | ||
115 | ) | ||
116 | |||
117 | ipc = i3ipc.Connection() | ||
118 | |||
119 | for sig in [signal.SIGINT, signal.SIGTERM]: | ||
120 | signal.signal(sig, lambda signal, frame: undo_window_renaming(ipc)) | ||
121 | |||
122 | def window_event_handler(ipc, e): | ||
123 | if e.change in ["new", "close", "move"]: | ||
124 | rename_workspaces(ipc) | ||
125 | |||
126 | ipc.on("window", window_event_handler) | ||
127 | |||
128 | rename_workspaces(ipc) | ||
129 | |||
130 | ipc.main() | ||