Skip to content

Add select all and tab controls #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ Example:
answers = inquirer.prompt(questions)

Checkbox questions can take one extra argument :code:`carousel=False`. If set to true, the answers will rotate (back to first when pressing down on last choice, and down to last choice when pressing up on first choice)
Use ctrl+a and ctrl+q to select and unselect all

|inquirer checkbox|

Expand Down
9 changes: 7 additions & 2 deletions src/inquirer/render/console/_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ def get_options(self):

def process_input(self, pressed):
question = self.question
if pressed == key.UP:
if pressed in (key.UP, key.SHIFT_TAB):
if question.carousel and self.current == 0:
self.current = len(question.choices) - 1
else:
self.current = max(0, self.current - 1)
return
elif pressed == key.DOWN:
elif pressed in (key.DOWN, key.CTRL_I):
if question.carousel and self.current == len(question.choices) - 1:
self.current = 0
else:
Expand All @@ -89,6 +89,11 @@ def process_input(self, pressed):
elif pressed == key.RIGHT:
if self.current not in self.selection:
self.selection.append(self.current)
elif pressed == key.CTRL_A:
Copy link
Collaborator

Choose a reason for hiding this comment

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

You added new branches to the code without adding tests for them. So this change request is reducing our test coverage. Please update the tests.

for x in self.question.choices:
self.selection = list(range(len(self.question.choices)))
elif pressed == key.CTRL_Q:
self.selection = []
elif pressed == key.ENTER:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also I see there are no mentions of the changes on documentation and change-logs.

Copy link
Author

Choose a reason for hiding this comment

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

Where do i can found change-logs files?

result = []
for x in self.selection:
Expand Down
27 changes: 27 additions & 0 deletions tests/acceptance/test_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,33 @@ def test_select_last(self):
self.sut.send(key.ENTER)
self.sut.expect(r"{'interests': \['Computers', 'Books', 'History'\]}.*", timeout=1) # noqa

def test_navigate_with_tab(self):
self.sut.send(key.CTRL_I)
self.sut.send(key.LEFT)
self.sut.send(key.ENTER)
self.sut.expect(r"{'interests': \['Computers'\]}.*", timeout=1) # noqa

# Check after merge release readchar >= dev 4.0.0
@unittest.SkipTest
def test_navigate_with_shift_tab(self):
self.sut.send(key.DOWN)
self.sut.send(key.DOWN)
self.sut.send(key.SHIFT_TAB)
self.sut.send(key.LEFT)
self.sut.send(key.ENTER)
self.sut.expect(r"{'interests': \['Computers'\]}.*", timeout=1) # noqa

def test_select_all_with_ctrl_a(self):
self.sut.send(key.CTRL_A)
self.sut.send(key.ENTER)
self.sut.expect(r"{'interests': \['Computers', 'Books', 'Science', 'Nature', 'Fantasy', 'History'\]}.*", timeout=1) # noqa

def test_unselect_all_with_ctrl_q(self):
self.sut.send(key.CTRL_A)
self.sut.send(key.CTRL_Q)
self.sut.send(key.ENTER)
self.sut.expect(r"{'interests': \[\]}.*", timeout=1) # noqa


@unittest.skipUnless(sys.platform.startswith("lin"), "Linux only")
class CheckCarouselTest(unittest.TestCase):
Expand Down
28 changes: 28 additions & 0 deletions tests/integration/console_render/test_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,19 @@ def test_move_down_carousel(self):

assert result == ["bar"]

def test_move_down_with_tab_carousel(self):
stdin = helper.event_factory(key.CTRL_I, key.CTRL_I, key.CTRL_I, key.CTRL_I, key.SPACE, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]

question = questions.Checkbox(variable, message, choices=choices, carousel=True)

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

assert result == ["bar"]

def test_move_up_carousel(self):
stdin = helper.event_factory(key.UP, key.SPACE, key.ENTER)
message = "Foo message"
Expand All @@ -158,6 +171,21 @@ def test_move_up_carousel(self):

assert result == ["bazz"]

# Check after merge release readchar >= dev 4.0.0
@unittest.SkipTest
def test_move_up_with_shift_tab_carousel(self):
stdin = helper.event_factory(key.SHIFT_TAB, key.SPACE, key.ENTER)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]

question = questions.Checkbox(variable, message, choices=choices, carousel=True)

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

assert result == ["bazz"]

def test_ctrl_c_breaks_execution(self):
stdin_array = [key.CTRL_C]
stdin = helper.event_factory(*stdin_array)
Expand Down