Skip to content

Instantly share code, notes, and snippets.

@DanSnow
Last active July 3, 2019 11:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DanSnow/ed1ea7a28ff76150ab80dbd8f4014413 to your computer and use it in GitHub Desktop.
Save DanSnow/ed1ea7a28ff76150ab80dbd8f4014413 to your computer and use it in GitHub Desktop.
[i3][Experimental] Focus left window if exists, or switch to previous workspace
#!/usr/bin/env python3
import subprocess
import json
def workspace_prev():
subprocess.run(["i3-msg", "workspace", "prev"])
def focus_left():
subprocess.run(["i3-msg", "focus", "left"])
def get_layout():
res = subprocess.run(["i3-msg", "-t", "get_tree"], stdout=subprocess.PIPE)
return json.loads(res.stdout.decode("utf-8"))
def find_focus_window(node, parent=None):
node["parent"] = parent
if node["focused"]:
return node
for n in node["nodes"]:
res = find_focus_window(n, node)
if res is not None:
return res
def is_left_most_window(window):
if window["type"] == "workspace":
return True
parent = window["parent"]
orientation = parent["orientation"]
if orientation == "horizontal" and window is not parent["nodes"][0]:
return False
return is_left_most_window(parent)
focus_window = find_focus_window(get_layout())
if is_left_most_window(focus_window):
workspace_prev()
else:
focus_left()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment