Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions questionary/prompts/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does everything work correctly if there are fewer than 10 items? For example, if there are 9 items, page down should go to the 9th. Could you write a test for this?

ic.select_next()

while not ic.is_selection_valid():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if you have a list with 1 valid item and 99 invalid items? In such a case, I think this might cause an infinite loop (unless ic.select_next() loops around to the start of the list)? Can you write a test to cover this.

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():
Expand Down
68 changes: 68 additions & 0 deletions tests/prompts/test_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
4 changes: 4 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down