示例#1
0
 def __init__(self, reader):
     """
     :type reader: pyjline.utils.NonBlockingReader
     """
     self.reader = reader
     self.opBuffer = StringBuilder()
     self.pushBackChar = deque()
     self.lastBinding = u""
示例#2
0
class BindingReader(object):

    def __init__(self, reader):
        """
        :type reader: pyjline.utils.NonBlockingReader
        """
        self.reader = reader
        self.opBuffer = StringBuilder()
        self.pushBackChar = deque()
        self.lastBinding = u""

    def readBinding(self, keys, local=None, block=True):
        """
        :type keys: pyjline.keymap.KeyMap
        :type local: pyjline.keymap.KeyMap
        :type block: bool
        """
        self.lastBinding = None
        
        o = None
        remaining = [0]

        while True:

            c  = self.readCharacter()
            if c == -1:
                return None
            self.opBuffer.appendCodePoint(c)

            if (not (local is None)):
                o = local.getBound(self.opBuffer, remaining)
            if ((o is None) and ((local is None) or (remaining[0] >= 0))):
                o = keys.getBound(self.opBuffer, remaining)

            if (not (o is None)):
                if (remaining[0] >= 0):
                    opblen = self.opBuffer.length() - remaining[0]
                    self.runMacro(self.opBuffer.substring(opblen))
                    self.opBuffer.setLength(opblen)
                else:
                    ambiguousTimeout = keys.getAmbigousTimeout()
                    if (ambiguousTimeout > 0 and self.peekCharacter(ambiguousTimeout) != NonBlockingReader.READ_EXPIRED):
                        o = None
                if (not (o is None)):
                    self.lastBinding = self.opBuffer.toString()
                    self.opBuffer.setLength(0)
                    return o
            elif (remaining[0] > 0):
                cp = self.opBuffer.codePointAt(0)
                rem = self.opBuffer.substring(Character.charCount(cp))
                self.lastBinding = self.opBuffer.substring(0, Character.charCount(cp))
                o = keys.getUnicode() if (cp >= KeyMap.KEYMAP_LENGTH) else keys.getNomatch()
                self.opBuffer.setLength(0)
                self.opBuffer.append(rem)
                if (not (o is None)):
                    return o

            if block:
                break

    def readCharacter(self):
        """
        :rtype int
        """
        if (not self.pushBackChar.is_empty()):
            return self.pushBackChar.pop()
        
        try:
            c = NonBlockingReader.READ_EXPIRED;
            s = 0;
            while (c == NonBlockingReader.READ_EXPIRED):
                c = self.reader.read()
                if (c >= 0 and Character.isHighSurrogate(c)):
                    s = c
                    c = NonBlockingReader.READ_EXPIRED
            return Character.toCodePoint(s, c) if s != 0 else c
        except:
            # TODO: Exception
            pass

    def peekCharacter(self, timeout):
        """
        :type timeout: int
        :rtype int
        """
        if (not self.pushBackChar.is_empty()):
            return self.pushBackChar.peek()
        
        try:
            return self.reader.peek(timeout)
        except:
            # TODO: Exception
            pass

    def runMacro(self, macro):
        """
        :type timeout: str
        """
        #TODO: toCodePoints
        for c in macro:
            self.pushBackChar.append(ord(c))

    def getCurrentBuffer(self):
        """
        :type timeout: int
        :rtype unicode
        """
        return self.opBuffer.toString()

    def getLastBinding(self):
        """
        :rtype: unicode
        """
        return self.lastBinding