erikrose/blessings — pull request #27 by jquast (2013-04-11 05:27:59+00:00)
Title: Implement multibyte input keysequence to keycode translation.

Hi Erik,

I've implemented 50% of the input key translation work. It is still missing the termios, select, cooked-mode character-at-a-time input stuff, but it does the heavy lifting of translating input sequences into key names. Here is an example:

>>> term = blessings.Terminal()
>>> sequence='^[[D^[[H^[[A^[[6~^[[15~xyzzyå∫'

>>> for keystroke in term._resolve_multibyte(sequence):
... print keystroke.code, keystroke.name, repr(keystroke)
...
260 KEY_LEFT
262 KEY_HOME
259 KEY_UP
338 KEY_NPAGE
269 KEY_F5
120 None u'x'
121 None u'y'
122 None u'z'
122 None u'z'
121 None u'y'
229 None u'\xe5'
8747 None u'\u222b'

(please note, press ctrl+v, then LEFT arrow key, etc. to input the sequence, it will not copy and paste! This is necessary for 'raw input' for readline-enabled python prompts. Also some alt+things,)

below is the commit message.

- add unicode-derived 'Keystroke' class, which is yielded by the
generator method _resolve_multibyte, adding additional properties,
.is_sequence (bool), .code (int), and .name (str).
- detect encoding and initialize incrementaldecoder in __init__, used
by _resolve_multibyte.
- inherit curses key capability names, values, and create multibyte
sequence pattern lookup in __init__
- add _resolve_keycode method, returns key capability name for an
integer.
- add _resolve_multibyte(text) method, given a string of multibyte
input, 'text', returns instances of Keystroke class, which may be
checked for .is_sequence, .code, and .name.