summary refs log tree commit diff stats
diff options
context:
space:
mode:
authornfnty <git@nfnty.se>2017-02-03 00:05:49 +0100
committernfnty <git@nfnty.se>2017-02-03 00:09:55 +0100
commitbf080d7a110eb9741a143bc8529a55d91862ec0d (patch)
treec6e0e6a7c17f411b8b88ed7705ca88ad42005cc3
parent043b7635ed9ef60e6e08453d692685e943fb2a78 (diff)
downloadranger-bf080d7a110eb9741a143bc8529a55d91862ec0d.tar.gz
api.commands: Refactor aliasing
Fixes #537
-rw-r--r--ranger/api/commands.py65
1 files changed, 23 insertions, 42 deletions
diff --git a/ranger/api/commands.py b/ranger/api/commands.py
index d6c02aaa..b7497b61 100644
--- a/ranger/api/commands.py
+++ b/ranger/api/commands.py
@@ -7,7 +7,6 @@ from __future__ import (absolute_import, division, print_function)
 
 import os
 import re
-import inspect
 # COMPAT pylint: disable=unused-import
 from collections import deque  # NOQA
 from ranger.api import LinemodeBase, hook_init, hook_ready, register_linemode  # NOQA
@@ -20,7 +19,11 @@ from ranger.ext.lazy_property import lazy_property
 _SETTINGS_RE = re.compile(r'^\s*([^\s]+?)=(.*)$')
 
 
-class CommandContainer(object):
+def _alias_line(full_command, line):
+    return full_command + ''.join(re.split(r'(\s+)', line)[1:])
+
+
+class CommandContainer(FileManagerAware):
 
     def __init__(self):
         self.commands = {}
@@ -29,12 +32,24 @@ class CommandContainer(object):
         return self.commands[key]
 
     def alias(self, name, full_command):
-        cmd = type(name, (AliasCommand, ), dict())
-        cmd.based_function = name
-        cmd.function_name = name
-        cmd.object_name = name
-        cmd.full_command = full_command
-        self.commands[name] = cmd
+        cmd_name = full_command.split()[0]
+        try:
+            cmd = self.get_command(cmd_name)
+        except KeyError:
+            self.fm.notify('alias failed: No such command: {0}'.format(cmd_name), bad=True)
+            return None
+
+        class CommandAlias(cmd):   # pylint: disable=too-few-public-methods
+            def __init__(self, line, *args, **kwargs):
+                super(CommandAlias, self).__init__(
+                    _alias_line(self.full_command, line), *args, **kwargs)
+
+        cmd_alias = type(name, (CommandAlias, ), dict(full_command=full_command))
+        if issubclass(cmd_alias, FunctionCommand):
+            cmd_alias.based_function = name
+            cmd_alias.object_name = name
+            cmd_alias.function_name = name
+        self.commands[name] = cmd_alias
 
     def load_commands_from_module(self, module):
         for var in vars(module).values():
@@ -420,40 +435,6 @@ class FunctionCommand(Command):
                 )
 
 
-class AliasCommand(Command):
-    based_function = None
-    object_name = ""
-    function_name = "unknown"
-    full_command = ""
-
-    def execute(self):
-        return self._make_cmd().execute()
-
-    def quick(self):
-        return self._make_cmd().quick()
-
-    def tab(self, tabnum):
-        cmd = self._make_cmd()
-        if self.fm.py3:
-            args = inspect.signature(cmd.tab).parameters  # pylint: disable=no-member
-        else:
-            args = inspect.getargspec(cmd.tab).args  # pylint: disable=deprecated-method
-        return cmd.tab(tabnum) if 'tabnum' in args else cmd.tab()
-
-    def cancel(self):
-        return self._make_cmd().cancel()
-
-    def _make_cmd(self):
-        cmd_class = self.fm.commands.get_command(self.full_command.split()[0])
-        cmd = cmd_class(self.full_command + ' ' + self.rest(1))
-        cmd.quickly_executed = self.quickly_executed
-        cmd.quantifier = self.quantifier
-        cmd.escape_macros_for_shell = self.escape_macros_for_shell
-        cmd.resolve_macros = self.resolve_macros
-        cmd.allow_abbrev = self.allow_abbrev
-        return cmd
-
-
 if __name__ == '__main__':
     import doctest
     doctest.testmod()
a75b ^
77cdc6d0 ^
e00d4854 ^
09454849 ^
77cdc6d0 ^
51530916 ^

41f5188d ^
e00d4854 ^
7feea75b ^

41f5188d ^
e00d4854 ^
51530916 ^
41f5188d ^
7feea75b ^
51530916 ^


41f5188d ^
51530916 ^

d03028a3 ^
35064671 ^
41f5188d ^
5f98a10c ^


41f5188d ^
51530916 ^

41f5188d ^
e00d4854 ^
51530916 ^


41f5188d ^
51530916 ^
41f5188d ^
5feb36ff ^
41f5188d ^
77cdc6d0 ^
5feb36ff ^
51530916 ^

51530916 ^






7feea75b ^
5f98a10c ^
51530916 ^
e00d4854 ^

51530916 ^
51530916 ^
35064671 ^
51530916 ^
35064671 ^
51530916 ^
35064671 ^
51530916 ^



41f5188d ^
51530916 ^
029c04e2 ^

f4b6b2c2 ^
029c04e2 ^


51530916 ^

f4b6b2c2 ^
23eed027 ^
f4b6b2c2 ^
f4b6b2c2 ^





51530916 ^



6fa778b3 ^
51530916 ^
baaf6d65 ^
5af83346 ^

51530916 ^
2e72dc12 ^
06584c52 ^
51530916 ^
ac0e9db5 ^

51530916 ^
ac0e9db5 ^
41f5188d ^

5feb36ff ^
41f5188d ^
51530916 ^

5feb36ff ^
51530916 ^


ac0e9db5 ^
51530916 ^
ac0e9db5 ^
41f5188d ^
51530916 ^


54e4548d ^
41f5188d ^
51530916 ^



41f5188d ^
d1bd0439 ^
ac0e9db5 ^

41f5188d ^
d7494165 ^
51530916 ^

41f5188d ^
d7494165 ^
51530916 ^


51530916 ^

41f5188d ^

51530916 ^

41f5188d ^
51530916 ^
5feb36ff ^
41f5188d ^
eba30983 ^
51530916 ^
eba30983 ^
51530916 ^



5f98a10c ^


51530916 ^
5f98a10c ^

51530916 ^



41f5188d ^

51530916 ^


7be15b8b ^
05d17773 ^
51530916 ^

51530916 ^



51530916 ^

ac0e9db5 ^
51530916 ^
ac0e9db5 ^

51530916 ^


7be15b8b ^
51530916 ^




7be15b8b ^








d7494165 ^












51530916 ^
51530916 ^



2fb94e3c ^



51530916 ^




51530916 ^


5feb36ff ^
51530916 ^

51530916 ^






467046ef ^

51530916 ^
7feea75b ^



77cdc6d0 ^


cdd6fd09 ^
7feea75b ^
cdd6fd09 ^

7feea75b ^


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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370