Skip to content

Instantly share code, notes, and snippets.

@revarcline
Last active January 6, 2023 04:57
Show Gist options
  • Save revarcline/aa322d846ba206792b363e93a90216a8 to your computer and use it in GitHub Desktop.
Save revarcline/aa322d846ba206792b363e93a90216a8 to your computer and use it in GitHub Desktop.
Sway workspace tool
#!/bin/env python
import subprocess
import argparse
def get_args():
"""parse flags"""
parser = argparse.ArgumentParser(description="Move workspaces in sway")
parser.add_argument("-gn", "--go_next", action="store_true",
help="Next workspace")
parser.add_argument("-gp", "--go_previous",
action="store_true", help="Previous workspace")
parser.add_argument("-go", "--go_open", action="store_true",
help="Next open workspace (workspaces must be ints)")
parser.add_argument("-mn", "--move_next", action="store_true",
help="Next workspace")
parser.add_argument("-mp", "--move_previous",
action="store_true", help="Previous workspace")
parser.add_argument("-mo", "--move_open", action="store_true",
help="Next open workspace (workspaces must be ints)")
return parser.parse_args()
def get_current_workspace():
"""return current workspace"""
cmd = "swaymsg -t get_outputs | jq -r '.[] | .current_workspace'"
curr = subprocess.check_output(
cmd, shell=True).decode().strip().split('\n', maxsplit=1)[0]
print(f"Current workspace: {curr}")
return int(curr)
def all_workspaces():
"""all workspaces"""
cmd = "swaymsg -t get_workspaces | jq '.[] | .num'"
out = subprocess.check_output(cmd, shell=True).decode().strip().split('\n')
return [int(i) for i in out]
def first_missing(workspaces):
"""get index of first empty workspace"""
missing = [i for x, y in zip(workspaces, workspaces[1:])
for i in range(x + 1, y) if y - x > 1]
if len(missing) == 0:
return workspaces[-1] + 1
return missing[0]
def go_to_workspace(workspace):
"""to to workspace at index"""
print(f"Moving to {workspace}")
cmd = f"swaymsg workspace number {workspace}"
subprocess.run(cmd, check=True, shell=True)
def move_window_to_workspace(workspace):
"""move current window to to workspace at index"""
print(f"Moving to {workspace}")
cmd = f"swaymsg move container to workspace number {workspace}"
subprocess.run(cmd, check=True, shell=True)
def increment_workspace():
"""return next workspace"""
ws_arr = all_workspaces()
curr = get_current_workspace()
curr_index = ws_arr.index(curr)
new_index = curr_index+1
try:
new_workspace = ws_arr[new_index]
except IndexError:
new_workspace = ws_arr[0]
return new_workspace
def decrement_workspace():
"""return previous workspace"""
ws_arr = all_workspaces()
curr = get_current_workspace()
curr_index = ws_arr.index(curr)
new_index = curr_index-1
return ws_arr[new_index]
def next_open():
"""return next available"""
workspaces = all_workspaces()
new_ws = first_missing(workspaces)
return new_ws
def go_to_next():
"""go to next workspace"""
go_to_workspace(increment_workspace())
def go_to_previous():
"""go to previous workspace"""
go_to_workspace(decrement_workspace())
def go_to_open():
"""go to next open workspace"""
go_to_workspace(next_open())
def move_to_next():
"""move window to next workspace"""
workspace = increment_workspace()
move_window_to_workspace(workspace)
go_to_workspace(workspace)
def move_to_previous():
"""move window to previous workspace"""
workspace = decrement_workspace()
move_window_to_workspace(workspace)
go_to_workspace(workspace)
def move_to_open():
"""move window to next open workspace"""
workspace = next_open()
move_window_to_workspace(workspace)
go_to_workspace(workspace)
if __name__ == "__main__":
args = get_args()
if args.go_next:
print("Moving to next workspace")
go_to_next()
if args.go_previous:
print("Moving to previous workspace")
go_to_previous()
if args.go_open:
print("Moving to next available workspace")
go_to_open()
if args.move_next:
print("Moving window to next workspace")
move_to_next()
if args.move_previous:
print("Moving window to previous workspace")
move_to_previous()
if args.move_open:
print("Moving window to next available workspace")
move_to_open()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment