Exemplo n.º 1
0
def test_previous_key_sequence(processor, handlers):
    """
    test whether we receive the correct previous_key_sequence.
    """
    events = []

    def handler(event):
        events.append(event)

    # Build registry.
    registry = Registry()
    registry.add_binding('a', 'a')(handler)
    registry.add_binding('b', 'b')(handler)
    processor = InputProcessor(registry, lambda: None)

    # Create processor and feed keys.
    processor.feed(KeyPress('a', 'a'))
    processor.feed(KeyPress('a', 'a'))
    processor.feed(KeyPress('b', 'b'))
    processor.feed(KeyPress('b', 'b'))
    processor.process_keys()

    # Test.
    assert len(events) == 2
    assert len(events[0].key_sequence) == 2
    assert events[0].key_sequence[0].key == 'a'
    assert events[0].key_sequence[0].data == 'a'
    assert events[0].key_sequence[1].key == 'a'
    assert events[0].key_sequence[1].data == 'a'
    assert events[0].previous_key_sequence == []

    assert len(events[1].key_sequence) == 2
    assert events[1].key_sequence[0].key == 'b'
    assert events[1].key_sequence[0].data == 'b'
    assert events[1].key_sequence[1].key == 'b'
    assert events[1].key_sequence[1].data == 'b'
    assert len(events[1].previous_key_sequence) == 2
    assert events[1].previous_key_sequence[0].key == 'a'
    assert events[1].previous_key_sequence[0].data == 'a'
    assert events[1].previous_key_sequence[1].key == 'a'
    assert events[1].previous_key_sequence[1].data == 'a'
def registry(handlers):
    registry = Registry()
    registry.add_binding(Keys.ControlX, Keys.ControlC)(handlers.controlx_controlc)
    registry.add_binding(Keys.ControlX)(handlers.control_x)
    registry.add_binding(Keys.ControlD)(handlers.control_d)
    registry.add_binding(Keys.ControlSquareClose, Keys.Any)(handlers.control_square_close_any)

    return registry
Exemplo n.º 3
0
def registry(handlers):
    registry = Registry()
    registry.add_binding(Keys.ControlX,
                         Keys.ControlC)(handlers.controlx_controlc)
    registry.add_binding(Keys.ControlX)(handlers.control_x)
    registry.add_binding(Keys.ControlD)(handlers.control_d)
    registry.add_binding(Keys.ControlSquareClose,
                         Keys.Any)(handlers.control_square_close_any)

    return registry
Exemplo n.º 4
0
Arquivo: ui.py Projeto: mswart/reptly
def select(message, *options):
    """
    Display a confirmation prompt.
    """
    styling = style_from_dict({
        Token.Key: 'bold',
        Token.DefaultKey: 'bold underline',
    })

    def _get_tokens(cli):
        yield (Token, message + ': ')
        for i, option in enumerate(options):
            if i:
                yield (Token, ', ')
            if option.default:
                yield (Token.DefaultKey, option.caption[0].upper())
            else:
                yield (Token.Key, option.caption[0].upper())
            yield (Token.Caption, option.caption[1:])
        yield (Token, '? ')

    def _event(option, event):
        event.cli.buffers[DEFAULT_BUFFER].text = option.output
        event.cli.set_return_value(option.value)

    registry = Registry()

    for option in options:
        handler = functools.partial(_event, option)
        for char in option.chars:
            registry.add_binding(char)(handler)
        if option.fallback:
            registry.add_binding(Keys.ControlC)(handler)
            registry.add_binding(Keys.Escape)(handler)
        if option.default:
            registry.add_binding(Keys.Enter)(handler)

    sys.stdout.flush()

    return run_application(
        create_prompt_application(
            get_prompt_tokens=_get_tokens,
            style=styling,
            key_bindings_registry=registry,
        ))
class KeyBindingTest(unittest.TestCase):
    def setUp(self):
        class Handlers(object):
            def __init__(self):
                self.called = []

            def __getattr__(self, name):
                def func(event):
                    self.called.append(name)

                return func

        self.handlers = Handlers()

        self.registry = Registry()
        self.registry.add_binding(Keys.ControlX, Keys.ControlC)(
            self.handlers.controlx_controlc)
        self.registry.add_binding(Keys.ControlX)(self.handlers.control_x)
        self.registry.add_binding(Keys.ControlD)(self.handlers.control_d)
        self.registry.add_binding(Keys.ControlSquareClose, Keys.Any)(
            self.handlers.control_square_close_any)

        self.processor = InputProcessor(self.registry, lambda: None)

    def test_feed_simple(self):
        self.processor.feed_key(KeyPress(Keys.ControlX, '\x18'))
        self.processor.feed_key(KeyPress(Keys.ControlC, '\x03'))

        self.assertEqual(self.handlers.called, ['controlx_controlc'])

    def test_feed_several(self):
        # First an unknown key first.
        self.processor.feed_key(KeyPress(Keys.ControlQ, ''))
        self.assertEqual(self.handlers.called, [])

        # Followed by a know key sequence.
        self.processor.feed_key(KeyPress(Keys.ControlX, ''))
        self.processor.feed_key(KeyPress(Keys.ControlC, ''))
        self.assertEqual(self.handlers.called, ['controlx_controlc'])

        # Followed by another unknown sequence.
        self.processor.feed_key(KeyPress(Keys.ControlR, ''))
        self.processor.feed_key(KeyPress(Keys.ControlS, ''))

        # Followed again by a know key sequence.
        self.processor.feed_key(KeyPress(Keys.ControlD, ''))
        self.assertEqual(self.handlers.called,
                         ['controlx_controlc', 'control_d'])

    def test_control_square_closed_any(self):
        self.processor.feed_key(KeyPress(Keys.ControlSquareClose, ''))
        self.processor.feed_key(KeyPress('C', 'C'))

        self.assertEqual(self.handlers.called, ['control_square_close_any'])

    def test_common_prefix(self):
        # Sending Control_X should not yet do anything, because there is
        # another sequence starting with that as well.
        self.processor.feed_key(KeyPress(Keys.ControlX, ''))
        self.assertEqual(self.handlers.called, [])

        # When another key is pressed, we know that we did not meant the longer
        # "ControlX ControlC" sequence and the callbacks are called.
        self.processor.feed_key(KeyPress(Keys.ControlD, ''))

        self.assertEqual(self.handlers.called, ['control_x', 'control_d'])
Exemplo n.º 6
0
class KeyBindingTest(unittest.TestCase):
    def setUp(self):
        class Handlers(object):
            def __init__(self):
                self.called = []

            def __getattr__(self, name):
                def func(event):
                    self.called.append(name)
                return func

        self.handlers = Handlers()

        self.registry = Registry()
        self.registry.add_binding(Keys.ControlX, Keys.ControlC)(self.handlers.controlx_controlc)
        self.registry.add_binding(Keys.ControlX)(self.handlers.control_x)
        self.registry.add_binding(Keys.ControlD)(self.handlers.control_d)
        self.registry.add_binding(Keys.ControlSquareClose, Keys.Any)(self.handlers.control_square_close_any)

        self.processor = InputProcessor(self.registry, lambda: None)

    def test_feed_simple(self):
        self.processor.feed_key(KeyPress(Keys.ControlX, '\x18'))
        self.processor.feed_key(KeyPress(Keys.ControlC, '\x03'))

        self.assertEqual(self.handlers.called, ['controlx_controlc'])

    def test_feed_several(self):
        # First an unknown key first.
        self.processor.feed_key(KeyPress(Keys.ControlQ, ''))
        self.assertEqual(self.handlers.called, [])

        # Followed by a know key sequence.
        self.processor.feed_key(KeyPress(Keys.ControlX, ''))
        self.processor.feed_key(KeyPress(Keys.ControlC, ''))
        self.assertEqual(self.handlers.called, ['controlx_controlc'])

        # Followed by another unknown sequence.
        self.processor.feed_key(KeyPress(Keys.ControlR, ''))
        self.processor.feed_key(KeyPress(Keys.ControlS, ''))

        # Followed again by a know key sequence.
        self.processor.feed_key(KeyPress(Keys.ControlD, ''))
        self.assertEqual(self.handlers.called, ['controlx_controlc', 'control_d'])

    def test_control_square_closed_any(self):
        self.processor.feed_key(KeyPress(Keys.ControlSquareClose, ''))
        self.processor.feed_key(KeyPress('C', 'C'))

        self.assertEqual(self.handlers.called, ['control_square_close_any'])

    def test_common_prefix(self):
        # Sending Control_X should not yet do anything, because there is
        # another sequence starting with that as well.
        self.processor.feed_key(KeyPress(Keys.ControlX, ''))
        self.assertEqual(self.handlers.called, [])

        # When another key is pressed, we know that we did not meant the longer
        # "ControlX ControlC" sequence and the callbacks are called.
        self.processor.feed_key(KeyPress(Keys.ControlD, ''))

        self.assertEqual(self.handlers.called, ['control_x', 'control_d'])