#!/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()