-
Notifications
You must be signed in to change notification settings - Fork 0
/
i3swap
executable file
·66 lines (53 loc) · 1.82 KB
/
i3swap
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
#!/usr/bin/env python3
"""
Swap (secondary) workspaces with the "main" workspace (1 or 11), keeping their
names.
"""
import functools
import subprocess
import json
@functools.cache
def get_workspaces():
return json.loads(subprocess.check_output(['i3-msg', '-t', 'get_workspaces'],
encoding='utf-8'))
def current_workspace():
d = get_workspaces()
for s in d:
if s['focused']:
return s['num'], s['name']
def focus_workspace(num):
subprocess.run(["i3-msg", f"workspace number {num}"])
def main_workspace(d, reference_ws_num):
target = 1 if reference_ws_num < 10 else 11
for s in d:
if s['num'] == target:
return s['num'], s['name']
return target, str(target)
def switch_workspaces(ws1, ws2):
"""Arguments are pairs (num, name)
"""
print(ws1)
print(ws2)
num1, name1 = ws1
num2, name2 = ws2
new_name1 = f'{num2}:{extract_name_prefix(name1)}'.removesuffix(':')
new_name2 = f'{num1}:{extract_name_prefix(name2)}'.removesuffix(':')
print(['i3-msg', f"rename workspace {name1} to temporary; rename workspace {name2} to {new_name2}; rename workspace temporary to {new_name1}"])
subprocess.run(
['i3-msg', f"rename workspace {name1} to temporary; rename workspace {name2} to {new_name2}; rename workspace temporary to {new_name1}"])
def extract_name_prefix(name):
split = name.split(':')
if len(split) > 1:
return split[1].strip()
else:
return ''
def swap_current_workspace_with_main():
d = get_workspaces()
current_ws = current_workspace()
switch_workspaces(current_ws, main_workspace(d, current_ws[0]))
def swap():
current_num, _ = current_workspace()
swap_current_workspace_with_main()
focus_workspace(current_num)
if __name__ == '__main__':
swap()