1
# Copyright (C) 2004, 2005 Aaron Bentley
2
# <aaron.bentley@utoronto.ca>
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23
from bzrlib.errors import BzrError
24
from bzrlib.commands import get_cmd_object
26
class PromptCmd(cmd.Cmd):
28
cmd.Cmd.__init__(self)
31
self.tree = arch.tree_root(".")
36
self.identchars += '-'
37
self.history_file = os.path.expanduser("~/.bazaar/shell-history")
38
readline.set_completer_delims(string.whitespace)
39
if os.access(self.history_file, os.R_OK) and \
40
os.path.isfile(self.history_file):
41
readline.read_history_file(self.history_file)
42
self.cwd = os.getcwd()
44
def write_history(self):
45
readline.write_history_file(self.history_file)
47
def do_quit(self, args):
51
def do_exit(self, args):
54
def do_EOF(self, args):
58
def postcmd(self, line, bar):
63
if self.tree is not None:
65
prompt = pylon.alias_or_version(self.tree.tree_version,
68
if prompt is not None:
69
prompt = " " + prompt +":"+ pylon.tree_cwd(self.tree)
74
self.prompt = "bzr%s> " % prompt
76
def set_title(self, command=None):
78
version = pylon.alias_or_version(self.tree.tree_version, self.tree,
81
version = "[no version]"
84
sys.stdout.write(terminal.term_title("bzr %s %s" % (command, version)))
86
def do_cd(self, line):
89
line = os.path.expanduser(line)
90
if os.path.isabs(line):
93
newcwd = self.cwd+'/'+line
94
newcwd = os.path.normpath(newcwd)
101
self.tree = arch.tree_root(".")
105
def do_help(self, line):
108
def default(self, line):
110
commandname = args.pop(0)
112
cmd_obj = get_cmd_object(commandname)
114
return os.system(line)
118
return (cmd_obj.run_argv(args) or 0)
121
except KeyboardInterrupt, e:
124
# print "Unhandled error:\n%s" % errors.exception_str(e)
125
print "Unhandled error:\n%s" % (e)
128
def completenames(self, text, line, begidx, endidx):
130
iter = iter_command_names(self.fake_aba)
133
arg = line.split()[-1]
136
iter = cmdutil.iter_munged_completions(iter, arg, text)
141
def completedefault(self, text, line, begidx, endidx):
142
"""Perform completion for native commands.
144
:param text: The text to complete
146
:param line: The entire line to complete
148
:param begidx: The start of the text in the line
150
:param endidx: The end of the text in the line
154
(cmd, args, foo) = self.parseline(line)
155
command_obj=find_command(cmd)
156
if command_obj is not None:
157
return command_obj.complete(args.split(), text)
158
elif not self.fake_aba.is_command(cmd) and \
159
cmdutil.is_tla_command(cmd):
160
iter = cmdutil.iter_supported_switches(cmd)
162
arg = args.split()[-1]
165
if arg.startswith("-"):
166
return list(cmdutil.iter_munged_completions(iter, arg,
169
return list(cmdutil.iter_munged_completions(
170
cmdutil.iter_file_completions(arg), arg, text))
175
arg = args.split()[-1]
178
iter = cmdutil.iter_dir_completions(arg)
179
iter = cmdutil.iter_munged_completions(iter, arg, text)
182
arg = args.split()[-1]
183
iter = cmdutil.iter_file_completions(arg)
184
return list(cmdutil.iter_munged_completions(iter, arg, text))
186
return self.completenames(text, line, begidx, endidx)
195
prompt.write_history()
197
def iter_file_completions(arg, only_dirs = False):
198
"""Generate an iterator that iterates through filename completions.
200
:param arg: The filename fragment to match
202
:param only_dirs: If true, match only directories
203
:type only_dirs: bool
210
(dir, file) = os.path.split(arg)
212
listingdir = os.path.expanduser(dir)
215
for file in iter_combine([os.listdir(listingdir), extras]):
217
userfile = dir+'/'+file
220
if userfile.startswith(arg):
221
if os.path.isdir(listingdir+'/'+file):
228
def iter_dir_completions(arg):
229
"""Generate an iterator that iterates through directory name completions.
231
:param arg: The directory name fragment to match
234
return iter_file_completions(arg, True)