-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaliases.py
242 lines (198 loc) · 6.62 KB
/
aliases.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
import curses
from dataclasses import dataclass
import sys
from plugin import Plugin
from utils import input_text
dataclass_params = {}
if int(sys.version.split(" ")[0].split(".")[1]) >= 10: # If Python version >= 3.10
dataclass_params["slots"] = True
@dataclass(**dataclass_params)
class Alias:
source: str
destination: str
def alias_to_list(alias: Alias) -> list[str]:
return [alias.source, alias.destination]
def list_to_alias(lst: list[str]) -> Alias:
return Alias(lst[0], lst[1])
class AliasesPlugin(Plugin):
"""
Adds a new config option for the user to rebind their aliases.
"""
__singleton = None
def __new__(cls, *args, **kwargs):
"""
Creates a singleton of the class.
"""
if cls.__singleton is None:
cls.__singleton = super().__new__(cls)
return cls.__singleton
def __init__(self, app):
super().__init__(app)
self.translations = {
"en": {
"option_name": "Add command aliases",
"new": "New",
"modify_aliases": "Modify the aliases",
"add_new": "Add new alias",
"remove_alias": "Remove alias",
"done": "Done",
"errors": {
"cannot_add": "Cannot add this alias.",
"already_exists": "{} command already exists, cannot replace with {}",
"cannot_set": "{} cannot be set as {} does not exist"
}
},
"fr": {
"option_name": "Ajouter un alias de commande",
"new": "Nouveau",
"modify_aliases": "Modifier les alias",
"add_new": "Ajouter un nouvel alias",
"remove_alias": "Retirer un alias",
"done": "Terminé",
"errors": {
"cannot_add": "Impossible d'ajouter cet alias.",
"already_exists": "La commande {} existe déjà, impossible de la remplacer par {}",
"cannot_set": "{} ne pas pas devenir un alias, parce que {} n'existe pas"
}
}
}
self.add_option(self.translate("option_name"), lambda: self.translate("new"), self.modify_aliases)
# Contains the list of aliases
self.aliases: list[Alias] = []
def init(self):
"""
Loads the previous config.
"""
# Loads the commands added before this plugin
self.previous_commands = self.app.commands.copy()
# Loads from the config
self.load_from_config()
# Loads all the aliases
self.load_aliases()
def modify_aliases(self):
"""
Modifies the aliases of the commands.
"""
# Counts which option is selected
current_index = 0
col_index = 0
while True:
# Displays the label of the aliases
msg_str = f"-- {self.translate('modify_aliases')} --"
self.app.stdscr.addstr(
1,
self.app.cols // 2 - len(msg_str) // 2,
msg_str,
curses.A_REVERSE
)
# Which key was last pressed
key = ""
while key not in ('\n', '\t', "PADENTER"):
# Displays each of the aliases already created
for i, alias in enumerate(self.aliases):
self.app.stdscr.addstr(
i + 3, 10, alias.source,
curses.A_REVERSE if i == current_index and col_index == 0 else curses.A_NORMAL
)
self.app.stdscr.addstr(
i + 3, self.app.cols // 3, alias.destination,
curses.A_REVERSE if i == current_index and col_index == 1 else curses.A_NORMAL
)
# Displays the two other options
self.app.stdscr.addstr(
len(self.aliases) + 5, 10, self.translate("add_new"),
curses.A_REVERSE if current_index - len(self.aliases) == 0 else curses.A_NORMAL
)
self.app.stdscr.addstr(
len(self.aliases) + 6, 10, self.translate("remove_alias"),
curses.A_REVERSE if current_index - len(self.aliases) == 1 else curses.A_NORMAL
)
self.app.stdscr.addstr(
len(self.aliases) + 7, 10, self.translate("done"),
curses.A_REVERSE if current_index - len(self.aliases) == 2 else curses.A_NORMAL
)
# Gets what the user wants to do
key = self.app.stdscr.getkey()
# Gets which key is used
if key == "KEY_UP":
current_index -= 1
elif key == "KEY_DOWN":
current_index += 1
if key == "KEY_LEFT":
col_index -= 1
elif key == "KEY_RIGHT":
col_index += 1
current_index %= len(self.aliases) + 3
col_index %= 2
if current_index - len(self.aliases) == 0: # Add one row
new_alias = Alias("", "")
new_alias.source = input_text(self.app.stdscr, 10, len(self.aliases) + 3)
new_alias.destination = input_text(self.app.stdscr, self.app.cols // 3, len(self.aliases) + 3)
if new_alias.source == "" or new_alias.destination == "":
self.app.stdscr.addstr(len(self.aliases) + 3, 10, self.translate("errors", "cannot_add"))
self.app.stdscr.getch()
else:
self.aliases.append(new_alias)
self.app.stdscr.clear()
elif current_index - len(self.aliases) == 1: # Remove one alias
self.app.stdscr.addstr(len(self.aliases) + 6, 10, " " * len(self.translate("remove_alias")))
alias_to_remove = input_text(self.app.stdscr, 10, len(self.aliases) + 6)
if alias_to_remove != "" and alias_to_remove in [alias.source for alias in self.aliases]:
# Finds the correct alias to pop
for i, alias in enumerate(self.aliases.copy()):
if alias.source == alias_to_remove:
self.aliases.pop(i)
self.app.stdscr.clear()
elif current_index - len(self.aliases) == 2: # Finish changing the aliases
self.load_aliases()
self.save_to_config()
break
else:
self.app.stdscr.addstr(
current_index + 3,
10 if col_index == 0 else self.app.cols // 3,
" " * (self.app.cols // 3 - 10)
)
new_text = input_text(
self.app.stdscr,
10 if col_index == 0 else self.app.cols // 3,
current_index + 3
)
if new_text != "":
if col_index == 0:
self.aliases[current_index].source = new_text
else:
self.aliases[current_index].destination = new_text
def save_to_config(self):
"""
Saves the current alias preset to config.
"""
self.config["aliases"] = [
alias_to_list(alias) for alias in self.aliases
]
def load_from_config(self):
"""
Saves the current alias preset to config.
"""
if "aliases" in self.config.keys():
self.aliases = [
list_to_alias(alias) for alias in self.config["aliases"]
]
else:
self.config["aliases"] = []
def load_aliases(self):
"""
Loads the aliases of each command.
"""
# Deletes all the previous aliased commands
self.app.commands = self.previous_commands.copy()
# Adds all the aliased commands
for alias in self.aliases:
if alias.source in self.app.commands:
print(self.translate("errors", "already_exists").format(repr(alias.source), repr(alias.destination)))
elif alias.destination not in self.app.commands:
print(self.translate("errors", "cannot_set").format(repr(alias.source), repr(alias.destination)))
else:
self.app.commands[alias.source] = self.app.commands[alias.destination]
def init(app):
return AliasesPlugin(app)