-
Notifications
You must be signed in to change notification settings - Fork 0
/
i3-workspace-rename.py
executable file
·214 lines (177 loc) · 7.53 KB
/
i3-workspace-rename.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
#!/usr/bin/env python3
# feature ideas:
# - save favorites in cache file and print them to be found by rofi
# TODO: write README.md (rofi combi example, requirements i3ipc, mode of
# support: best-effort, scratch own itch,
# force rename `!rename bla`)
# TODO: random color (optional: provide list)
# TODO: ":color" or "color:" to change color, same for id
from argparse import ArgumentParser
import re
# fmt: off
PANGO_COLOR_NAMES = {
"aliceblue", "antiquewhite", "aqua", "aquamarine", "azure", "beige",
"bisque", "black", "blanchedalmond", "blue", "blueviolet", "brown",
"burlywood", "cadetblue", "chartreuse", "chocolate", "coral",
"cornflowerblue", "cornsilk", "crimson", "cyan", "darkblue", "darkcyan",
"darkgoldenrod", "darkgray", "darkgreen", "darkgrey", "darkkhaki",
"darkmagenta", "darkolivegreen", "darkorange", "darkorchid", "darkred",
"darksalmon", "darkseagreen", "darkslateblue", "darkslategray",
"darkslategrey", "darkturquoise", "darkviolet", "deeppink", "deepskyblue",
"dimgray", "dimgrey", "dodgerblue", "firebrick", "floralwhite",
"forestgreen", "fuchsia", "gainsboro", "ghostwhite", "gold", "goldenrod",
"gray", "green", "greenyellow", "grey", "honeydew", "hotpink", "indianred",
"indigo", "ivory", "khaki", "lavender", "lavenderblush", "lawngreen",
"lemonchiffon", "lightblue", "lightcoral", "lightcyan",
"lightgoldenrodyellow", "lightgray", "lightgreen", "lightgrey",
"lightpink", "lightsalmon", "lightseagreen", "lightskyblue",
"lightslategray", "lightslategrey", "lightsteelblue", "lightyellow",
"lime", "limegreen", "linen", "magenta", "maroon", "mediumaquamarine",
"mediumblue", "mediumorchid", "mediumpurple", "mediumseagreen",
"mediumslateblue", "mediumspringgreen", "mediumturquoise",
"mediumvioletred", "midnightblue", "mintcream", "mistyrose", "moccasin",
"navajowhite", "navy", "oldlace", "olive", "olivedrab", "orange",
"orangered", "orchid", "palegoldenrod", "palegreen", "paleturquoise",
"palevioletred", "papayawhip", "peachpuff", "peru", "pink", "plum",
"powderblue", "purple", "rebeccapurple", "red", "rosybrown", "royalblue",
"saddlebrown", "salmon", "sandybrown", "seagreen", "seashell", "sienna",
"silver", "skyblue", "slateblue", "slategray", "slategrey", "snow",
"springgreen", "steelblue", "tan", "teal", "thistle", "tomato",
"turquoise", "violet", "wheat", "white", "whitesmoke", "yellow",
"yellowgreen",
}
# fmt: on
def pango_color_string(color):
"""Returns the string if it is a pango color, returns empty string it not
This also adds a # if there was none before
"""
# Hi, if you see this and know a better way and perferms the same as
# `color in PANGO_COLOR_NAMES or re.match("#[0-9a-fA-F]{3,12}$", color)`
if color in PANGO_COLOR_NAMES:
return color
if re.match("#[0-9a-fA-F]{3,12}$", color):
return color
if re.match("[0-9a-fA-F]{3,12}$", color):
# prepend # if it was not given because you cannot use # in rofi args
return f"#{color}"
return ""
def parse_string(renamestring, i3):
rename_elements = renamestring.split(":")
number = None
color = None
name_elements = []
for i in rename_elements:
# pick first element that looks like a color
if not color:
if pango_color := pango_color_string(i):
color = pango_color
continue
# pick first element that looks like a number
if not number and i.isnumeric():
number = i
continue
# since i is not a number or color, it must be part of the name
name_elements.append(i)
# find current ws number if no new number wanted
if not number:
tree = i3.get_tree()
number = tree.find_focused().workspace().num
# stitch name back together
name = ":".join(name_elements)
return number, color, name
def clean_workspace_name(dirty_name: str) -> str:
"""Return string with quotes(") and backslashes escaped"""
clean_name = dirty_name.replace("\\", "\\\\").replace('"', '\\"')
return clean_name
def string_for_rename(name, number, color="", prefix="", default_color=""):
name = clean_workspace_name(name)
# use default color if no new color wanted
if not color:
color = pango_color_string(default_color)
color_start, color_end = "", ""
if color:
color_start = f"<span color='{color}'>"
color_end = "</span>"
return f"{number}:{prefix}{color_start}{name}{color_end}"
def rename_workspace(
i3, name, number, color="", prefix="", default_color="", workspace=""
):
rename_string = string_for_rename(name, number, color, prefix, default_color)
workspace_quote = ""
workspace_string = ""
if workspace:
workspace_quote = '"'
number_of_workspace = workspace[: workspace.find(":")]
name_of_workspace = workspace[workspace.find(":") + 1 :]
workspace_string = string_for_rename(name_of_workspace, number_of_workspace)
i3.command(
f"rename workspace "
f"{workspace_quote}{workspace_string}{workspace_quote} "
f'to "{rename_string}"'
)
def main(args):
if args.renamestring and args.renamestring != args.print_string:
from i3ipc import Connection
i3 = Connection()
number, color, name = parse_string(args.renamestring, i3)
workspaces = i3.get_workspaces()
# if other workspace has wanted number, give it the old number
if args.swap_workspace and int(number) in [ws.num for ws in workspaces]:
# remote workspace will get number of the current/local workspace
number_for_remote_workspace = i3.get_tree().find_focused().workspace().num
full_workspace_name = (
[ws for ws in workspaces if ws.num == int(number)][0]
).name
name_for_remote_workspace = full_workspace_name[
full_workspace_name.find(":") + 1 :
]
rename_workspace(
i3,
name_for_remote_workspace,
number_for_remote_workspace,
workspace=full_workspace_name,
)
rename_workspace(i3, name, number, color, args.prefix, args.default_color)
else:
print(args.print_string)
exit(0)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument(
"renamestring",
nargs="?",
help=(
"New name for workspace. Optionally provide color or number "
"separated by colons, e.g. red:new_name:7 (order does not matter)"
),
)
parser.add_argument(
"--print-string",
default="workspace",
help="Define what to print if not arguments are given.",
)
parser.add_argument(
"--default-color",
default="",
help="Default color for workspace name if not given in renamestring",
)
parser.add_argument(
"--prefix",
default="",
help="Prefix before workspace name (after number and colon)",
)
parser.add_argument(
"--swap-workspace",
default=False,
action="store_true",
help=(
"Swap workspace index with existing workspace if desired number "
"is already in use"
),
)
# TODO: This might be a new featue to change workspace number to the next
# unsued number instead of swapping numbers
# parser.add_argument(
# "--unique", default=False, action="store_true")
args = parser.parse_args()
main(args)