Skip to content

Instantly share code, notes, and snippets.

@k2nr k2nr/wa
Created Apr 8, 2019

Embed
What would you like to do?
window arrangement
#!/usr/bin/env python3
import subprocess
import os
import argparse
wdir = os.environ["HOME"]+"/.wa"
def get(command):
return subprocess.check_output(["/bin/bash", "-c", command]).decode("utf-8")
def winlist_path(name):
return wdir + "/" + name
def check_window(w_id):
w_type = get("xprop -id "+w_id)
if " _NET_WM_WINDOW_TYPE_NORMAL" in w_type:
return True
else:
return False
def read_windows(args):
w_list = [l.split()[:7] for l in get("wmctrl -lGx").splitlines()]
relevant = [(" ").join(w[2:7]) for w in w_list if check_window(w[0]) == True]
winlist = winlist_path(args.name)
with open(winlist, "wt") as out:
for item in relevant:
print(item)
out.write(item+"\n")
def restore_windows(args):
winlist = winlist_path(args.name)
try:
wlist = [l.split() for l in open(winlist).read().splitlines()]
except FileNotFoundError:
print(args.name + " not found")
pass
else:
for w in wlist:
try:
cmd = "wmctrl -xr "+w[4]+" -e 0,"+(",").join(w[0:4])
subprocess.Popen(["/bin/bash", "-c", cmd])
except:
pass
parser = argparse.ArgumentParser()
parser.add_argument("name", type=str, default="default")
parser.add_argument("--dump", action="store_true", default=False)
args = parser.parse_args()
os.makedirs(wdir, exist_ok=True)
if args.dump:
read_windows(args)
else:
restore_windows(args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.