~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2006-07-05 05:32:42 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060705053242-b9bbea9d1acd32d6
Make Ellerman's cdiff colours the default

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
20
20
import os
21
21
import readline
22
22
import shlex
23
 
import stat
24
23
import string
25
24
import sys
26
25
 
27
 
from bzrlib import osutils
28
26
from bzrlib.branch import Branch
29
 
from bzrlib.config import config_dir, ensure_config_dir_exists
30
27
from bzrlib.commands import get_cmd_object, get_all_cmds, get_alias
31
28
from bzrlib.errors import BzrError
32
 
from bzrlib.workingtree import WorkingTree
33
29
 
34
30
import terminal
35
31
 
68
64
 
69
65
    def get_completions_or_raise(self):
70
66
        if self.command is None:
71
 
            if '/' in self.text:
72
 
                iter = iter_executables(self.text)
73
 
            else:
74
 
                iter = (c+" " for c in iter_command_names() if
75
 
                        c not in COMPLETION_BLACKLIST)
 
67
            iter = (c+" " for c in iter_command_names() if
 
68
                    c not in COMPLETION_BLACKLIST)
76
69
            return list(filter_completions(iter, self.text))
77
70
        if self.prev_opt is None:
78
71
            completions = self.get_option_completions()
81
74
                completions.extend(list(filter_completions(iter, self.text)))
82
75
            else:
83
76
                iter = iter_file_completions(self.text)
84
 
                completions.extend(filter_completions(iter, self.text))
85
 
            return completions
 
77
                completions.extend([f+" " for f in 
 
78
                                    filter_completions(iter, self.text)])
 
79
            return completions 
86
80
 
87
81
 
88
82
class PromptCmd(cmd.Cmd):
89
 
 
90
83
    def __init__(self):
91
84
        cmd.Cmd.__init__(self)
92
85
        self.prompt = "bzr> "
97
90
        self.set_title()
98
91
        self.set_prompt()
99
92
        self.identchars += '-'
100
 
        ensure_config_dir_exists()
101
 
        self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
 
93
        self.history_file = os.path.expanduser("~/.bazaar/shell-history")
102
94
        readline.set_completer_delims(string.whitespace)
103
95
        if os.access(self.history_file, os.R_OK) and \
104
96
            os.path.isfile(self.history_file):
126
118
    def set_prompt(self):
127
119
        if self.tree is not None:
128
120
            try:
129
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
130
 
                               self.tree.relpath('.'))
 
121
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
 
122
                               self.tree.branch.relpath('.'))
131
123
                prompt = " %s:%d/%s" % prompt_data
132
124
            except:
133
125
                prompt = ""
172
164
        alias_args = get_alias(args[0])
173
165
        if alias_args is not None:
174
166
            args[0] = alias_args.pop(0)
175
 
 
 
167
            
176
168
        commandname = args.pop(0)
177
169
        for char in ('|', '<', '>'):
178
170
            commandname = commandname.split(char)[0]
204
196
 
205
197
    def completedefault(self, text, line, begidx, endidx):
206
198
        """Perform completion for native commands.
207
 
 
 
199
        
208
200
        :param text: The text to complete
209
201
        :type text: str
210
202
        :param line: The entire line to complete
267
259
                userfile+='/'
268
260
                yield userfile
269
261
            elif not only_dirs:
270
 
                yield userfile + ' '
 
262
                yield userfile
271
263
 
272
264
 
273
265
def iter_dir_completions(arg):
289
281
                yield name
290
282
 
291
283
 
292
 
def iter_executables(path):
293
 
    dirname, partial = os.path.split(path)
294
 
    for filename in os.listdir(dirname):
295
 
        if not filename.startswith(partial):
296
 
            continue
297
 
        fullpath = os.path.join(dirname, filename)
298
 
        mode=os.lstat(fullpath)[stat.ST_MODE]
299
 
        if stat.S_ISREG(mode) and 0111 & mode:
300
 
            yield fullpath + ' '
301
 
 
302
 
 
303
284
def filter_completions(iter, arg):
304
285
    return (c for c in iter if c.startswith(arg))
305
286