Sway: toggle gaps script
This commit is contained in:
parent
48095e853a
commit
618f03668a
1 changed files with 89 additions and 0 deletions
89
sway/toggle_gaps.py
Executable file
89
sway/toggle_gaps.py
Executable file
|
@ -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 <vert> <horiz> # Toggle between reset and gaps
|
||||
# ./toggle_gaps.py toggle <vertical> <horizontal>
|
||||
# ./toggle_gaps.py all <vertical> <horizontal>
|
||||
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 <vert> <horiz> # Toggle between reset and gaps
|
||||
./toggle_gaps.py toggle <vertical> <horizontal>
|
||||
./toggle_gaps.py all <vertical> <horizontal>"""
|
||||
)
|
||||
|
||||
|
||||
# ╔───────────────────────────────────────────────────────────────────────────╗
|
||||
# │ 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)
|
Loading…
Reference in a new issue