~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Aaron Bentley
  • Date: 2008-05-12 03:22:55 UTC
  • mfrom: (0.9.28 heads)
  • Revision ID: aaron@aaronbentley.com-20080512032255-gevqj6eos15kob0j
Merge in heads command

Show diffs side-by-side

added added

removed removed

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