Skip to content

Add select all, reset and invert selection to checkbox #275

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

Merged
merged 2 commits into from
Dec 15, 2022
Merged
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
7 changes: 6 additions & 1 deletion docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Result on something like:

|inquirer checkbox|

Apart from all the controlls supported by other questions, checkbox supports some more as shown above:

* `Ctrl + A` to select all.
* `Ctrl + R` to un-select all the choices, even the defaults.
* `Ctrl + I` to invert/toggle all the choices.

The :code:`choices` list can also be a list of tuples. The first value in each tuple should be the label displayed to the user. The second value in each tuple should be the actual value for that option. This allows you to have the user choose options that are not plain strings in the code.

Expand All @@ -64,7 +69,7 @@ Result on something like:
.. |inquirer list| image:: images/inquirer_list.png
:alt: Example of List Question

.. |inquirer checkbox| image:: images/inquirer_checkbox.png
.. |inquirer checkbox| image:: images/inquirer_checkbox.gif
:alt: Example of Checkbox Question

.. |inquirer theme| image:: images/inquirer_theme.gif
Expand Down
Binary file added docs/images/inquirer_checkbox.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/inquirer/render/console/_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,12 @@ 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:
self.selection = [i for i in range(len(self.question.choices))]
elif pressed == key.CTRL_R:
self.selection = []
elif pressed == key.CTRL_I:
self.selection = [i for i in range(len(self.question.choices)) if i not in self.selection]
elif pressed == key.ENTER:
result = []
for x in self.selection:
Expand Down
17 changes: 17 additions & 0 deletions tests/acceptance/test_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ def test_select_last(self):
self.sut.send(key.ENTER)
self.sut.expect(r"{'interests': \['Computers', 'Books', 'History'\]}.*", 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_reset_with_ctrl_r(self):
self.sut.send(key.CTRL_R)
self.sut.send(key.ENTER)
self.sut.expect(r"{'interests': \[\]}.*", timeout=1) # noqa

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


@unittest.skipUnless(sys.platform.startswith("lin"), "Linux only")
class CheckCarouselTest(unittest.TestCase):
Expand Down
84 changes: 84 additions & 0 deletions tests/integration/console_render/test_checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,3 +240,87 @@ def test_left_cursor_do_not_select(self):
result = sut.render(question)

assert result == []

def test_select_all(self):
stdin_array = [key.CTRL_A, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]

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

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

assert result == choices

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

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

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

assert result == []

def test_reset_all_selection(self):
stdin_array = [key.CTRL_A, key.CTRL_R, key.ENTER]
stdin = helper.event_factory(*stdin_array)
message = "Foo message"
variable = "Bar variable"
choices = ["foo", "bar", "bazz"]

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

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

assert result == []

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

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

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

assert result == ["bar", "bazz"]

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

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

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

assert result == choices

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

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

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

assert result == []