-
Notifications
You must be signed in to change notification settings - Fork 0
/
yabaiWindowLayout.py
74 lines (61 loc) · 1.96 KB
/
yabaiWindowLayout.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
#!/usr/bin/env python3
import json
import subprocess
import click
from click import echo
YABAI = '/usr/local/bin/yabai'
# put your generated configs here
configs = {
'default': [
{'app': 'Google Chrome', 'space': 1},
{'app': 'Notes', 'space': 1},
{'app': 'KeePassXC', 'space': 1},
{'app': 'iTerm2', 'space': 2},
{'app': 'Code', 'space': 3},
{'app': 'Slack', 'space': 4}
],
'2monitors': [
{'app': 'Google Chrome', 'space': 1},
{'app': 'Notes', 'space': 1},
{'app': 'KeePassXC', 'space': 1},
{'app': 'iTerm2', 'space': 2},
{'app': 'Slack', 'space': 4},
{'app': 'Mail', 'space': 5},
{'app': 'Calendar', 'space': 5},
{'app': 'Code', 'space': 9},
]
}
def run(cmd: str):
return subprocess.run(cmd, shell=True, capture_output=True).stdout
@click.command()
@click.argument('config', required=False, default=None)
def main(config):
if(not config):
# list all config names
echo(f'Available configs: {", ".join(configs.keys())}')
exit(0)
# get all windows
windows = json.loads(run(f'{YABAI} -m query --windows'))
if config not in configs:
# generate config from open windows
echo({
config: [
{
'app': window['app'],
'space': window['space']
}
for window in windows
]
})
else:
# restore window space from config
config = configs[config]
for window in windows:
space = next((x['space'] for x in config if x['app'] == window.get('app')), None)
if space:
# move only if space is diffferent
if window['space'] != space:
run(f'{YABAI} -m window {window["id"]} --space {space}')
print(f'{window["app"]} moved to workspace {space}')
if __name__ == '__main__':
main()