Skip to content

Instantly share code, notes, and snippets.

@krkhan
Created June 25, 2020 21:23
Show Gist options
  • Save krkhan/59feffa32ca0c33d1776e5da994eaf09 to your computer and use it in GitHub Desktop.
Save krkhan/59feffa32ca0c33d1776e5da994eaf09 to your computer and use it in GitHub Desktop.
Toggle i3 workspace layouts on the fly
#!/usr/bin/env python3
import argparse
import os
from pathlib import Path
from i3ipc import Connection
from time import sleep
parser = argparse.ArgumentParser()
parser.add_argument("layout", help="layout for workspaces")
args = parser.parse_args()
if args.layout not in ["tabbed", "stacked", "default"]:
print("invalid layout " + args.layout)
sed_command = "sed -i '/workspace_layout/c\\workspace_layout {layout}' {homedir}/.config/i3/config".format(layout=args.layout, homedir=Path.home())
print("Executing: " + sed_command)
os.system(sed_command)
reload_command = "i3-msg reload"
print("Executing: " + reload_command)
os.system(reload_command)
print()
i3 = Connection()
windows = i3.get_tree().leaves()
workspaces = i3.get_workspaces()
focused_window = i3.get_tree().find_focused()
focused_workspace = focused_window.workspace().name
workspace_windows = {}
for w in windows:
wsname = w.workspace().name
if not wsname in workspace_windows:
workspace_windows[wsname] = list()
workspace_windows[wsname].append(w)
for ws in workspaces:
i3.command("workspace {wsname}".format(wsname=ws.name))
wswindows = workspace_windows[ws.name]
print("Workspace '{wsname}' on output '{output}'".format(wsname=ws.name, output=ws.output))
print("\t" + "\n\t".join([w.window_title for w in wswindows]))
for w in wswindows:
w.command("move container to workspace tmp")
i3.command("workspace tmp")
for w in wswindows:
w.command("move container to workspace {wsname}".format(wsname=ws.name))
i3.command("workspace " + ws.name)
i3.command("workspace {wsname}".format(wsname=focused_workspace))
for w in i3.get_tree().leaves():
if w.window_title == focused_window.window_title:
w.command("focus")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment