From 618f03668a56f2e5051b01faede472adc121b31c Mon Sep 17 00:00:00 2001 From: Akemi Izuko Date: Tue, 30 Apr 2024 23:42:06 -0600 Subject: [PATCH] Sway: toggle gaps script --- sway/toggle_gaps.py | 89 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100755 sway/toggle_gaps.py diff --git a/sway/toggle_gaps.py b/sway/toggle_gaps.py new file mode 100755 index 0000000..8356990 --- /dev/null +++ b/sway/toggle_gaps.py @@ -0,0 +1,89 @@ +#!/usr/bin/env python3 +# Toggles between none and some gaps +# +# EXAMPLES: +# ./toggle_gaps.py reset # Force reset current workspace +# ./toggle_gaps.py toggle # Toggle between reset and gaps +# ./toggle_gaps.py toggle +# ./toggle_gaps.py all +import sys +import json +import subprocess + + +def get_workspaces(): + swaymsg = subprocess.Popen( + ["swaymsg", "-t", "get_workspaces", "--raw"], + stdout=subprocess.PIPE, + stderr=subprocess.DEVNULL, + ) + + stdout, _ = swaymsg.communicate(2) + return json.loads(stdout) + + +def get_current_workspace(): + workspaces = get_workspaces() + return next(filter(lambda x: x["focused"], workspaces)) + + +def has_gaps(workspace): + rect = workspace["rect"] + return rect["x"] > 0 or rect["y"] > 0 + + +def swaymsg_gaps(v, h, is_all): + for x, n in [[v, "vertical"], [h, "horizontal"]]: + swaymsg = subprocess.Popen( + [ + "swaymsg", + "gaps", + n, + "all" if is_all else "current", + "set", + str(x), + ] + ) + + swaymsg.communicate(1) + + +def toggle_workspace(ws, v, h): + if has_gaps(ws): + swaymsg_gaps(0, 0, False) + else: + swaymsg_gaps(v, h, False) + + +def print_help(): + print( + """\ +Toggles between none and some gaps + +EXAMPLES: +./toggle_gaps.py reset # Force reset current workspace +./toggle_gaps.py toggle # Toggle between reset and gaps +./toggle_gaps.py toggle +./toggle_gaps.py all """ + ) + + +# ╔───────────────────────────────────────────────────────────────────────────╗ +# │ Mαiη Prδgrαm | +# ╚───────────────────────────────────────────────────────────────────────────╝ + +if len(sys.argv) < 2: + print_help() + exit(1) +elif sys.argv[1] == "reset": + swaymsg_gaps(0, 0, False) +elif sys.argv[1] == "toggle": + v, h = sys.argv[2:4] + ws = get_current_workspace() + toggle_workspace(ws, v, h) +elif sys.argv[1] == "all": + v, h = sys.argv[2:4] + swaymsg_gaps(v, h, True) +else: + print_help() + exit(1)