From f27fc033fcd1dae6998798e00a97dcd129691fd2 Mon Sep 17 00:00:00 2001 From: asolana Date: Wed, 5 Oct 2022 16:59:02 +0200 Subject: [PATCH 1/4] Add shortcuts to traverse Checkbox faster --- questionary/prompts/checkbox.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index a0948291..82243f49 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.End, eager=True) + def move_cursor_start(event): + ic.pointed_at = 0 + + @bindings.add(Keys.Home, 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(): From 7b2007bfb511090110d43d26012d7ac965251c32 Mon Sep 17 00:00:00 2001 From: asolana Date: Wed, 5 Oct 2022 17:04:00 +0200 Subject: [PATCH 2/4] Home and End were reversed --- questionary/prompts/checkbox.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 82243f49..d935e696 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -246,11 +246,11 @@ def all(_event): perform_validation(get_selected_values()) - @bindings.add(Keys.End, eager=True) + @bindings.add(Keys.Home, eager=True) def move_cursor_start(event): ic.pointed_at = 0 - @bindings.add(Keys.Home, eager=True) + @bindings.add(Keys.End, eager=True) def move_cursor_end(event): ic.pointed_at = len(ic.choices) - 1 From b5288dcdbcd03bc1622f81ba01f0dfcecd7c6328 Mon Sep 17 00:00:00 2001 From: asolana Date: Tue, 29 Nov 2022 13:15:23 +0100 Subject: [PATCH 3/4] Added unit tests --- tests/prompts/test_checkbox.py | 61 ++++++++++++++++++++++++++++++++++ tests/utils.py | 4 +++ 2 files changed, 65 insertions(+) diff --git a/tests/prompts/test_checkbox.py b/tests/prompts/test_checkbox.py index 705238d0..7cff1ef8 100644 --- a/tests/prompts/test_checkbox.py +++ b/tests/prompts/test_checkbox.py @@ -120,6 +120,7 @@ def test_cycle_to_first_choice(): assert result == ["foo"] + def test_cycle_backwards(): message = "Foo message" kwargs = {"choices": ["foo", "bar", "bazz"]} @@ -341,3 +342,63 @@ 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"] \ No newline at end of file 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): From c1e8389aa035352b360ca5892687ec98cb277d97 Mon Sep 17 00:00:00 2001 From: asolana Date: Wed, 30 Nov 2022 09:45:41 +0100 Subject: [PATCH 4/4] Code quality --- tests/prompts/test_checkbox.py | 73 +++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 33 deletions(-) diff --git a/tests/prompts/test_checkbox.py b/tests/prompts/test_checkbox.py index 7cff1ef8..d41b882a 100644 --- a/tests/prompts/test_checkbox.py +++ b/tests/prompts/test_checkbox.py @@ -120,7 +120,6 @@ def test_cycle_to_first_choice(): assert result == ["foo"] - def test_cycle_backwards(): message = "Foo message" kwargs = {"choices": ["foo", "bar", "bazz"]} @@ -360,45 +359,53 @@ def test_end(): 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" + 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" - ]} + 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"] \ No newline at end of file + assert result == ["bar"]