~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2008-06-03 14:46:55 UTC
  • Revision ID: aaron@aaronbentley.com-20080603144655-8v3zq81xmlwefg2d
Fix behavior when TERM is set to nothing

Show diffs side-by-side

added added

removed removed

Lines of Context:
18
18
import cmd
19
19
from itertools import chain
20
20
import os
21
 
try:
22
 
    import readline
23
 
except ImportError:
24
 
    _has_readline = False
25
 
else:
26
 
    _has_readline = True
 
21
import readline
27
22
import shlex
28
23
import stat
29
24
import string
30
25
import sys
31
26
 
32
 
from bzrlib import osutils, trace
 
27
from bzrlib import osutils
33
28
from bzrlib.branch import Branch
34
29
from bzrlib.config import config_dir, ensure_config_dir_exists
35
 
from bzrlib.commands import get_cmd_object, all_command_names, get_alias
 
30
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
36
31
from bzrlib.errors import BzrError
37
32
from bzrlib.workingtree import WorkingTree
38
33
 
104
99
        self.identchars += '-'
105
100
        ensure_config_dir_exists()
106
101
        self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
107
 
        whitespace = ''.join(c for c in string.whitespace if c < chr(127))
108
 
        if _has_readline:
109
 
            readline.set_completer_delims(whitespace)
110
 
            if os.access(self.history_file, os.R_OK) and \
111
 
                os.path.isfile(self.history_file):
112
 
                readline.read_history_file(self.history_file)
 
102
        readline.set_completer_delims(string.whitespace)
 
103
        if os.access(self.history_file, os.R_OK) and \
 
104
            os.path.isfile(self.history_file):
 
105
            readline.read_history_file(self.history_file)
113
106
        self.cwd = os.getcwd()
114
107
 
115
108
    def write_history(self):
116
 
        if _has_readline:
117
 
            readline.write_history_file(self.history_file)
 
109
        readline.write_history_file(self.history_file)
118
110
 
119
111
    def do_quit(self, args):
120
112
        self.write_history()
176
168
        self.default("help "+line)
177
169
 
178
170
    def default(self, line):
179
 
        try:
180
 
            args = shlex.split(line)
181
 
        except ValueError, e:
182
 
            print 'Parse error:', e
183
 
            return
184
 
 
 
171
        args = shlex.split(line)
185
172
        alias_args = get_alias(args[0])
186
173
        if alias_args is not None:
187
174
            args[0] = alias_args.pop(0)
199
186
            return os.system(line)
200
187
 
201
188
        try:
202
 
            is_qbzr = cmd_obj.__module__.startswith('bzrlib.plugins.qbzr.')
203
 
            if too_complicated(line) or is_qbzr:
 
189
            if too_complicated(line):
204
190
                return os.system("bzr "+line)
205
191
            else:
206
192
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
207
193
        except BzrError, e:
208
 
            trace.log_exception_quietly()
209
194
            print e
210
195
        except KeyboardInterrupt, e:
211
196
            print "Interrupted"
212
197
        except Exception, e:
213
 
            trace.log_exception_quietly()
 
198
#            print "Unhandled error:\n%s" % errors.exception_str(e)
214
199
            print "Unhandled error:\n%s" % (e)
215
200
 
216
201
 
235
220
        return CompletionContext(text, command=cmd).get_completions()
236
221
 
237
222
 
238
 
def run_shell(directory=None):
 
223
def run_shell():
239
224
    try:
240
 
        if not directory is None:
241
 
            os.chdir(directory)
242
225
        prompt = PromptCmd()
243
 
        while True:
244
 
            try:
245
 
                try:
246
 
                    prompt.cmdloop()
247
 
                except KeyboardInterrupt:
248
 
                    print
249
 
            finally:
250
 
                prompt.write_history()
 
226
        try:
 
227
            prompt.cmdloop()
 
228
        finally:
 
229
            prompt.write_history()
251
230
    except StopIteration:
252
231
        pass
253
232
 
301
280
 
302
281
 
303
282
def iter_command_names(hidden=False):
304
 
    for real_cmd_name in all_command_names():
305
 
        cmd_obj = get_cmd_object(real_cmd_name)
306
 
        if not hidden and cmd_obj.hidden:
 
283
    for real_cmd_name, cmd_class in get_all_cmds():
 
284
        if not hidden and cmd_class.hidden:
307
285
            continue
308
 
        for name in [real_cmd_name] + cmd_obj.aliases:
 
286
        for name in [real_cmd_name] + cmd_class.aliases:
309
287
            # Don't complete on aliases that are prefixes of the canonical name
310
288
            if name == real_cmd_name or not real_cmd_name.startswith(name):
311
289
                yield name