~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2006-08-07 03:07:35 UTC
  • mto: This revision was merged to the branch mainline in revision 425.
  • Revision ID: aaron.bentley@utoronto.ca-20060807030735-0a9f8330ce1a836a
Add --no-color option to shelf

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
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
33
27
from bzrlib.branch import Branch
34
 
from bzrlib.config import config_dir, ensure_config_dir_exists
35
 
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
36
29
from bzrlib.errors import BzrError
37
30
from bzrlib.workingtree import WorkingTree
38
31
 
87
80
            else:
88
81
                iter = iter_file_completions(self.text)
89
82
                completions.extend(filter_completions(iter, self.text))
90
 
            return completions
 
83
            return completions 
91
84
 
92
85
 
93
86
class PromptCmd(cmd.Cmd):
94
 
 
95
87
    def __init__(self):
96
88
        cmd.Cmd.__init__(self)
97
89
        self.prompt = "bzr> "
102
94
        self.set_title()
103
95
        self.set_prompt()
104
96
        self.identchars += '-'
105
 
        ensure_config_dir_exists()
106
 
        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)
 
97
        self.history_file = os.path.expanduser("~/.bazaar/shell-history")
 
98
        readline.set_completer_delims(string.whitespace)
 
99
        if os.access(self.history_file, os.R_OK) and \
 
100
            os.path.isfile(self.history_file):
 
101
            readline.read_history_file(self.history_file)
113
102
        self.cwd = os.getcwd()
114
103
 
115
104
    def write_history(self):
116
 
        if _has_readline:
117
 
            readline.write_history_file(self.history_file)
 
105
        readline.write_history_file(self.history_file)
118
106
 
119
107
    def do_quit(self, args):
120
108
        self.write_history()
134
122
    def set_prompt(self):
135
123
        if self.tree is not None:
136
124
            try:
137
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
 
125
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
138
126
                               self.tree.relpath('.'))
139
127
                prompt = " %s:%d/%s" % prompt_data
140
128
            except:
176
164
        self.default("help "+line)
177
165
 
178
166
    def default(self, line):
179
 
        try:
180
 
            args = shlex.split(line)
181
 
        except ValueError, e:
182
 
            print 'Parse error:', e
183
 
            return
184
 
 
 
167
        args = shlex.split(line)
185
168
        alias_args = get_alias(args[0])
186
169
        if alias_args is not None:
187
170
            args[0] = alias_args.pop(0)
188
 
 
 
171
            
189
172
        commandname = args.pop(0)
190
173
        for char in ('|', '<', '>'):
191
174
            commandname = commandname.split(char)[0]
199
182
            return os.system(line)
200
183
 
201
184
        try:
202
 
            is_qbzr = cmd_obj.__module__.startswith('bzrlib.plugins.qbzr.')
203
 
            if too_complicated(line) or is_qbzr:
 
185
            if too_complicated(line):
204
186
                return os.system("bzr "+line)
205
187
            else:
206
188
                return (cmd_obj.run_argv_aliases(args, alias_args) or 0)
207
189
        except BzrError, e:
208
 
            trace.log_exception_quietly()
209
190
            print e
210
191
        except KeyboardInterrupt, e:
211
192
            print "Interrupted"
212
193
        except Exception, e:
213
 
            trace.log_exception_quietly()
 
194
#            print "Unhandled error:\n%s" % errors.exception_str(e)
214
195
            print "Unhandled error:\n%s" % (e)
215
196
 
216
197
 
219
200
 
220
201
    def completedefault(self, text, line, begidx, endidx):
221
202
        """Perform completion for native commands.
222
 
 
 
203
        
223
204
        :param text: The text to complete
224
205
        :type text: str
225
206
        :param line: The entire line to complete
235
216
        return CompletionContext(text, command=cmd).get_completions()
236
217
 
237
218
 
238
 
def run_shell(directory=None):
 
219
def run_shell():
239
220
    try:
240
 
        if not directory is None:
241
 
            os.chdir(directory)
242
221
        prompt = PromptCmd()
243
 
        while True:
244
 
            try:
245
 
                try:
246
 
                    prompt.cmdloop()
247
 
                except KeyboardInterrupt:
248
 
                    print
249
 
            finally:
250
 
                prompt.write_history()
 
222
        try:
 
223
            prompt.cmdloop()
 
224
        finally:
 
225
            prompt.write_history()
251
226
    except StopIteration:
252
227
        pass
253
228
 
301
276
 
302
277
 
303
278
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:
 
279
    for real_cmd_name, cmd_class in get_all_cmds():
 
280
        if not hidden and cmd_class.hidden:
307
281
            continue
308
 
        for name in [real_cmd_name] + cmd_obj.aliases:
 
282
        for name in [real_cmd_name] + cmd_class.aliases:
309
283
            # Don't complete on aliases that are prefixes of the canonical name
310
284
            if name == real_cmd_name or not real_cmd_name.startswith(name):
311
285
                yield name