-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathi3plug.py
executable file
·51 lines (41 loc) · 1.52 KB
/
i3plug.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
#!/usr/bin/env python3
import i3ipc
import sys
import pickle
import os
import pwd
# logged_in_user = os.getlogin() # does not work for non-interactive shells
logged_in_user = pwd.getpwuid(os.geteuid())[0] # we think this is only one
PATH = "/tmp/.i3_workspace_mapping_{}".format(logged_in_user)
def showHelp():
print(sys.argv[0] + " [save|restore]")
if __name__ == '__main__':
if len(sys.argv) < 2:
showHelp()
sys.exit(1)
i3 = i3ipc.Connection()
if sys.argv[1] == 'save':
pickle.dump(i3.get_workspaces(), open(PATH, "wb"))
elif sys.argv[1] == 'restore':
try:
workspace_mapping = pickle.load(open(PATH, "rb"))
except FileNotFoundError:
print("Can't find existing mappings...")
sys.exit(1)
for workspace in workspace_mapping:
i3.command(f"workspace {workspace.name}")
i3.command(f"move workspace to output {workspace.output}")
for workspace in filter(lambda w: w.visible, workspace_mapping):
i3.command(f"workspace {workspace.name}")
elif sys.argv[1] == 'show':
try:
workspace_mapping = pickle.load(open(PATH, "rb"))
print(f"Loaded workspace from {PATH}")
except FileNotFoundError:
print("Can't find mappings in {}".format(PATH))
sys.exit(1)
for workspace in workspace_mapping:
print(f"{workspace.name} is on {workspace.output} and is visible: {workspace.visible}")
else:
showHelp()
sys.exit(1)