/
i3layout.py
278 lines (251 loc) · 7.81 KB
/
i3layout.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python2.7
import sys
import getopt
import time
import logging
import ConfigParser
from os import getenv
from os.path import exists
import numpy as np
import i3
FORMAT = "%(module)s.%(funcName)s %(levelname)s: %(message)s"
UNIT = 100
def layout2arr(layout):
logging.debug("input: %s", layout)
a = layout.split('\n')
b = []
for line in a:
if len(line) > 0:
b.append(line)
d = []
for line in b:
for n in line:
d.append(int(n))
res = np.array(d).reshape(len(b),len(b[0]))
logging.debug("output: %s", res)
return res
def load_config(path):
tasks = []
idx = 0
with open(path, 'r') as f:
layout = ""
progs = []
for line in f:
line = line.replace('\n','')
logging.debug("current line: %s", line)
line = line.strip()
if line.startswith("//"):
continue
elif line.startswith('#'):
line = line.replace('#','')
line = line.strip()
layout_mode = True
if ":" in line:
name, ws = line.split(":")
else:
idx += 1
name = line
ws = str(idx)
elif line.startswith('-'):
layout_mode = False
elif len(line) == 0:
logging.debug("add layout")
tasks.append({'name': name, 'workspace': ws,
'layout': layout, 'progs': progs})
layout = ""
progs = []
else:
if layout_mode:
layout += line + "\n"
else:
progs.append(line)
tasks.append({'name': name, 'workspace': ws,
'layout': layout, 'progs': progs})
return tasks
def vertical(mat, idx):
if idx < mat.shape[0]:
logging.info("split at %d", idx)
return (np.vsplit(mat, [idx]), mat[idx, 0])
else:
logging.info("split overrun")
return False, None
def horizontal(mat, idx):
if idx < mat.shape[1]:
logging.info("split at %d", idx)
return (np.hsplit(mat, [idx]), mat[0,idx])
else:
logging.info("split overrun")
return False, None
def split_check(x,y):
logging.debug("\n%s\n---\n%s", x, y)
for idx in np.unique(x):
if idx in np.unique(y):
logging.info("split failed")
return False
logging.info("split worked")
return True
def step(arr, func, nfunc, flagged=False):
logging.debug("fields: %s", np.unique(arr))
if np.unique(arr).shape[0] == 1:
logging.info("found leaf")
return True, None
else:
idx = 1
c = func(arr, idx)
s = c[0]
while(s != False):
if split_check(s[0],s[1]):
l, lop = step(s[0], nfunc, func)
r, rop= step(s[1], nfunc, func)
if l and r:
op = [(func.__name__, c[1], idx),lop, rop]
return True, op
idx += 1
c = func(arr, idx)
s = c[0]
if flagged:
return False, None
return step(arr, nfunc, func, True)
def i3conv(arr, dim_x, dim_y, progs):
logging.debug("current field size %d : %d", dim_x, dim_y)
if arr is None:
return []
op = arr[0][0]
prog = arr[0][1]
idx = arr[0][2]
ops = [("split", op), ("exec", progs[prog-1])]
if op == "horizontal":
if idx != dim_x / 2.0:
dx = dim_x / 2.0 - idx
if dx < 0:
com = "shrink"
else:
com = "grow"
ops.append(("resize", "%s left %d px or %d ppt" % (com, (abs(dx) * UNIT), abs(dx/float(dim_x)) * 100 )))
new_dim_x_r = idx
new_dim_x_l = dim_x - new_dim_x_r
else:
new_dim_x_l = new_dim_x_r = dim_x
if op == "vertical":
if idx != dim_y / 2.0:
dy = dim_y / 2.0 - idx
if dy < 0:
com = "shrink"
else:
com = "grow"
ops.append(("resize", "%s up %d px or %d ppt" % (com, (abs(dy) * UNIT), (abs(dy/float(dim_y))) * 100)))
new_dim_y_r = idx
new_dim_y_l = dim_y - new_dim_y_r
else:
new_dim_y_l = new_dim_y_r = dim_y
lops = i3conv(arr[2], new_dim_x_l, new_dim_y_l, progs)
rops = i3conv(arr[1], new_dim_x_r, new_dim_y_r, progs)
if lops is not 'x':
ops.extend(lops)
if rops is not 'x':
if op == "vertical":
back_op = ("focus", "up")
else:
back_op = ("focus", "left")
ops.append(back_op)
ops.extend(rops)
return ops
#i3.command('workspace','4')
#i3.command('layout','default')
#i3.command('exec','gnome-terminal')
#i3.command('exec','gnome-terminal')
class Usage(Exception):
def __init__(self, msg):
self.msg = msg
def main(argv=None):
if argv is None:
argv = sys.argv
try:
try:
opts, args = getopt.getopt(argv[1:], "hxvdi",
["help"])
ws = layout = progs = None
simulate = load = True
verbose = debug = interactive = store = False
for o, a in opts:
if o in ('h', "help"):
raise Usage(None)
elif o == '-x':
simulate = False
elif o == '-v':
verbose = True
elif o == '-d':
debug = True
elif o == '-i':
interactive = True
load = False
except getopt.error, msg:
raise Usage(msg)
# more code, unchanged
except Usage, err:
print >>sys.stderr, err.msg
print >>sys.stderr, "for help use --help"
return 2
lvl = 'CRITICAL'
if debug:
lvl = 'DEBUG'
elif verbose:
lvl = 'INFO'
logging.basicConfig(format=FORMAT, level=lvl, stream=sys.stdout)
if interactive:
ws = raw_input('set workspace: ')
print "enter layout"
layout = ""
line = raw_input(">")
while(line != ''):
layout += "%s\n" % line
line = raw_input(">")
print "choose programs:"
progs = []
idx = 1
line = raw_input("%d: " %idx)
while(line != ''):
progs.append(line)
idx += 1
line = raw_input("%d: " % idx)
tasks = [{'name': None, 'workspace': ws,
'layout': layout, 'progs': progs}]
elif load:
cfg_file = "%s/.i3/layouts"%getenv("HOME")
if exists(cfg_file):
tasks = load_config(cfg_file)
else:
print "ERROR: No layout file found. Please use the\
interactive mode or use %s." % cfg_file
f = open(cfg_file,"w")
f.close()
tasks = []
for task in tasks:
logging.info("current task: %s" % task)
ws = task['workspace']
layout = task['layout']
progs = task['progs']
x = layout2arr(layout)
r = step(x, horizontal, vertical)
logging.info("path: %s", r)
if r[0]:
#print r
op = [("exec", progs[x[0,0]-1])]
op.extend(i3conv(r[1],x.shape[1], x.shape[0], progs))
logging.info("result: %s", op)
if not simulate:
i3.command("workspace", ws)
time.sleep(0.1)
for i in op:
logging.debug("next op: %s", i)
if not simulate:
i3.command(i[0],i[1])
if i[0] == "exec":
time.sleep(0.5)
else:
time.sleep(0.1)
else:
print "ERROR: No path found for layout!"
print layout
if __name__ == "__main__":
sys.exit(main())