VirtualBox

Ignore:
Timestamp:
May 17, 2010 12:39:18 PM (15 years ago)
Author:
vboxsync
Message:

python shell: smarter matcher

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxShell/vboxshell.py

    r29537 r29541  
    161161
    162162if g_hasreadline:
     163  import string
    163164  class CompleterNG(rlcompleter.Completer):
    164165    def __init__(self, dic, ctx):
     
    171172        http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496812
    172173        """
    173         if text == "":
     174        if False and text == "":
    174175            return ['\t',None][state]
    175176        else:
    176177            return rlcompleter.Completer.complete(self,text,state)
     178
     179    def canBeCommand(self, phrase, word):
     180        spaceIdx = phrase.find(" ")
     181        begIdx = readline.get_begidx()
     182        firstWord = (spaceIdx == -1 or begIdx < spaceIdx)
     183        if firstWord:
     184            return True
     185        if phrase.startswith('help'):
     186            return True
     187        return False
     188
     189    def canBeMachine(self,phrase,word):
     190        return not self.canBeCommand(phrase, word)
    177191
    178192    def global_matches(self, text):
     
    185199        matches = []
    186200        n = len(text)
    187 
    188         for list in [ self.namespace ]:
    189             for word in list:
    190                 if word[:n] == text:
    191                     matches.append(word)
    192 
     201        phrase = readline.get_line_buffer()
     202
     203        if self.canBeCommand(phrase,text):
     204            for list in [ self.namespace ]:
     205                for word in list:
     206                    if word[:n] == text:
     207                        matches.append(word)
    193208        try:
    194             for m in getMachines(self.ctx, False, True):
    195                 # although it has autoconversion, we need to cast
    196                 # explicitly for subscripts to work
    197                 word = re.sub("(?<!\\\\) ", "\\ ", str(m.name))
    198                 if word[:n] == text:
    199                     matches.append(word)
    200                 word = str(m.id)
    201                 if word[0] == '{':
    202                     word = word[1:-1]
    203                 if word[:n] == text:
    204                     matches.append(word)
     209            if self.canBeMachine(phrase,text):
     210                for m in getMachines(self.ctx, False, True):
     211                    # although it has autoconversion, we need to cast
     212                    # explicitly for subscripts to work
     213                    word = re.sub("(?<!\\\\) ", "\\ ", str(m.name))
     214                    if word[:n] == text:
     215                        matches.append(word)
     216                    word = str(m.id)
     217                    if word[0] == '{':
     218                        word = word[1:-1]
     219                    if word[:n] == text:
     220                        matches.append(word)
    205221        except Exception,e:
    206222            printErr(e)
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette