-
Notifications
You must be signed in to change notification settings - Fork 1
/
i3_long_split.py
executable file
·100 lines (81 loc) · 2.6 KB
/
i3_long_split.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#!/usr/bin/env python3
import getopt
import sys
import os
from i3ipc import Connection, Event
import time
last_focus_con = None
def find_parent(i3, window_id):
"""
Find the parent of a given window id
"""
def finder(con, parent):
if con.id == window_id:
return parent
for node in con.nodes:
res = finder(node, con)
if res:
return res
return None
return finder(i3.get_tree(), None)
def split_move_new(i3, e):
parent = find_parent(i3, e.container.id)
# First window on a workspace:
if parent is None or (parent.workspace() is parent and len(parent.nodes) == 1):
return
tall = last_focus_con.rect.height > last_focus_con.rect.width
if len(parent.nodes) == 2:
# If two nodes, then no new split is needed
if tall:
command = f"[con_id={last_focus_con.id}] layout splitv"
else:
command = f"[con_id={last_focus_con.id}] layout splith"
else:
if tall:
command = f"[con_id={last_focus_con.id}] split v; "
else:
command = f"[con_id={last_focus_con.id}] split h; "
playout = parent.layout
if playout == "splitv" or playout == "stacked":
command += f"[con_id={e.container.id}] move up"
elif playout == "splith" or playout == "tabbed":
command += f"[con_id={e.container.id}] move left"
i3.command(command)
def record_focus(i3, e):
global last_focus_con
last_focus_con = i3.get_tree().find_by_id(e.container.id)
def split_none(i3, e):
i3.command("split none")
def print_help():
print("Usage: " + sys.argv[0] + " [-p path/to/pid.file]")
print("")
print("Options:")
print(" -p path/to/pid.file Saves the PID for this program in the filename specified")
print("")
def main():
"""
Main function - listen for window focus
changes and call set_layout when focus
changes
"""
opt_list, _ = getopt.getopt(sys.argv[1:], 'hp:')
pid_file = None
for opt in opt_list:
if opt[0] == "-h":
print_help()
sys.exit()
if opt[0] == "-p":
pid_file = opt[1]
if pid_file:
with open(pid_file, 'w') as f:
f.write(str(os.getpid()))
i3 = Connection()
global last_focus_con
last_focus_con = i3.get_tree().find_focused()
i3.on(Event.WINDOW_NEW, split_move_new)
i3.on(Event.WINDOW_FOCUS, record_focus)
if os.getenv("SWAYSOCK") is not None:
i3.on(Event.WINDOW_CLOSE, split_none)
i3.main()
if __name__ == "__main__":
main()