~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to shell.py

  • Committer: Alexander Belchenko
  • Date: 2006-07-18 20:37:53 UTC
  • mto: This revision was merged to the branch mainline in revision 421.
  • Revision ID: bialix@ukr.net-20060718203753-fa30c2f3cc59316b
don't use curses on win32

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()
82
75
            else:
83
76
                iter = iter_file_completions(self.text)
84
77
                completions.extend(filter_completions(iter, self.text))
85
 
            return completions
 
78
            return completions 
86
79
 
87
80
 
88
81
class PromptCmd(cmd.Cmd):
89
 
 
90
82
    def __init__(self):
91
83
        cmd.Cmd.__init__(self)
92
84
        self.prompt = "bzr> "
97
89
        self.set_title()
98
90
        self.set_prompt()
99
91
        self.identchars += '-'
100
 
        ensure_config_dir_exists()
101
 
        self.history_file = osutils.pathjoin(config_dir(), 'shell-history')
 
92
        self.history_file = os.path.expanduser("~/.bazaar/shell-history")
102
93
        readline.set_completer_delims(string.whitespace)
103
94
        if os.access(self.history_file, os.R_OK) and \
104
95
            os.path.isfile(self.history_file):
126
117
    def set_prompt(self):
127
118
        if self.tree is not None:
128
119
            try:
129
 
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(),
130
 
                               self.tree.relpath('.'))
 
120
                prompt_data = (self.tree.branch.nick, self.tree.branch.revno(), 
 
121
                               self.tree.branch.relpath('.'))
131
122
                prompt = " %s:%d/%s" % prompt_data
132
123
            except:
133
124
                prompt = ""
172
163
        alias_args = get_alias(args[0])
173
164
        if alias_args is not None:
174
165
            args[0] = alias_args.pop(0)
175
 
 
 
166
            
176
167
        commandname = args.pop(0)
177
168
        for char in ('|', '<', '>'):
178
169
            commandname = commandname.split(char)[0]
204
195
 
205
196
    def completedefault(self, text, line, begidx, endidx):
206
197
        """Perform completion for native commands.
207
 
 
 
198
        
208
199
        :param text: The text to complete
209
200
        :type text: str
210
201
        :param line: The entire line to complete
289
280
                yield name
290
281
 
291
282
 
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
283
def filter_completions(iter, arg):
304
284
    return (c for c in iter if c.startswith(arg))
305
285