How to use the readchar.key.BACKSPACE function in readchar

To help you get started, we’ve selected a few readchar examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github magmax / python-inquirer / tests / acceptance / test_password.py View on Github external
def test_backspace_limit(self):
        self.sut.expect("What's.*", timeout=1)
        self.sut.send('a')
        self.sut.send(key.BACKSPACE)
        self.sut.send(key.BACKSPACE)
        self.sut.send('b')
        self.sut.send(key.ENTER)
        self.sut.expect("{'password': 'b'}", timeout=1)
github magmax / python-inquirer / tests / acceptance / test_password.py View on Github external
def test_backspace_limit(self):
        self.sut.expect("What's.*", timeout=1)
        self.sut.send('a')
        self.sut.send(key.BACKSPACE)
        self.sut.send(key.BACKSPACE)
        self.sut.send('b')
        self.sut.send(key.ENTER)
        self.sut.expect("{'password': 'b'}", timeout=1)
github Kamik423 / cutie / test / test_prompt_yes_or_no.py View on Github external
def test_backspace_delete_char(self, mock_print):
        expected_calls = [
                            (tuple(),),
                            print_call("Yes", "selected"),
                            print_call("No"),
                            (('\x1b[3A\r\x1b[Kfoo (Y/N) Ye',), {"end": '', "flush": True},),
                            (('\x1b[K\n\x1b[K\n\x1b[K\n\x1b[3A',),),
                        ]
        with InputContext(readchar.key.UP, readchar.key.BACKSPACE, "\r"):
            cutie.prompt_yes_or_no("foo")
            self.assertEqual(mock_print.call_args_list[-5:], expected_calls)
github magmax / python-inquirer / tests / integration / console_render / test_password.py View on Github external
def test_allows_deletion(self):
        stdin_array = ['a', key.BACKSPACE, 'b', key.ENTER]
        stdin = helper.event_factory(*stdin_array)
        message = 'Foo message'
        variable = 'Bar variable'

        question = questions.Password(variable, message)

        sut = ConsoleRender(event_generator=stdin)
        result = sut.render(question)

        self.assertEqual('b', result)
github magmax / python-inquirer / tests / integration / console_render / test_text.py View on Github external
def test_validation_fails(self):
        stdin_array = [x for x in
                       'Invalid' + key.ENTER +
                       key.BACKSPACE*20 +
                       '9999' + key.ENTER]
        stdin = helper.event_factory(*stdin_array)

        message = 'Insert number'
        variable = 'foo'
        expected = '9999'

        question = questions.Text(variable,
                                  validate=lambda _, x: re.match(r'\d+', x),
                                  message=message)

        sut = ConsoleRender(event_generator=stdin)
        result = sut.render(question)
        self.assertEqual(expected, result)
        self.assertInStdout(message)
        self.assertInStdout('"Invalid" is not a valid foo')
github magmax / python-inquirer / tests / acceptance / test_text.py View on Github external
def test_invalid_phone(self):
        self.set_name()
        self.set_surname()
        self.set_phone('abcde')
        self.sut.expect('I don\'t like your phone number!', timeout=1)
        self.sut.sendline(5*key.BACKSPACE + '12345')
        self.sut.expect_list([re.compile(b"'name': 'foo'"),
                              re.compile(b"'surname': 'bar'"),
                              re.compile(b"'phone': '12345'")],
                             timeout=1)
github Kamik423 / cutie / cutie.py View on Github external
class DefaultKeys:
    """List of default keybindings.

    Attributes:
        interrupt(List[str]): Keys that cause a keyboard interrupt.
        select(List[str]): Keys that trigger list element selection.
        confirm(List[str]): Keys that trigger list confirmation.
        delete(List[str]): Keys that trigger character deletion.
        down(List[str]): Keys that select the element below.
        up(List[str]): Keys that select the element above.
    """
    interrupt: List[str] = [readchar.key.CTRL_C, readchar.key.CTRL_D]
    select: List[str] = [readchar.key.SPACE]
    confirm: List[str] = [readchar.key.ENTER]
    delete: List[str] = [readchar.key.BACKSPACE]
    down: List[str] = [readchar.key.DOWN, 'j']
    up: List[str] = [readchar.key.UP, 'k']


def get_number(
        prompt: str,
        min_value: Optional[float] = None,
        max_value: Optional[float] = None,
        allow_float: bool = True) -> float:
    """Get a number from user input.
    If an invalid number is entered the user will be prompted again.

    Args:
        prompt (str): The prompt asking the user to input.
        min_value (float, optional): The [inclusive] minimum value.
        max_value (float, optional): The [inclusive] maximum value.
github magmax / python-inquirer / inquirer / render / console / _text.py View on Github external
def process_input(self, pressed):
        if pressed == key.CTRL_C:
            raise KeyboardInterrupt()

        if pressed in (key.CR, key.LF, key.ENTER):
            raise errors.EndOfInput(self.current)

        if pressed == key.BACKSPACE:
            if self.current and self.cursor_offset != len(self.current):
                if self.cursor_offset > 0:
                    self.current = (self.current[:-self.cursor_offset - 1] +
                                    self.current[-self.cursor_offset:])
                else:
                    self.current = self.current[:-1]
        elif pressed == key.LEFT:
            if self.cursor_offset < len(self.current):
                self.cursor_offset += 1
        elif pressed == key.RIGHT:
            self.cursor_offset = max(self.cursor_offset - 1, 0)
        elif len(pressed) != 1:
            return
        else:
            if self.cursor_offset == 0:
                self.current += pressed

readchar

Library to easily read single chars and key strokes

MIT
Latest version published 17 days ago

Package Health Score

88 / 100
Full package analysis

Similar packages