forked from mrconter1/i3_meta_workspaces
-
Notifications
You must be signed in to change notification settings - Fork 0
/
meta_workspaces.py
218 lines (188 loc) · 7.44 KB
/
meta_workspaces.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import os
import argparse
import subprocess
import json
import re
from subprocess import call
#Handle flags
parser = argparse.ArgumentParser()
parser.add_argument("-m", "--meta", help="Meta flag")
parser.add_argument("-w", "--workspace", help="Workspace flag")
parser.add_argument("-mw","--move_window", help="Move flag")
parser.add_argument("-r", "--rename", help="Rename Meta Workspace")
parser.add_argument("-d", "--delete", help="Delete current Meta Workspace")
#TODO add -d / --delete arg in order to nuke current workspace (if empty!)
args = parser.parse_args()
# Now populate current meta-workspace info for given $HOST$SDISPLAY
hostname = os.environ['HOSTNAME']
display = os.environ['DISPLAY']
#Name of stored meta variable
metaVarName = "meta_workspace"
#Correct path to stored variable
dirname = os.path.dirname(__file__)
variable_name = 'variables_' + hostname + display
filename = os.path.join(dirname, variable_name)
workspace_name = 'cur_ws_' + hostname + display
cur_ws = os.path.join(dirname, workspace_name)
ws_list = 'ws_list_' + hostname + display
wslist = os.path.join(dirname, ws_list)
ws_str = 'ws_str_' + hostname + display
wsstr = os.path.join(dirname, ws_str)
#cmd = os.system('zenity --question')
#cmd = os.system('i3-input')
def check_ws_empty(wsnum):
cmd = subprocess.Popen("i3-msg -t get_workspaces", stdout=subprocess.PIPE, shell=True)
output, err = cmd.communicate()
returncode = cmd.wait()
zzz = json.loads(output)
#No sorting required - leaving in for time being
zzz.sort(key=lambda ws: ws["name"])
zzz.sort(key=lambda ws: ws["output"])
#print(*zzz, sep=" \n")
#Find workspace names matching a regex
for i in zzz:
ws = i["name"]
#print(f"{ws}")
#Put pattern match into 2x groups
n = re.match(r"(?P<ws_num>\d)(?P<ws_win>\d)", ws)
#print(f"{n}")
if n:
#print(f"match={n.group('ws_num')} - {n.group('ws_win')} -- {wsnum}")
if int(n.group('ws_num')) == wsnum:
#We have found a workspace, this is a problem - cannot report as empty
return 1
#If we get to here we haven't found a workspace in the selected meta
return 0
def readMeta():
with open(filename) as fin:
for line in fin:
if line.startswith(metaVarName):
return line.split(":")[-1].strip()
def readWsList(meta,overwrite,delete):
outline = []
found = 0
with open(wslist) as fin:
for line in fin:
line = line.rstrip()
if line.startswith(meta):
found = 1
#If we're renaming a workspace
if overwrite:
cmd = subprocess.Popen('zenity --entry', stdout=subprocess.PIPE, shell=True)
output, err = cmd.communicate()
returncode = cmd.wait()
if returncode == 0:
name = "%s" % output.decode("utf-8").strip()
cur_ws_string = meta + ":" + name
outline.append(cur_ws_string + "")
else:
cur_ws_string = line
outline.append(cur_ws_string + "")
elif delete:
#TODO Get current meta
print(f'running delete on ws={meta}')
if check_ws_empty(int(meta)) == 0:
#We have an empty ws - don't print wsname
#Have to put an executable line in here lest we change the conditional
cmd = subprocess.Popen('zenity --notification --window-icon="info" --text="Workspace deleted!"', stdout=subprocess.PIPE, shell=True)
output, err = cmd.communicate()
returncode = cmd.wait()
else:
#Alert the user that the ws is not empty
cmd = subprocess.Popen('zenity --notification --window-icon="error" --text="Workspace not empty - cannot delete!"', stdout=subprocess.PIPE, shell=True)
output, err = cmd.communicate()
returncode = cmd.wait()
outline.append(line)
print(f'|->cant delete on ws={meta}')
else:
if line.split(":")[-1].strip() == "":
cmd = os.popen('zenity --entry').read()
cmd = cmd.split("output = ")[-1].strip()
cur_ws_string = meta + ":" + cmd
outline.append(cur_ws_string + "")
else:
outline.append(line)
cur_ws_string = line
else:
#check if format of line is correct
if re.match('\d+:', line):
cur_ws_string = line
outline.append(line)
if found == 0: #if we have scanned all lines and not found a match
cmd = os.popen('zenity --entry').read()
cur_ws_string = meta + ":" + cmd
outline.append(cur_ws_string)
fin.close
with open(wslist, 'w') as out:
for line in outline:
out.write(line + "\n")
if os.path.isfile(cur_ws):
os.remove(cur_ws)
with open(cur_ws, 'a') as out:
out.write(cur_ws_string)
statusline = ""
myoutline = sorted(outline, key = lambda a: a.split(":")[0])
for line in myoutline:
a = line.split(":")
disp = " " if (a[0] == meta) else " "
statusline += "["+disp+a[0]+":"+a[1]+"]"
with open(wsstr, 'w') as out:
out.write(statusline)
def writeMeta(value):
if os.path.isfile(filename):
os.remove(filename)
with open(filename, 'a') as out:
out.write(metaVarName + ":" + value + '\n')
#Create file if it does not exist
if not os.path.isfile(filename):
writeMeta("0")
#Create file if it does not exist
if not os.path.isfile(cur_ws):
writeMeta("0")
if not os.path.isfile(wslist):
with open(wslist, 'a') as out:
out.write("\n")
out.close()
if not os.path.isfile(wsstr):
with open(wsstr, 'a') as out:
out.write("\n")
out.close()
#Change meta workspace
if args.meta is not None:
meta = args.meta
print("change meta: " + meta)
#Now check to see if the meta ws has a name - if not, add one
readWsList(meta,0,0)
#Now write this out to cur_ws, then write meta
writeMeta(meta)
#Change workspace within meta workspace
if args.workspace is not None:
meta = readMeta()
workspace = args.workspace
print("change meta workspace: " + meta)
cmd = 'i3-msg "workspace ' + meta + workspace + ';"'
print(cmd)
os.system(cmd)
#Move window in between workspaces within meta workspace
if args.move_window is not None:
meta = readMeta()
print("move window between workspaces in current meta: " + meta)
workspace = args.move_window
cmd = 'i3-msg "move container to workspace ' + meta + workspace + ';"'
print(cmd)
os.system(cmd)
#Change meta workspace
if args.rename is not None:
#get meta value from cur_ws
meta = readMeta()
#update ws_list dictionary
#write out cur_ws
readWsList(meta,1,0)
#Delete current workspace provided it is empty
if args.delete is not None:
#get meta value from cur_ws
meta = readMeta()
#update ws_list dictionary
#write out cur_ws
readWsList(meta,0,1)
#TODO must set meta workspace to an existing workspace for safety reasons!