Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
- A Key or KeyChain instance, which is simply returned.
- A string that contains spaces is split at those spaces and all
non-empty items of the resulting list are passed to KeyChain.
- A string with '+' in it is split at those '+' and the resulting list
is passed to KeyChain.
- Any other string is passed to Key.
"""
if isinstance(string, (Key, KeyChain)):
return string
elif ' ' in string:
keys = (key for key in self._KEY_SPLIT_SPACE.split(string) if key)
return KeyChain(*keys)
elif '+' in string and len(string) >= 3:
return KeyChain(*string.strip().split('+'))
else:
return Key(string)
urwid.command_map[Key('home')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('end')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-a')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('ctrl-e')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-k')] = urwid.DELETE_TO_EOL
urwid.command_map[Key('ctrl-u')] = urwid.DELETE_LINE
urwid.command_map[Key('ctrl-d')] = urwid.DELETE_CHAR_UNDER_CURSOR
urwid.command_map[Key('meta-d')] = urwid.DELETE_WORD_LEFT
urwid.command_map[Key('meta-backspace')] = urwid.DELETE_WORD_RIGHT
urwid.command_map[Key('ctrl-w')] = urwid.DELETE_WORD_RIGHT
urwid.command_map[Key('enter')] = urwid.ACTIVATE
urwid.command_map[Key('escape')] = urwid.CANCEL
urwid.command_map[Key('ctrl-g')] = urwid.CANCEL
urwid.command_map[Key('ctrl-c')] = urwid.CANCEL
urwid.command_map[Key('ctrl-l')] = urwid.REDRAW_SCREEN
urwid.command_map[Key('tab')] = urwid.COMPLETE_NEXT
urwid.command_map[Key('shift-tab')] = urwid.COMPLETE_PREV
import re
import operator
class Edit_readline(urwid.Edit):
def keypress(self, size, key):
def move_to_next_word(forward=True):
if forward:
match_iterator = re.finditer(r'(\b\W+|$)', self.edit_text, flags=re.UNICODE)
match_positions = (m.start() for m in match_iterator)
op = operator.gt
else:
match_iterator = re.finditer(r'(\w+\b|^)', self.edit_text, flags=re.UNICODE)
def evaluate(self, key, context=DEFAULT_CONTEXT, callback=None, widget=None):
"""
Run action that is mapped to `key` in `context`
key: the pressed key
context: the context `key` was pressed in
callback: None or any callable
widget: the widget that received `key`
"""
if context not in self._actions:
raise ValueError('Unknown context: {}'.format(context))
key = Key(key)
action = None
# Unless we've are already started a key chain, try to find a single-key mapping
if not self.any_keychain_started:
log.debug('%s: Evaluating %r as single key for widget %r', context, key, widget)
action = self._get_single_key_action(key, context)
# Try to advance keychains only if no keychain was started previously or
# if that previously started keychain was in the same context as we're
# in now.
if action is None and (self._keychain_context == self.NO_CONTEXT or
self._keychain_context == context):
log.debug('%s: Evaluating %r as chain key for widget %r', context, key, widget)
action = self._get_keychain_action(key, context)
if action is KeyChain.ADVANCE:
log.debug('%s: %r was used to advance a keychain; locking context', context, key)
def __new__(cls, *keys):
obj = super().__new__(cls, (Key(k) for k in keys))
if len(keys) < 2:
raise ValueError('Not enough keys to make a chain: %s' % str(obj))
return obj
urwid.DELETE_WORD_RIGHT = 'delete word right'
urwid.CANCEL = 'cancel'
urwid.COMPLETE_NEXT = 'complete next'
urwid.COMPLETE_PREV = 'complete previous'
# Remove urwid's default keybindings and create our own built-in command_map
for key in tuple(urwid.command_map._command):
del urwid.command_map._command[key]
from .keymap import Key
urwid.command_map[Key('pgup')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('pgdn')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('ctrl-b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('ctrl-f')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('space')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('up')] = urwid.CURSOR_UP
urwid.command_map[Key('down')] = urwid.CURSOR_DOWN
urwid.command_map[Key('left')] = urwid.CURSOR_LEFT
urwid.command_map[Key('right')] = urwid.CURSOR_RIGHT
urwid.command_map[Key('meta-b')] = urwid.CURSOR_WORD_LEFT
urwid.command_map[Key('meta-f')] = urwid.CURSOR_WORD_RIGHT
urwid.command_map[Key('home')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('end')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-a')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('ctrl-e')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-k')] = urwid.DELETE_TO_EOL
urwid.command_map[Key('ctrl-u')] = urwid.DELETE_LINE
urwid.command_map[Key('ctrl-d')] = urwid.DELETE_CHAR_UNDER_CURSOR
# Remove urwid's default keybindings and create our own built-in command_map
for key in tuple(urwid.command_map._command):
del urwid.command_map._command[key]
from .keymap import Key
urwid.command_map[Key('pgup')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('pgdn')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('ctrl-b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('ctrl-f')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('space')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('up')] = urwid.CURSOR_UP
urwid.command_map[Key('down')] = urwid.CURSOR_DOWN
urwid.command_map[Key('left')] = urwid.CURSOR_LEFT
urwid.command_map[Key('right')] = urwid.CURSOR_RIGHT
urwid.command_map[Key('meta-b')] = urwid.CURSOR_WORD_LEFT
urwid.command_map[Key('meta-f')] = urwid.CURSOR_WORD_RIGHT
urwid.command_map[Key('home')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('end')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-a')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('ctrl-e')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-k')] = urwid.DELETE_TO_EOL
urwid.command_map[Key('ctrl-u')] = urwid.DELETE_LINE
urwid.command_map[Key('ctrl-d')] = urwid.DELETE_CHAR_UNDER_CURSOR
urwid.command_map[Key('meta-d')] = urwid.DELETE_WORD_LEFT
urwid.command_map[Key('meta-backspace')] = urwid.DELETE_WORD_RIGHT
urwid.command_map[Key('ctrl-w')] = urwid.DELETE_WORD_RIGHT
urwid.command_map[Key('up')] = urwid.CURSOR_UP
urwid.command_map[Key('down')] = urwid.CURSOR_DOWN
urwid.command_map[Key('left')] = urwid.CURSOR_LEFT
urwid.command_map[Key('right')] = urwid.CURSOR_RIGHT
urwid.command_map[Key('meta-b')] = urwid.CURSOR_WORD_LEFT
urwid.command_map[Key('meta-f')] = urwid.CURSOR_WORD_RIGHT
urwid.command_map[Key('home')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('end')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-a')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('ctrl-e')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-k')] = urwid.DELETE_TO_EOL
urwid.command_map[Key('ctrl-u')] = urwid.DELETE_LINE
urwid.command_map[Key('ctrl-d')] = urwid.DELETE_CHAR_UNDER_CURSOR
urwid.command_map[Key('meta-d')] = urwid.DELETE_WORD_LEFT
urwid.command_map[Key('meta-backspace')] = urwid.DELETE_WORD_RIGHT
urwid.command_map[Key('ctrl-w')] = urwid.DELETE_WORD_RIGHT
urwid.command_map[Key('enter')] = urwid.ACTIVATE
urwid.command_map[Key('escape')] = urwid.CANCEL
urwid.command_map[Key('ctrl-g')] = urwid.CANCEL
urwid.command_map[Key('ctrl-c')] = urwid.CANCEL
urwid.command_map[Key('ctrl-l')] = urwid.REDRAW_SCREEN
urwid.command_map[Key('tab')] = urwid.COMPLETE_NEXT
urwid.command_map[Key('shift-tab')] = urwid.COMPLETE_PREV
import re
import operator
class Edit_readline(urwid.Edit):
def keypress(self, size, key):
urwid.DELETE_TO_EOL = 'delete to end of line'
urwid.DELETE_LINE = 'delete line'
urwid.DELETE_CHAR_UNDER_CURSOR = 'delete char under cursor'
urwid.DELETE_WORD_LEFT = 'delete word left'
urwid.DELETE_WORD_RIGHT = 'delete word right'
urwid.CANCEL = 'cancel'
urwid.COMPLETE_NEXT = 'complete next'
urwid.COMPLETE_PREV = 'complete previous'
# Remove urwid's default keybindings and create our own built-in command_map
for key in tuple(urwid.command_map._command):
del urwid.command_map._command[key]
from .keymap import Key
urwid.command_map[Key('pgup')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('pgdn')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('ctrl-b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('ctrl-f')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('space')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('up')] = urwid.CURSOR_UP
urwid.command_map[Key('down')] = urwid.CURSOR_DOWN
urwid.command_map[Key('left')] = urwid.CURSOR_LEFT
urwid.command_map[Key('right')] = urwid.CURSOR_RIGHT
urwid.command_map[Key('meta-b')] = urwid.CURSOR_WORD_LEFT
urwid.command_map[Key('meta-f')] = urwid.CURSOR_WORD_RIGHT
urwid.command_map[Key('home')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('end')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-a')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('ctrl-e')] = urwid.CURSOR_MAX_RIGHT
urwid.DELETE_WORD_LEFT = 'delete word left'
urwid.DELETE_WORD_RIGHT = 'delete word right'
urwid.CANCEL = 'cancel'
urwid.COMPLETE_NEXT = 'complete next'
urwid.COMPLETE_PREV = 'complete previous'
# Remove urwid's default keybindings and create our own built-in command_map
for key in tuple(urwid.command_map._command):
del urwid.command_map._command[key]
from .keymap import Key
urwid.command_map[Key('pgup')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('pgdn')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('ctrl-b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('ctrl-f')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('b')] = urwid.CURSOR_PAGE_UP
urwid.command_map[Key('space')] = urwid.CURSOR_PAGE_DOWN
urwid.command_map[Key('up')] = urwid.CURSOR_UP
urwid.command_map[Key('down')] = urwid.CURSOR_DOWN
urwid.command_map[Key('left')] = urwid.CURSOR_LEFT
urwid.command_map[Key('right')] = urwid.CURSOR_RIGHT
urwid.command_map[Key('meta-b')] = urwid.CURSOR_WORD_LEFT
urwid.command_map[Key('meta-f')] = urwid.CURSOR_WORD_RIGHT
urwid.command_map[Key('home')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('end')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-a')] = urwid.CURSOR_MAX_LEFT
urwid.command_map[Key('ctrl-e')] = urwid.CURSOR_MAX_RIGHT
urwid.command_map[Key('ctrl-k')] = urwid.DELETE_TO_EOL
urwid.command_map[Key('ctrl-u')] = urwid.DELETE_LINE
def keypress(self, size, key_orig):
keymap = self.__keymap
context = self.__context
callback = self.__callback
key_eval = Key(key_orig)
def try_parent_class(key, sup=super()):
if sup.selectable():
# mro = sup.__self_class__.mro()
# super_cls = mro[mro.index(sup.__thisclass__) + 1]
# log.debug('%s: Offering %r to %s.keypress()', context, key, super_cls.__name__)
key = sup.keypress(size, key)
# log.debug('%s: %s.keypress() returned %r', context, super_cls.__name__, key)
return key
log.debug('%s: %r / %r pressed', context, key_orig, key_eval)
# Any started keychains have the highest priority
if keymap.any_keychain_started:
log.debug('%s: Advancing started keychain(s)', context)
key_ = keymap.evaluate(key_eval, context=context, callback=callback, widget=self)