Skip to content

Commit e7adcdd

Browse files
author
Tony Crisci
authored
Merge pull request #71 from julius383/add_example_script
Add script that makes sure workspace numbers are always consecutive
2 parents 8d9ec80 + 2f55697 commit e7adcdd

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

examples/workspace_renumber.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/usr/bin/env python3
2+
3+
import i3ipc
4+
5+
# make connection to i3 ipc
6+
i3 = i3ipc.Connection()
7+
8+
# check if workspaces are all in order
9+
def workspaces_ordered(i3conn):
10+
last_workspace = 0
11+
for i in sorted(i3conn.get_workspaces(), key=lambda x: x['num']):
12+
number = int(i['num'])
13+
if number != last_workspace+1:
14+
return False
15+
last_workspace += 1
16+
return True
17+
18+
# find all the workspaces that are out of order and
19+
# the least possible valid workspace number that is unassigned
20+
def find_disordered(i3conn):
21+
disordered = []
22+
least_number = None
23+
workspaces = sorted(i3conn.get_workspaces(), key=lambda x: x['num'])
24+
occupied_workspaces = [int(x['num']) for x in workspaces]
25+
last_workspace = 0
26+
for i in workspaces:
27+
number = int(i['num'])
28+
if number != last_workspace+1:
29+
disordered.append(number)
30+
if least_number is None and last_workspace + 1 not in occupied_workspaces:
31+
least_number = last_workspace + 1
32+
last_workspace += 1
33+
return (disordered, least_number)
34+
35+
# renumber all the workspaces that appear out of order from the others
36+
def fix_ordering(i3conn):
37+
if workspaces_ordered(i3conn):
38+
return
39+
else:
40+
workspaces = i3conn.get_tree().workspaces()
41+
disordered_workspaces,least_number = find_disordered(i3conn)
42+
containers = list(filter(lambda x: x.num in disordered_workspaces, workspaces))
43+
for c in containers:
44+
for i in c.leaves():
45+
i.command("move container to workspace %s" % least_number)
46+
least_number += 1
47+
return
48+
49+
# callback for when workspace focus changes
50+
def on_workspace_focus(i3conn, e):
51+
fix_ordering(i3conn)
52+
53+
54+
if __name__ == '__main__':
55+
i3.on('workspace::focus', on_workspace_focus)
56+
i3.main()
57+

0 commit comments

Comments
 (0)