workspace switcher overhaul

This commit is contained in:
dalahast 2020-12-09 00:20:37 +01:00
parent d4db40e2e8
commit 35c42caf4d
Signed by: dalahast
GPG Key ID: 504A02165E7FA6B2

@ -1,12 +1,70 @@
#!/usr/bin/python #!/usr/bin/python
import json,sys,subprocess import sys
from i3ipc import Connection
args = sys.argv PRIMARY = "eDP-1"
msg = json.loads(subprocess.run(["swaymsg", "-t", "get_workspaces"], stdout=subprocess.PIPE).stdout.decode('utf-8'))
for i in range(len(msg)): class Switcher():
if msg[i]["focused"] == True: def __init__(self, connection):
if msg[i]["output"] == "DP-1": self.sway = connection
subprocess.run(["swaymsg", "workspace", str(args[1])]) if len(sys.argv) > 1:
if msg[i]["output"] == "DVI-D-1": self.ws = sys.argv[1]
subprocess.run(["swaymsg", "workspace", str(args[1]) + "P"]) else:
raise TypeError("At least one argument is required")
def find_focused_out(self):
outputs = self.sway.get_outputs()
for output in outputs:
if output.focused:
focused_out = output.name
return focused_out
def move_to_next(self):
focused_out = self.find_focused_out()
if focused_out == PRIMARY:
self.sway.command("move container to workspace {}".format(self.ws + "s"))
else:
self.sway.command("move container to workspace {}".format(self.ws))
def move_to(self):
focused_out = self.find_focused_out()
if focused_out == PRIMARY:
self.sway.command("move container to workspace {}".format(self.ws))
else:
self.sway.command("move container to workspace {}".format(self.ws + "s"))
def switch_ws(self):
focused_out = self.find_focused_out()
if focused_out == PRIMARY:
self.sway.command("workspace {}".format(self.ws))
else:
self.sway.command("workspace {}".format(self.ws + "s"))
def switch_other_ws(self):
focused_out = self.find_focused_out()
if focused_out == PRIMARY:
self.sway.command("workspace {}".format(self.ws + "s"))
else:
self.sway.command("workspace {}".format(self.ws))
def main():
switcher = Switcher(Connection())
if len(sys.argv) > 2:
flag = sys.argv[2]
else:
flag = None
if not flag:
switcher.switch_ws()
if flag == "-m":
switcher.move_to()
if flag == "-n":
switcher.move_to_next()
if flag == "-o":
switcher.switch_other_ws()
else:
raise ValueError("Unknown operation")
if __name__ == "__main__":
main()