~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2006-03-14 18:10:04 UTC
  • mto: This revision was merged to the branch mainline in revision 329.
  • Revision ID: abentley@panoramicfeedback.com-20060314181004-ea3edbc59ddc8ae3
Handle aliases in bzr shell

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
#    You should have received a copy of the GNU General Public License
15
15
#    along with this program; if not, write to the Free Software
16
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17
 
 
18
17
import cmd
19
 
from itertools import chain
 
18
import sys
20
19
import os
 
20
import terminal
21
21
import readline
22
 
import shlex
23
22
import string
24
 
import sys
25
 
 
 
23
from itertools import chain
 
24
from bzrlib.errors import BzrError
 
25
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
26
26
from bzrlib.branch import Branch
27
 
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
28
 
from bzrlib.errors import BzrError
29
 
 
30
 
import terminal
31
 
 
32
27
 
33
28
SHELL_BLACKLIST = set(['rm', 'ls'])
34
29
COMPLETION_BLACKLIST = set(['shell'])
35
30
 
36
 
 
37
31
class BlackListedCommand(BzrError):
38
32
    def __init__(self, command):
39
33
        BzrError.__init__(self, "The command %s is blacklisted for shell use" %
40
34
                          command)
41
35
 
42
 
 
43
36
class CompletionContext(object):
44
37
    def __init__(self, text, command=None, prev_opt=None, arg_pos=None):
45
38
        self.text = text
74
67
                completions.extend(list(filter_completions(iter, self.text)))
75
68
            else:
76
69
                iter = iter_file_completions(self.text)
77
 
                completions.extend(filter_completions(iter, self.text))
 
70
                completions.extend([f+" " for f in 
 
71
                                    filter_completions(iter, self.text)])
78
72
            return completions 
79
73
 
80
74
 
159
153
        self.default("help "+line)
160
154
 
161
155
    def default(self, line):
162
 
        args = shlex.split(line)
 
156
        args = line.split()
163
157
        alias_args = get_alias(args[0])
164
158
        if alias_args is not None:
165
159
            args[0] = alias_args.pop(0)
210
204
            cmd = None
211
205
        return CompletionContext(text, command=cmd).get_completions()
212
206
 
213
 
 
214
207
def run_shell():
215
208
    try:
216
209
        prompt = PromptCmd()
221
214
    except StopIteration:
222
215
        pass
223
216
 
224
 
 
225
217
def iter_opt_completions(command_obj):
226
218
    for option_name, option in command_obj.options().items():
227
219
        yield "--" + option_name
229
221
        if short_name:
230
222
            yield "-" + short_name
231
223
 
232
 
 
233
224
def iter_file_completions(arg, only_dirs = False):
234
225
    """Generate an iterator that iterates through filename completions.
235
226
 
258
249
                userfile+='/'
259
250
                yield userfile
260
251
            elif not only_dirs:
261
 
                yield userfile + ' '
 
252
                yield userfile
262
253
 
263
254
 
264
255
def iter_dir_completions(arg):
269
260
    """
270
261
    return iter_file_completions(arg, True)
271
262
 
272
 
 
273
263
def iter_command_names(hidden=False):
274
264
    for real_cmd_name, cmd_class in get_all_cmds():
275
265
        if not hidden and cmd_class.hidden:
279
269
            if name == real_cmd_name or not real_cmd_name.startswith(name):
280
270
                yield name
281
271
 
282
 
 
283
272
def filter_completions(iter, arg):
284
273
    return (c for c in iter if c.startswith(arg))
285
274
 
286
 
 
287
275
def iter_munged_completions(iter, arg, text):
288
276
    for completion in iter:
289
277
        completion = str(completion)
290
278
        if completion.startswith(arg):
291
279
            yield completion[len(arg)-len(text):]+" "
292
280
 
293
 
 
294
281
def too_complicated(line):
295
 
    for char in '|<>*?':
 
282
    for char in '|<>"\"*?':
296
283
        if char in line:
297
284
            return True
298
285
    return False