diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index a0948291..d935e696 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -246,6 +246,30 @@ def all(_event): perform_validation(get_selected_values()) + @bindings.add(Keys.Home, eager=True) + def move_cursor_start(event): + ic.pointed_at = 0 + + @bindings.add(Keys.End, eager=True) + def move_cursor_end(event): + ic.pointed_at = len(ic.choices) - 1 + + @bindings.add(Keys.PageDown, eager=True) + def move_cursor_down_fast(event): + for _ in range(10): + ic.select_next() + + while not ic.is_selection_valid(): + ic.select_next() + + @bindings.add(Keys.PageUp, eager=True) + def move_cursor_up_fast(event): + for _ in range(10): + ic.select_previous() + + while not ic.is_selection_valid(): + ic.select_previous() + def move_cursor_down(event): ic.select_next() while not ic.is_selection_valid(): diff --git a/tests/prompts/test_checkbox.py b/tests/prompts/test_checkbox.py index 705238d0..d41b882a 100644 --- a/tests/prompts/test_checkbox.py +++ b/tests/prompts/test_checkbox.py @@ -341,3 +341,71 @@ def test_fail_on_no_method_to_move_selection(): with pytest.raises(ValueError): feed_cli_with_input("checkbox", message, text, **kwargs) + + +def test_home(): + message = "Foo message" + kwargs = {"choices": ["foo", "bar", "bazz"]} + text = KeyInputs.DOWN + KeyInputs.HOME + KeyInputs.SPACE + KeyInputs.ENTER + "\r" + + result, cli = feed_cli_with_input("checkbox", message, text, **kwargs) + assert result == ["foo"] + + +def test_end(): + message = "Foo message" + kwargs = {"choices": ["foo", "bar", "bazz"]} + text = KeyInputs.END + KeyInputs.SPACE + KeyInputs.ENTER + "\r" + + result, cli = feed_cli_with_input("checkbox", message, text, **kwargs) + assert result == ["bazz"] + + +def test_pageup(): + message = "Foo message" + kwargs = { + "choices": [ + "foo0", + "foo1", + "foo2", + "foo3", + "foo4", + "foo5", + "foo6", + "foo7", + "foo8", + "foo9", + "bar", + "bazz", + ] + } + text = ( + KeyInputs.DOWN * 10 + KeyInputs.PGUP + KeyInputs.SPACE + KeyInputs.ENTER + "\r" + ) + + result, cli = feed_cli_with_input("checkbox", message, text, **kwargs) + assert result == ["foo0"] + + +def test_pagedown(): + message = "Foo message" + kwargs = { + "choices": [ + "foo0", + "foo1", + "foo2", + "foo3", + "foo4", + "foo5", + "foo6", + "foo7", + "foo8", + "foo9", + "bar", + "bazz", + ] + } + text = KeyInputs.PGDN + KeyInputs.SPACE + KeyInputs.ENTER + "\r" + + result, cli = feed_cli_with_input("checkbox", message, text, **kwargs) + assert result == ["bar"] diff --git a/tests/utils.py b/tests/utils.py index 2737c336..d9034c2e 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -28,6 +28,10 @@ class KeyInputs: ONE = "1" TWO = "2" THREE = "3" + END = "\x1b[F" + HOME = "\x1b[H" + PGUP = "\x1b[5~" + PGDN = "\x1b[6~" def feed_cli_with_input(_type, message, texts, sleep_time=1, **kwargs):