From ec46aef38724f1e6cdf1f8c101dce73e95a21548 Mon Sep 17 00:00:00 2001 From: fleytman Date: Sun, 17 Jul 2022 14:02:58 +0300 Subject: [PATCH 1/6] add selext all and tab controls --- src/inquirer/render/console/_checkbox.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/inquirer/render/console/_checkbox.py b/src/inquirer/render/console/_checkbox.py index 7adb1197..437ca64f 100644 --- a/src/inquirer/render/console/_checkbox.py +++ b/src/inquirer/render/console/_checkbox.py @@ -65,14 +65,16 @@ def get_options(self): yield choice, selector + " " + symbol, color def process_input(self, pressed): + # Remove after merge https://github.com/magmax/python-readchar/pull/83 + key.SHIFT_TAB = "\x1b\x5b\x5a" 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: @@ -89,6 +91,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: + 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: result = [] for x in self.selection: From d46c2530cc7399c88d9deb8a8812510fb53197b6 Mon Sep 17 00:00:00 2001 From: fleytman Date: Sun, 17 Jul 2022 15:16:49 +0300 Subject: [PATCH 2/6] add tests --- tests/acceptance/test_checkbox.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/acceptance/test_checkbox.py b/tests/acceptance/test_checkbox.py index 5bda0dfd..0c13776a 100644 --- a/tests/acceptance/test_checkbox.py +++ b/tests/acceptance/test_checkbox.py @@ -57,6 +57,31 @@ 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 https://github.com/magmax/python-readchar/pull/79 + @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.sur.send(key.CTRL_A) + self.sut.expect(r"{'interests': \['Computers', 'Books', 'Science', 'Nature', 'Fantasy', 'History'\]}.*", timeout=1) # noqa + + def test_unselect_all_with_ctrl_q(self): + self.sur.send(key.CTRL_A) + self.sur.send(key.CTRL_Q) + self.sut.expect(r"{'interests': \[\]}.*", timeout=1) # noqa + @unittest.skipUnless(sys.platform.startswith("lin"), "Linux only") class CheckCarouselTest(unittest.TestCase): From 65cca7f42694de17be03a60e02eae703a4b3d016 Mon Sep 17 00:00:00 2001 From: fleytman Date: Sun, 17 Jul 2022 15:33:42 +0300 Subject: [PATCH 3/6] add select and unselect info to README.rst fix test --- README.rst | 1 + tests/acceptance/test_checkbox.py | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.rst b/README.rst index 5c5f2440..469c62a1 100644 --- a/README.rst +++ b/README.rst @@ -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| diff --git a/tests/acceptance/test_checkbox.py b/tests/acceptance/test_checkbox.py index 0c13776a..92b02a6a 100644 --- a/tests/acceptance/test_checkbox.py +++ b/tests/acceptance/test_checkbox.py @@ -74,12 +74,12 @@ def test_navigate_with_shift_tab(self): self.sut.expect(r"{'interests': \['Computers'\]}.*", timeout=1) # noqa def test_select_all_with_ctrl_a(self): - self.sur.send(key.CTRL_A) + self.sut.send(key.CTRL_A) self.sut.expect(r"{'interests': \['Computers', 'Books', 'Science', 'Nature', 'Fantasy', 'History'\]}.*", timeout=1) # noqa def test_unselect_all_with_ctrl_q(self): - self.sur.send(key.CTRL_A) - self.sur.send(key.CTRL_Q) + self.sut.send(key.CTRL_A) + self.sut.send(key.CTRL_Q) self.sut.expect(r"{'interests': \[\]}.*", timeout=1) # noqa From c1598fdb74acd9f63a73cb7da885616d230577f0 Mon Sep 17 00:00:00 2001 From: fleytman Date: Sun, 17 Jul 2022 14:07:12 +0300 Subject: [PATCH 4/6] change link to mr --- src/inquirer/render/console/_checkbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/inquirer/render/console/_checkbox.py b/src/inquirer/render/console/_checkbox.py index 437ca64f..c77bae03 100644 --- a/src/inquirer/render/console/_checkbox.py +++ b/src/inquirer/render/console/_checkbox.py @@ -65,7 +65,7 @@ def get_options(self): yield choice, selector + " " + symbol, color def process_input(self, pressed): - # Remove after merge https://github.com/magmax/python-readchar/pull/83 + # Remove after merge https://github.com/magmax/python-readchar/pull/79 key.SHIFT_TAB = "\x1b\x5b\x5a" question = self.question if pressed in (key.UP, key.SHIFT_TAB): From 163874eef6e17bcefb2300041c566583aa261d71 Mon Sep 17 00:00:00 2001 From: fleytman Date: Mon, 18 Jul 2022 12:38:43 +0300 Subject: [PATCH 5/6] fix tests --- tests/acceptance/test_checkbox.py | 2 ++ .../console_render/test_checkbox.py | 27 +++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/tests/acceptance/test_checkbox.py b/tests/acceptance/test_checkbox.py index 92b02a6a..213cbb83 100644 --- a/tests/acceptance/test_checkbox.py +++ b/tests/acceptance/test_checkbox.py @@ -75,11 +75,13 @@ def test_navigate_with_shift_tab(self): 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 diff --git a/tests/integration/console_render/test_checkbox.py b/tests/integration/console_render/test_checkbox.py index 0350d74e..1f8ed09e 100644 --- a/tests/integration/console_render/test_checkbox.py +++ b/tests/integration/console_render/test_checkbox.py @@ -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" @@ -158,6 +171,20 @@ def test_move_up_carousel(self): assert result == ["bazz"] + @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) From 30b95b64977d354e2547b18e0e1512fec59d5b20 Mon Sep 17 00:00:00 2001 From: fleytman Date: Fri, 29 Jul 2022 16:59:56 +0300 Subject: [PATCH 6/6] remove shift+tab key change comment in tests --- src/inquirer/render/console/_checkbox.py | 2 -- tests/acceptance/test_checkbox.py | 2 +- tests/integration/console_render/test_checkbox.py | 1 + 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/inquirer/render/console/_checkbox.py b/src/inquirer/render/console/_checkbox.py index c77bae03..887720a8 100644 --- a/src/inquirer/render/console/_checkbox.py +++ b/src/inquirer/render/console/_checkbox.py @@ -65,8 +65,6 @@ def get_options(self): yield choice, selector + " " + symbol, color def process_input(self, pressed): - # Remove after merge https://github.com/magmax/python-readchar/pull/79 - key.SHIFT_TAB = "\x1b\x5b\x5a" question = self.question if pressed in (key.UP, key.SHIFT_TAB): if question.carousel and self.current == 0: diff --git a/tests/acceptance/test_checkbox.py b/tests/acceptance/test_checkbox.py index 213cbb83..8669ffef 100644 --- a/tests/acceptance/test_checkbox.py +++ b/tests/acceptance/test_checkbox.py @@ -63,7 +63,7 @@ def test_navigate_with_tab(self): self.sut.send(key.ENTER) self.sut.expect(r"{'interests': \['Computers'\]}.*", timeout=1) # noqa - # Check after merge https://github.com/magmax/python-readchar/pull/79 + # Check after merge release readchar >= dev 4.0.0 @unittest.SkipTest def test_navigate_with_shift_tab(self): self.sut.send(key.DOWN) diff --git a/tests/integration/console_render/test_checkbox.py b/tests/integration/console_render/test_checkbox.py index 1f8ed09e..dd7e18e6 100644 --- a/tests/integration/console_render/test_checkbox.py +++ b/tests/integration/console_render/test_checkbox.py @@ -171,6 +171,7 @@ 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)