~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2006-12-12 16:50:31 UTC
  • Revision ID: abentley@panoramicfeedback.com-20061212165031-51w8gjy1eps1vnw0
update NEWS

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
# Copyright (C) 2004, 2005 Aaron Bentley
2
 
# <aaron@aaronbentley.com>
 
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
24
24
import string
25
25
import sys
26
26
 
27
 
from bzrlib import osutils
28
27
from bzrlib.branch import Branch
29
 
from bzrlib.config import config_dir, ensure_config_dir_exists
30
 
from bzrlib.commands import get_cmd_object, all_command_names, get_alias
 
28
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
31
29
from bzrlib.errors import BzrError
32
30
from bzrlib.workingtree import WorkingTree
33
31
 
82
80
            else:
83
81
                iter = iter_file_completions(self.text)
84
82
                completions.extend(filter_completions(iter, self.text))
85
 
            return completions
 
83
            return completions 
86
84
 
87
85
 
88
86
class PromptCmd(cmd.Cmd):
89
 
 
90
87
    def __init__(self):
91
88
        cmd.Cmd.__init__(self)
92
89
        self.prompt = "bzr> "
97
94
        self.set_title()
98
95
        self.set_prompt()
99
96
        self.identchars += '-'
100
 
        ensure_config_dir_exists()
101
 
        self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
102
 
        whitespace = ''.join(c for c in string.whitespace if c < chr(127))
103
 
        readline.set_completer_delims(whitespace)
 
97
        self.history_file = os.path.expanduser("~/.bazaar/shell-history")
 
98
        readline.set_completer_delims(string.whitespace)
104
99
        if os.access(self.history_file, os.R_OK) and \
105
100
            os.path.isfile(self.history_file):
106
101
            readline.read_history_file(self.history_file)
127
122
    def set_prompt(self):
128
123
        if self.tree is not None:
129
124
            try:
130
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
 
125
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
131
126
                               self.tree.relpath('.'))
132
127
                prompt = " %s:%d/%s" % prompt_data
133
128
            except:
169
164
        self.default("help "+line)
170
165
 
171
166
    def default(self, line):
172
 
        try:
173
 
            args = shlex.split(line)
174
 
        except ValueError, e:
175
 
            print 'Parse error:', e
176
 
            return
177
 
 
 
167
        args = shlex.split(line)
178
168
        alias_args = get_alias(args[0])
179
169
        if alias_args is not None:
180
170
            args[0] = alias_args.pop(0)
181
 
 
 
171
            
182
172
        commandname = args.pop(0)
183
173
        for char in ('|', '<', '>'):
184
174
            commandname = commandname.split(char)[0]
192
182
            return os.system(line)
193
183
 
194
184
        try:
195
 
            is_qbzr = cmd_obj.__module__.startswith('bzrlib.plugins.qbzr.')
196
 
            if too_complicated(line) or is_qbzr:
 
185
            if too_complicated(line):
197
186
                return os.system("bzr "+line)
198
187
            else:
199
188
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
211
200
 
212
201
    def completedefault(self, text, line, begidx, endidx):
213
202
        """Perform completion for native commands.
214
 
 
 
203
        
215
204
        :param text: The text to complete
216
205
        :type text: str
217
206
        :param line: The entire line to complete
227
216
        return CompletionContext(text, command=cmd).get_completions()
228
217
 
229
218
 
230
 
def run_shell(directory=None):
 
219
def run_shell():
231
220
    try:
232
 
        if not directory is None:
233
 
            os.chdir(directory)
234
221
        prompt = PromptCmd()
235
 
        while True:
236
 
            try:
237
 
                try:
238
 
                    prompt.cmdloop()
239
 
                except KeyboardInterrupt:
240
 
                    print
241
 
            finally:
242
 
                prompt.write_history()
 
222
        try:
 
223
            prompt.cmdloop()
 
224
        finally:
 
225
            prompt.write_history()
243
226
    except StopIteration:
244
227
        pass
245
228
 
293
276
 
294
277
 
295
278
def iter_command_names(hidden=False):
296
 
    for real_cmd_name in all_command_names():
297
 
        cmd_obj = get_cmd_object(real_cmd_name)
298
 
        if not hidden and cmd_obj.hidden:
 
279
    for real_cmd_name, cmd_class in get_all_cmds():
 
280
        if not hidden and cmd_class.hidden:
299
281
            continue
300
 
        for name in [real_cmd_name] + cmd_obj.aliases:
 
282
        for name in [real_cmd_name] + cmd_class.aliases:
301
283
            # Don't complete on aliases that are prefixes of the canonical name
302
284
            if name == real_cmd_name or not real_cmd_name.startswith(name):
303
285
                yield name