Beispiel #1
0
def get_encoding_mode():
    """
    Get the mode Urwid is using when processing text strings.
    Returns 'narrow' for 8-bit encodings, 'wide' for CJK encodings
    or 'utf8' for UTF-8 encodings.
    """
    return str_util.get_byte_encoding()
Beispiel #2
0
def get_encoding_mode():
    """
    Get the mode Urwid is using when processing text strings.
    Returns 'narrow' for 8-bit encodings, 'wide' for CJK encodings
    or 'utf8' for UTF-8 encodings.
    """
    return str_util.get_byte_encoding()
Beispiel #3
0
def process_keyqueue(codes, more_available):
    """
    codes -- list of key codes
    more_available -- if True then raise MoreInputRequired when in the
        middle of a character sequence (escape/utf8/wide) and caller
        will attempt to send more key codes on the next call.

    returns (list of input, list of remaining key codes).
    """
    code = codes[0]
    if code >= 32 and code <= 126:
        key = chr(code)
        return [key], codes[1:]
    if code in _keyconv:
        return [_keyconv[code]], codes[1:]
    if code >0 and code <27:
        return ["ctrl %s" % chr(ord('a')+code-1)], codes[1:]
    if code >27 and code <32:
        return ["ctrl %s" % chr(ord('A')+code-1)], codes[1:]

    em = str_util.get_byte_encoding()

    if (em == 'wide' and code < 256 and
        within_double_byte(chr(code),0,0)):
        if not codes[1:]:
            if more_available:
                raise MoreInputRequired()
        if codes[1:] and codes[1] < 256:
            db = chr(code)+chr(codes[1])
            if within_double_byte(db, 0, 1):
                return [db], codes[2:]
    if em == 'utf8' and code>127 and code<256:
        if code & 0xe0 == 0xc0: # 2-byte form
            need_more = 1
        elif code & 0xf0 == 0xe0: # 3-byte form
            need_more = 2
        elif code & 0xf8 == 0xf0: # 4-byte form
            need_more = 3
        else:
            return ["<%d>"%code], codes[1:]

        for i in range(need_more):
            if len(codes)-1 <= i:
                if more_available:
                    raise MoreInputRequired()
                else:
                    return ["<%d>"%code], codes[1:]
            k = codes[i+1]
            if k>256 or k&0xc0 != 0x80:
                return ["<%d>"%code], codes[1:]

        s = bytes3(codes[:need_more+1])

        assert isinstance(s, bytes)
        try:
            return [s.decode("utf-8")], codes[need_more+1:]
        except UnicodeDecodeError:
            return ["<%d>"%code], codes[1:]

    if code >127 and code <256:
        key = chr(code)
        return [key], codes[1:]
    if code != 27:
        return ["<%d>"%code], codes[1:]

    result = input_trie.get(codes[1:], more_available)

    if result is not None:
        result, remaining_codes = result
        return [result], remaining_codes

    if codes[1:]:
        # Meta keys -- ESC+Key form
        run, remaining_codes = process_keyqueue(codes[1:],
            more_available)
        if run[0] == "esc" or run[0].find("meta ") >= 0:
            return ['esc']+run, remaining_codes
        return ['meta '+run[0]]+run[1:], remaining_codes

    return ['esc'], codes[1:]
Beispiel #4
0
def process_keyqueue(codes, more_available):
    """
    codes -- list of key codes
    more_available -- if True then raise MoreInputRequired when in the 
        middle of a character sequence (escape/utf8/wide) and caller 
        will attempt to send more key codes on the next call.
    
    returns (list of input, list of remaining key codes).
    """
    code = codes[0]
    if code >= 32 and code <= 126:
        key = chr(code)
        return [key], codes[1:]
    if _keyconv.has_key(code):
        return [_keyconv[code]], codes[1:]
    if code > 0 and code < 27:
        return ["ctrl %s" % chr(ord('a') + code - 1)], codes[1:]
    if code > 27 and code < 32:
        return ["ctrl %s" % chr(ord('A') + code - 1)], codes[1:]

    em = str_util.get_byte_encoding()

    if (em == 'wide' and code < 256 and within_double_byte(chr(code), 0, 0)):
        if not codes[1:]:
            if more_available:
                raise MoreInputRequired()
        if codes[1:] and codes[1] < 256:
            db = chr(code) + chr(codes[1])
            if within_double_byte(db, 0, 1):
                return [db], codes[2:]
    if em == 'utf8' and code > 127 and code < 256:
        if code & 0xe0 == 0xc0:  # 2-byte form
            need_more = 1
        elif code & 0xf0 == 0xe0:  # 3-byte form
            need_more = 2
        elif code & 0xf8 == 0xf0:  # 4-byte form
            need_more = 3
        else:
            return ["<%d>" % code], codes[1:]

        for i in range(need_more):
            if len(codes) - 1 <= i:
                if more_available:
                    raise MoreInputRequired()
                else:
                    return ["<%d>" % code], codes[1:]
            k = codes[i + 1]
            if k > 256 or k & 0xc0 != 0x80:
                return ["<%d>" % code], codes[1:]

        s = bytes3(codes[:need_more + 1])

        assert isinstance(s, bytes)
        try:
            return [s.decode("utf-8")], codes[need_more + 1:]
        except UnicodeDecodeError:
            return ["<%d>" % code], codes[1:]

    if code > 127 and code < 256:
        key = chr(code)
        return [key], codes[1:]
    if code != 27:
        return ["<%d>" % code], codes[1:]

    result = input_trie.get(codes[1:], more_available)

    if result is not None:
        result, remaining_codes = result
        return [result], remaining_codes

    if codes[1:]:
        # Meta keys -- ESC+Key form
        run, remaining_codes = process_keyqueue(codes[1:], more_available)
        if run[0] == "esc" or run[0].find("meta ") >= 0:
            return ['esc'] + run, remaining_codes
        return ['meta ' + run[0]] + run[1:], remaining_codes

    return ['esc'], codes[1:]