Skip to content

Instantly share code, notes, and snippets.

@mrzor
Created July 8, 2020 13:40
Show Gist options
  • Save mrzor/aa13ca447ccbeefbefee93def29b7f35 to your computer and use it in GitHub Desktop.
Save mrzor/aa13ca447ccbeefbefee93def29b7f35 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# Renames i3 workspaces that may not be using pretty names from Xresources
# (as defined by Regolith default configuration)
#
# Execute once on i3 startup (edit path as appropriate):
# exec --no-startup-id "python3 /home/<YOUR_USER>/.config/regolith/i3/rename_default_workspaces.py"
#
# Author: mrzor
# License: mrzor has waived all copyright and related or neighboring rights to rename_default_workspaces.py.
# License: https://creativecommons.org/publicdomain/zero/1.0/
import asyncio
import re
import pdb
import os
from i3ipc.aio import Connection
async def load_workspace_names():
result = {}
workspace_regexp = "i3-wm\.workspace\.(\d+)\.name:(.+)$"
# Load all Xresources
proc = await asyncio.create_subprocess_shell(
"xrdb -query",
stdout=asyncio.subprocess.PIPE,
stderr=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
lines = stdout.decode().split('\n')
# Filter them out and map them by workspace number
for line in lines:
match = re.match(workspace_regexp, line)
if match:
default_workspace_name = str(int(match[1])) # no leading zeroes
result[default_workspace_name] = match[2]
return result
async def main():
# load workspace
i3 = await Connection(auto_reconnect=True).connect()
configured_workspaces = await load_workspace_names()
workspaces = await i3.get_workspaces()
for name in [workspace.name for workspace in workspaces]:
print(f"found {name}")
if name in configured_workspaces.keys():
cmd = f"rename workspace {name} to {configured_workspaces[name]}"
print(cmd)
await i3.command(cmd)
asyncio.run(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment