~em-dash/x-daemons

x-daemons/i3-workspace-namer.py -rwxr-xr-x 1.2 KiB
af585831Emily Ellis add auto-xrandr'ing thing 18 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
"""
Automatically rename i3 workspaces to list the windows in them, so that the i3bar shows them.
Start as a daemon at login.
When using this, it is recommended to bind your workspace switching keys to `workspace number 1` instead of `workspace 1`.
"""

import i3ipc

def name_for_container(con):
	if con.window_class is None:
		# probably a split container
		return None
	
	try:
		if con.window_class == 'code-oss':
			# vscode window title is ($filename - )$projectname - Code - OSS; pick the project name out of that
			parts = con.window_title.split(' - ')
			return parts[-3]
	except:
		pass

	return con.window_class.lower()

def rename_stuff(i3):
	try:
		tree = i3.get_tree()
		
		for workspace in tree.workspaces():
			name_segments = [name_for_container(con) for con in workspace.descendants()]
			name_segments = [ns for ns in name_segments if ns is not None]
			new_name = '\u202f\u2022\u202f'.join(name_segments)
			i3.command(f'rename workspace "{workspace.name}" to "{workspace.num}\u202f\u220d\u202f{new_name}"')
	except Exception as e:
		print(e)

def main():
	i3 = i3ipc.Connection(auto_reconnect = True)
	rename_stuff(i3)
	i3.on(i3ipc.Event.WINDOW, lambda *whatever: rename_stuff(i3))
	i3.main()

if __name__ == '__main__':
	main()