~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2005-11-10 15:17:49 UTC
  • mfrom: (278 bzrtools)
  • mto: (147.4.24 trunk)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: aaron.bentley@utoronto.ca-20051110151749-1c7d988327059acb
Merged the bzrtools mainline

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
from bzrlib.commands import get_cmd_object, get_all_cmds
26
26
 
27
27
SHELL_BLACKLIST = set(['rm', 'ls'])
 
28
COMPLETION_BLACKLIST = set(['shell'])
28
29
 
29
30
class BlackListedCommand(BzrError):
30
31
    def __init__(self, command):
31
32
        BzrError.__init__(self, "The command %s is blacklisted for shell use" %
32
33
                          command)
33
34
 
 
35
class CompletionContext(object):
 
36
    def __init__(self, text, command=None, prev_opt=None, arg_pos=None):
 
37
        self.text = text
 
38
        self.command = command
 
39
        self.prev_opt = prev_opt
 
40
        self.arg_pos = None
 
41
 
 
42
    def get_completions(self):
 
43
        if not command:
 
44
            iter = (c for c in iter_command_names() if
 
45
                    c not in COMPLETION_BLACKLIST)
 
46
            try:
 
47
                iter = list(filter_completions(iter, text))
 
48
            except Exception, e:
 
49
                print e, type(e)
 
50
 
 
51
 
 
52
 
34
53
class PromptCmd(cmd.Cmd):
35
54
    def __init__(self):
36
55
        cmd.Cmd.__init__(self)
142
161
 
143
162
 
144
163
    def completenames(self, text, line, begidx, endidx):
 
164
        from bzrlib.trace import mutter
145
165
        completions = []
146
 
        iter = iter_command_names()
 
166
        iter = (c for c in iter_command_names() if
 
167
                c not in COMPLETION_BLACKLIST)
147
168
        try:
148
169
            if len(line) > 0:
149
170
                arg = line.split()[-1]
150
171
            else:
151
172
                arg = ""
152
 
            iter = list(iter_munged_completions(iter, arg, text))
 
173
            iter = filter_completions(iter, arg)
153
174
        except Exception, e:
154
175
            print e, type(e)
155
176
        return list(iter)
178
199
            command_obj = None
179
200
        try:
180
201
            if command_obj is not None:
181
 
                opts = []
182
 
                for option_name, option in command_obj.options().items():
183
 
                    opts.append("--" + option_name)
184
 
                    short_name = option.short_name()
185
 
                    if short_name:
186
 
                        opts.append("-" + short_name)
187
 
                q = list(iter_munged_completions(opts, args, text))
188
 
                return list(iter_munged_completions(opts, args, text))
 
202
                opts = list(iter_opt_completions(command_obj))
 
203
                files = list(iter_file_completions(text))
 
204
                return list(filter_completions(opts+files, text))
189
205
            elif cmd == "cd":
190
206
                if len(args) > 0:
191
207
                    arg = args.split()[-1]
192
208
                else:
193
209
                    arg = ""
194
 
                iter = iter_dir_completions(arg)
195
 
                iter = iter_munged_completions(iter, arg, text)
 
210
                iter = iter_dir_completions(text)
 
211
                iter = filter_completions(iter, text)
196
212
                return list(iter)
197
213
            elif len(args)>0:
198
214
                arg = args.split()[-1]
213
229
    except StopIteration:
214
230
        pass
215
231
 
 
232
def iter_opt_completions(command_obj):
 
233
    for option_name, option in command_obj.options().items():
 
234
        yield "--" + option_name
 
235
        short_name = option.short_name()
 
236
        if short_name:
 
237
            yield "-" + short_name
 
238
 
216
239
def iter_file_completions(arg, only_dirs = False):
217
240
    """Generate an iterator that iterates through filename completions.
218
241
 
261
284
            if name == real_cmd_name or not real_cmd_name.startswith(name):
262
285
                yield name
263
286
 
 
287
def filter_completions(iter, arg):
 
288
    return (c for c in iter if c.startswith(arg))
 
289
 
264
290
def iter_munged_completions(iter, arg, text):
265
291
    for completion in iter:
266
292
        completion = str(completion)
268
294
            yield completion[len(arg)-len(text):]+" "
269
295
 
270
296
def too_complicated(line):
271
 
    for char in '|<>"\"':
 
297
    for char in '|<>"\"*?':
272
298
        if char in line:
273
299
            return True
274
300
    return False