Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unbuffered_input context manager to Terminal #44

Closed
wants to merge 2 commits into from

Conversation

ch3pjw
Copy link

@ch3pjw ch3pjw commented Oct 22, 2013

I wanted to write a terminal app that didn't do local echoing of input and didn't wait until return was pressed before providing the input to stdin, so I added this context manager to my Terminal class. Hope it's of use!

@jquast
Copy link
Collaborator

jquast commented Oct 22, 2013

:-)

Maybe you'd like https://gist.github.com/jquast/7018509 for the time being -- working towards getting it merged, one building block at a time.

@ch3pjw
Copy link
Author

ch3pjw commented Oct 24, 2013

Gosh, that looks a whole bunch more comprehensive than mine! I never really know which bits of this terminal handling stuff will work between different terminals, yet alone across platform :-s

I can't see if it handles modifier keys yes? There's only one mention of ctrl... I fiddled a while back with making something that could handle combinations of keys like ctrl+pgup :-)

def escape_modifier(modifer_flags):
    shift = 'shift ' * (modifer_flags & 1)
    meta =  'meta ' * ((modifer_flags & 2) >> 1)
    ctrl = 'ctrl ' * ((modifer_flags & 4) >> 2)
    return ''.join([shift, meta, ctrl])


escape_sequence_to_name = {
    '[A': 'up',
    '[B': 'down',
    '[C': 'left',
    '[D': 'right',
    '[E': '5',
    '[F': 'end',
    '[G': '5',
    '[H': 'home',

    '[1~': 'home',
    '[2~': 'insert',
    '[3~': 'delete',
    '[4~': 'end',
    '[5~': 'page up',
    '[6~': 'page down',
    '[7~': 'home',
    '[8~': 'end',

    '[11~': 'f1', '[[A': 'f1',
    '[12~': 'f2', '[[B': 'f2',
    '[13~': 'f3', '[[C': 'f3',
    '[14~': 'f4', '[[D': 'f4',
    '[15~': 'f5', '[[E': 'f5',

    '[Z': 'shift tab'}


def _create_modified_movement_keys():
    modified_keys = {}
    for letter, key_name in zip(
            'ABCDEFGH',
             ('up', 'down', 'right', 'left', '5', 'end', '5', 'home')):
        for modifer_flags in range(1, 9):
            for prefix in ('[', '[1;'):
                sequence = prefix + str(modifer_flags) + letter
                modified_keys[sequence] = escape_modifier(modifer_flags) + key_name
    return modified_keys

escape_sequence_to_name.update(_create_modified_movement_keys())

@jquast
Copy link
Collaborator

jquast commented Oct 24, 2013

This version I wrote does support some of them, as it does a sort of "reverse lookup" -- if curses knows to expect it, it works. (KEY_STAB for Shift+Tab, for instance). See when this demo first begins: http://asciinema.org/a/2859 all capability codes are printed on the screen.

There are raw capability codes in my fork like you've just presented. They're definitely not universal. I'm not sure if I'll leave it in, it was used as a "fall through" -- some terminal emulators lie, sending vt220 codes even though they reported 'xterm', etc.

@jquast
Copy link
Collaborator

jquast commented Apr 14, 2015

Your request for a unbuffered_input context manager is being prepared for merge in the blessed-integration branch. It is called keystroke_input: https://github.com/erikrose/blessings/blob/blessed-integration/blessings/terminal.py#L592-630

As far as ctrl and meta keys, these are not yet supported. Created issue #98 for tracking if you'd like to continue the discussion there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants