-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathworkspace_renumber.py
executable file
·60 lines (48 loc) · 1.77 KB
/
workspace_renumber.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
#!/usr/bin/env python3
import i3ipc
# make connection to i3 ipc
i3 = i3ipc.Connection()
# check if workspaces are all in order
def workspaces_ordered(i3conn):
last_workspace = 0
for i in sorted(i3conn.get_workspaces(), key=lambda x: x.num):
number = int(i.num)
if number != last_workspace + 1:
return False
last_workspace += 1
return True
# find all the workspaces that are out of order and
# the least possible valid workspace number that is unassigned
def find_disordered(i3conn):
disordered = []
least_number = None
workspaces = sorted(i3conn.get_workspaces(), key=lambda x: x.num)
occupied_workspaces = [int(x.num) for x in workspaces]
last_workspace = 0
for i in workspaces:
number = int(i.num)
if number != last_workspace + 1:
disordered.append(number)
if least_number is None and last_workspace + 1 not in occupied_workspaces:
least_number = last_workspace + 1
last_workspace += 1
return (disordered, least_number)
# renumber all the workspaces that appear out of order from the others
def fix_ordering(i3conn):
if workspaces_ordered(i3conn):
return
else:
workspaces = i3conn.get_tree().workspaces()
disordered_workspaces, least_number = find_disordered(i3conn)
containers = list(filter(lambda x: x.num in disordered_workspaces, workspaces))
for c in containers:
for i in c.leaves():
i.command("move container to workspace %s" % least_number)
least_number += 1
return
# callback for when workspace focus changes
def on_workspace_focus(i3conn, e):
fix_ordering(i3conn)
if __name__ == '__main__':
i3.on('workspace::focus', on_workspace_focus)
i3.main()