From 376d90af2ced23b31f40bb7bb1363b72ffab809c Mon Sep 17 00:00:00 2001 From: baturayo Date: Thu, 25 May 2023 14:34:08 +0200 Subject: [PATCH 01/24] Add dynamic instruction support --- questionary/prompts/checkbox.py | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index a0948291..a24db04b 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -38,6 +38,7 @@ def checkbox( use_arrow_keys: bool = True, use_jk_keys: bool = True, use_emacs_keys: bool = True, + instruction: Optional[str] = None, **kwargs: Any, ) -> Question: """Ask the user to select from a list of items. @@ -164,15 +165,18 @@ def get_prompt_tokens() -> List[Tuple[str, str]]: ("class:answer", "done ({} selections)".format(nbr_selected)) ) else: - tokens.append( - ( - "class:instruction", - "(Use arrow keys to move, " - " to select, " - " to toggle, " - " to invert)", + if instruction: + tokens.append(("class:instruction", instruction)) + else: + tokens.append( + ( + "class:instruction", + "(Use arrow keys to move, " + " to select, " + " to toggle, " + " to invert)", + ) ) - ) return tokens def get_selected_values() -> List[Any]: From cc5b20e3e9a3d4d1d5b4d5e96c3123f199291269 Mon Sep 17 00:00:00 2001 From: baturayo Date: Thu, 25 May 2023 14:42:56 +0200 Subject: [PATCH 02/24] document instruction --- questionary/prompts/checkbox.py | 1 + 1 file changed, 1 insertion(+) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index a24db04b..6e403bc3 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -105,6 +105,7 @@ def checkbox( use_emacs_keys: Allow the user to select items from the list using `Ctrl+N` (down) and `Ctrl+P` (up) keys. + instruction: A hint on how to navigate the menu. Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). From 97dfbb2b7459772aaa13d7f8b421f99bd0e9a280 Mon Sep 17 00:00:00 2001 From: baturayo Date: Wed, 31 May 2023 12:06:18 +0200 Subject: [PATCH 03/24] Add custom key binding for checkbox, confirm, select and text --- questionary/prompts/checkbox.py | 18 ++++++++++++++++++ questionary/prompts/confirm.py | 23 +++++++++++++++++++++-- questionary/prompts/select.py | 18 ++++++++++++++++++ questionary/prompts/text.py | 25 +++++++++++++++++++++++++ tests/prompts/test_checkbox.py | 14 ++++++++++++++ tests/prompts/test_confirm.py | 13 +++++++++++++ tests/prompts/test_select.py | 13 +++++++++++++ 7 files changed, 122 insertions(+), 2 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 95234652..c50b551b 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -38,6 +38,7 @@ def checkbox( use_jk_keys: bool = True, use_emacs_keys: bool = True, instruction: Optional[str] = None, + custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, **kwargs: Any, ) -> Question: """Ask the user to select from a list of items. @@ -104,8 +105,22 @@ def checkbox( use_emacs_keys: Allow the user to select items from the list using `Ctrl+N` (down) and `Ctrl+P` (up) keys. + instruction: A message describing how to navigate the menu. + custom_key_binding: Dictionary of custom key bindings. The keys are either + strings or `prompt_toolkit.keys.Keys` objects and the values are + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event + + Example: + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + ``` + { + "c": lambda event: event.app.exit(result="custom"), + Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), + } + ``` Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). """ @@ -202,6 +217,9 @@ def perform_validation(selected_values: List[str]) -> bool: layout = common.create_inquirer_layout(ic, get_prompt_tokens, **kwargs) bindings = KeyBindings() + if custom_key_binding is not None: + for key, func in custom_key_binding.items(): + bindings.add(key, eager=True)(func) @bindings.add(Keys.ControlQ, eager=True) @bindings.add(Keys.ControlC, eager=True) diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index 6d5c415c..e6865311 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -1,5 +1,4 @@ -from typing import Any -from typing import Optional +from typing import Any, Callable, Optional, Dict from prompt_toolkit import PromptSession from prompt_toolkit.formatted_text import to_formatted_text @@ -22,6 +21,7 @@ def confirm( qmark: str = DEFAULT_QUESTION_PREFIX, style: Optional[Style] = None, auto_enter: bool = True, + custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, **kwargs: Any, ) -> Question: """A yes or no question. The user can either confirm or deny. @@ -58,6 +58,20 @@ def confirm( accept their answer. If set to `True`, a valid input will be accepted without the need to press 'Enter'. + custom_key_binding: Dictionary of custom key bindings. The keys are either + strings or `prompt_toolkit.keys.Keys` objects and the values are + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event + + Example: + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + ``` + { + "c": lambda event: event.app.exit(result="custom"), + Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), + } + ``` + Returns: :class:`Question`: Question instance, ready to be prompted (using `.ask()`). """ @@ -86,6 +100,9 @@ def exit_with_result(event): event.app.exit(result=status["answer"]) bindings = KeyBindings() + if custom_key_binding is not None: + for key, func in custom_key_binding.items(): + bindings.add(key, eager=True)(func) @bindings.add(Keys.ControlQ, eager=True) @bindings.add(Keys.ControlC, eager=True) @@ -106,6 +123,8 @@ def key_y(event): if auto_enter: exit_with_result(event) + + @bindings.add(Keys.ControlH) def key_backspace(event): status["answer"] = None diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index d6d41c5d..6c918ade 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- from typing import Any +from typing import Callable from typing import Dict from typing import Optional from typing import Sequence @@ -36,6 +37,7 @@ def select( use_emacs_keys: bool = True, show_selected: bool = False, instruction: Optional[str] = None, + custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, **kwargs: Any, ) -> Question: """A list of items to select **one** option from. @@ -110,6 +112,18 @@ def select( show_selected: Display current selection choice at the bottom of list. + custom_key_binding: Dictionary of custom key bindings. The keys are either + strings or `prompt_toolkit.keys.Keys` objects and the values are + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event + + Example: + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + ``` + { + "c": lambda event: event.app.exit(result="custom"), + Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), + } Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). """ @@ -186,6 +200,10 @@ def get_prompt_tokens(): bindings = KeyBindings() + if custom_key_binding is not None: + for key, func in custom_key_binding.items(): + bindings.add(key, eager=True)(func) + @bindings.add(Keys.ControlQ, eager=True) @bindings.add(Keys.ControlC, eager=True) def _(event): diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index 03452055..417be505 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -2,12 +2,16 @@ from typing import List from typing import Optional from typing import Tuple +from typing import Dict +from typing import Callable from prompt_toolkit.document import Document from prompt_toolkit.lexers import Lexer from prompt_toolkit.lexers import SimpleLexer +from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.shortcuts.prompt import PromptSession from prompt_toolkit.styles import Style +from prompt_toolkit.keys import Keys from questionary.constants import DEFAULT_QUESTION_PREFIX from questionary.constants import INSTRUCTION_MULTILINE @@ -25,6 +29,7 @@ def text( multiline: bool = False, instruction: Optional[str] = None, lexer: Optional[Lexer] = None, + custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, **kwargs: Any, ) -> Question: """Prompt the user to enter a free text message. @@ -70,6 +75,20 @@ def text( lexer: Supply a valid lexer to style the answer. Leave empty to use a simple one by default. + custom_key_binding: Dictionary of custom key bindings. The keys are either + strings or `prompt_toolkit.keys.Keys` objects and the values are + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event + + Example: + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + ``` + { + "c": lambda event: event.app.exit(result="custom"), + Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), + } + ``` + kwargs: Additional arguments, they will be passed to prompt toolkit. Returns: @@ -88,8 +107,14 @@ def get_prompt_tokens() -> List[Tuple[str, str]]: result.append(("class:instruction", " {} ".format(instruction))) return result + bindings = KeyBindings() + if custom_key_binding is not None: + for key, func in custom_key_binding.items(): + bindings.add(key, eager=True)(func) + p: PromptSession = PromptSession( get_prompt_tokens, + key_bindings=bindings, style=merged_style, validator=validator, lexer=lexer, diff --git a/tests/prompts/test_checkbox.py b/tests/prompts/test_checkbox.py index 4b7c8404..1e59f38c 100644 --- a/tests/prompts/test_checkbox.py +++ b/tests/prompts/test_checkbox.py @@ -34,6 +34,20 @@ def test_select_with_instruction(): assert result == ["foo"] +def test_select_with_custom_key_bindings(): + message = "Foo message" + kwargs = { + "choices": ["foo", "bar", "bazz"], + "custom_key_binding": { + KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") + } + } + text = KeyInputs.ONE + "\r" + + result, cli = feed_cli_with_input("checkbox", message, text, **kwargs) + assert result == "1-pressed" + + def test_select_first_choice_with_token_title(): message = "Foo message" kwargs = { diff --git a/tests/prompts/test_confirm.py b/tests/prompts/test_confirm.py index 3737cc40..b4b4e647 100644 --- a/tests/prompts/test_confirm.py +++ b/tests/prompts/test_confirm.py @@ -91,3 +91,16 @@ def test_confirm_not_autoenter_backspace(): result, cli = feed_cli_with_input("confirm", message, text, auto_enter=False) assert result is True + + +def test_confirm_with_custom_key_bindings(): + message = "Foo message" + kwargs = { + "custom_key_binding": { + KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") + } + } + text = KeyInputs.ONE + "\r" + + result, cli = feed_cli_with_input("confirm", message, text, auto_enter=False, **kwargs) + assert result == "1-pressed" \ No newline at end of file diff --git a/tests/prompts/test_select.py b/tests/prompts/test_select.py index 1d3eaa57..58de5780 100644 --- a/tests/prompts/test_select.py +++ b/tests/prompts/test_select.py @@ -103,6 +103,19 @@ def test_select_with_instruction(): assert result == "foo" +def test_select_with_custom_key_bindings(): + message = "Foo message" + kwargs = { + "choices": ["foo", "bar", "bazz"], + "custom_key_binding": { + KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") + } + } + text = KeyInputs.ONE + "\r" + + result, cli = feed_cli_with_input("select", message, text, **kwargs) + assert result == "1-pressed" + def test_cycle_to_first_choice(): message = "Foo message" kwargs = {"choices": ["foo", "bar", "bazz"]} From ea73a31caaca9640db13a3728d1a89803f862f64 Mon Sep 17 00:00:00 2001 From: baturayo Date: Wed, 31 May 2023 12:07:01 +0200 Subject: [PATCH 04/24] add custom key binding unit test for text field --- tests/prompts/test_text.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/tests/prompts/test_text.py b/tests/prompts/test_text.py index efe3c850..44ea8724 100644 --- a/tests/prompts/test_text.py +++ b/tests/prompts/test_text.py @@ -4,7 +4,7 @@ from prompt_toolkit.validation import ValidationError from prompt_toolkit.validation import Validator -from tests.utils import feed_cli_with_input +from tests.utils import KeyInputs, feed_cli_with_input def test_legacy_name(): @@ -36,6 +36,18 @@ def test_text_validate(): assert result == "Doe" +def test_text_with_custom_key_bindings(): + message = "What is your name" + kwargs = { + "custom_key_binding": { + KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") + } + } + text = KeyInputs.ONE + "\r" + + result, cli = feed_cli_with_input("text", message, text, **kwargs) + assert result == "1-pressed" + def test_text_validate_with_class(): class SimpleValidator(Validator): def validate(self, document): From 5fb88e4c5e16ab4b2367b4716c7f083c76f618a6 Mon Sep 17 00:00:00 2001 From: baturayo Date: Wed, 31 May 2023 12:36:09 +0200 Subject: [PATCH 05/24] Lint isort and black --- questionary/prompts/checkbox.py | 2 +- questionary/prompts/confirm.py | 9 +++++---- questionary/prompts/text.py | 8 ++++---- tests/prompts/test_checkbox.py | 4 ++-- tests/prompts/test_confirm.py | 6 ++++-- tests/prompts/test_select.py | 5 +++-- tests/prompts/test_text.py | 4 +++- 7 files changed, 22 insertions(+), 16 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index c50b551b..044f7804 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -105,7 +105,7 @@ def checkbox( use_emacs_keys: Allow the user to select items from the list using `Ctrl+N` (down) and `Ctrl+P` (up) keys. - + instruction: A message describing how to navigate the menu. custom_key_binding: Dictionary of custom key bindings. The keys are either diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index e6865311..2eda6b22 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -1,4 +1,7 @@ -from typing import Any, Callable, Optional, Dict +from typing import Any +from typing import Callable +from typing import Dict +from typing import Optional from prompt_toolkit import PromptSession from prompt_toolkit.formatted_text import to_formatted_text @@ -71,7 +74,7 @@ def confirm( Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), } ``` - + Returns: :class:`Question`: Question instance, ready to be prompted (using `.ask()`). """ @@ -123,8 +126,6 @@ def key_y(event): if auto_enter: exit_with_result(event) - - @bindings.add(Keys.ControlH) def key_backspace(event): status["answer"] = None diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index 417be505..e751cf08 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -1,17 +1,17 @@ from typing import Any +from typing import Callable +from typing import Dict from typing import List from typing import Optional from typing import Tuple -from typing import Dict -from typing import Callable from prompt_toolkit.document import Document +from prompt_toolkit.key_binding import KeyBindings +from prompt_toolkit.keys import Keys from prompt_toolkit.lexers import Lexer from prompt_toolkit.lexers import SimpleLexer -from prompt_toolkit.key_binding import KeyBindings from prompt_toolkit.shortcuts.prompt import PromptSession from prompt_toolkit.styles import Style -from prompt_toolkit.keys import Keys from questionary.constants import DEFAULT_QUESTION_PREFIX from questionary.constants import INSTRUCTION_MULTILINE diff --git a/tests/prompts/test_checkbox.py b/tests/prompts/test_checkbox.py index 1e59f38c..f7c84c24 100644 --- a/tests/prompts/test_checkbox.py +++ b/tests/prompts/test_checkbox.py @@ -37,10 +37,10 @@ def test_select_with_instruction(): def test_select_with_custom_key_bindings(): message = "Foo message" kwargs = { - "choices": ["foo", "bar", "bazz"], + "choices": ["foo", "bar", "bazz"], "custom_key_binding": { KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") - } + }, } text = KeyInputs.ONE + "\r" diff --git a/tests/prompts/test_confirm.py b/tests/prompts/test_confirm.py index b4b4e647..b6287b76 100644 --- a/tests/prompts/test_confirm.py +++ b/tests/prompts/test_confirm.py @@ -102,5 +102,7 @@ def test_confirm_with_custom_key_bindings(): } text = KeyInputs.ONE + "\r" - result, cli = feed_cli_with_input("confirm", message, text, auto_enter=False, **kwargs) - assert result == "1-pressed" \ No newline at end of file + result, cli = feed_cli_with_input( + "confirm", message, text, auto_enter=False, **kwargs + ) + assert result == "1-pressed" diff --git a/tests/prompts/test_select.py b/tests/prompts/test_select.py index 58de5780..fc460434 100644 --- a/tests/prompts/test_select.py +++ b/tests/prompts/test_select.py @@ -106,16 +106,17 @@ def test_select_with_instruction(): def test_select_with_custom_key_bindings(): message = "Foo message" kwargs = { - "choices": ["foo", "bar", "bazz"], + "choices": ["foo", "bar", "bazz"], "custom_key_binding": { KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") - } + }, } text = KeyInputs.ONE + "\r" result, cli = feed_cli_with_input("select", message, text, **kwargs) assert result == "1-pressed" + def test_cycle_to_first_choice(): message = "Foo message" kwargs = {"choices": ["foo", "bar", "bazz"]} diff --git a/tests/prompts/test_text.py b/tests/prompts/test_text.py index 44ea8724..677ee4b6 100644 --- a/tests/prompts/test_text.py +++ b/tests/prompts/test_text.py @@ -4,7 +4,8 @@ from prompt_toolkit.validation import ValidationError from prompt_toolkit.validation import Validator -from tests.utils import KeyInputs, feed_cli_with_input +from tests.utils import KeyInputs +from tests.utils import feed_cli_with_input def test_legacy_name(): @@ -48,6 +49,7 @@ def test_text_with_custom_key_bindings(): result, cli = feed_cli_with_input("text", message, text, **kwargs) assert result == "1-pressed" + def test_text_validate_with_class(): class SimpleValidator(Validator): def validate(self, document): From 8752619d3a39577b68b032fb55ddeeaf62e7c75b Mon Sep 17 00:00:00 2001 From: baturayo Date: Wed, 31 May 2023 13:15:00 +0200 Subject: [PATCH 06/24] add union type to make it compatible with older python versions --- questionary/prompts/checkbox.py | 2 +- questionary/prompts/confirm.py | 3 ++- questionary/prompts/select.py | 2 +- questionary/prompts/text.py | 3 ++- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 044f7804..51d27dff 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -38,7 +38,7 @@ def checkbox( use_jk_keys: bool = True, use_emacs_keys: bool = True, instruction: Optional[str] = None, - custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, + custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, **kwargs: Any, ) -> Question: """Ask the user to select from a list of items. diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index 2eda6b22..fbfb3ba9 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -2,6 +2,7 @@ from typing import Callable from typing import Dict from typing import Optional +from typing import Union from prompt_toolkit import PromptSession from prompt_toolkit.formatted_text import to_formatted_text @@ -24,7 +25,7 @@ def confirm( qmark: str = DEFAULT_QUESTION_PREFIX, style: Optional[Style] = None, auto_enter: bool = True, - custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, + custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, **kwargs: Any, ) -> Question: """A yes or no question. The user can either confirm or deny. diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index 6c918ade..b685653b 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -37,7 +37,7 @@ def select( use_emacs_keys: bool = True, show_selected: bool = False, instruction: Optional[str] = None, - custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, + custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, **kwargs: Any, ) -> Question: """A list of items to select **one** option from. diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index e751cf08..f7796e4b 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -4,6 +4,7 @@ from typing import List from typing import Optional from typing import Tuple +from typing import Union from prompt_toolkit.document import Document from prompt_toolkit.key_binding import KeyBindings @@ -29,7 +30,7 @@ def text( multiline: bool = False, instruction: Optional[str] = None, lexer: Optional[Lexer] = None, - custom_key_binding: Optional[Dict[str | Keys, Callable]] = None, + custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, **kwargs: Any, ) -> Question: """Prompt the user to enter a free text message. From 2053ee4cd3f4f594451e006561d91318d689a27a Mon Sep 17 00:00:00 2001 From: baturayo Date: Wed, 31 May 2023 13:45:12 +0200 Subject: [PATCH 07/24] fix docstrings --- questionary/prompts/checkbox.py | 18 +++++++----------- questionary/prompts/confirm.py | 19 +++++++------------ questionary/prompts/select.py | 18 +++++++----------- questionary/prompts/text.py | 19 +++++++------------ 4 files changed, 28 insertions(+), 46 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 51d27dff..94fa4218 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -110,17 +110,13 @@ def checkbox( custom_key_binding: Dictionary of custom key bindings. The keys are either strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event - - Example: - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - ``` - { - "c": lambda event: event.app.exit(result="custom"), - Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), - } - ``` + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + `{"c": lambda event: event.app.exit(result="custom")}` or + `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` + respectively. + Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). """ diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index fbfb3ba9..8a537b51 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -63,18 +63,13 @@ def confirm( accepted without the need to press 'Enter'. custom_key_binding: Dictionary of custom key bindings. The keys are either - strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event - - Example: - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - ``` - { - "c": lambda event: event.app.exit(result="custom"), - Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), - } - ``` + strings or `prompt_toolkit.keys.Keys` objects and the values are + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + `{"c": lambda event: event.app.exit(result="custom")}` or + `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` + respectively. Returns: :class:`Question`: Question instance, ready to be prompted (using `.ask()`). diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index b685653b..617c1a0d 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -113,17 +113,13 @@ def select( show_selected: Display current selection choice at the bottom of list. custom_key_binding: Dictionary of custom key bindings. The keys are either - strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event - - Example: - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - ``` - { - "c": lambda event: event.app.exit(result="custom"), - Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), - } + strings or `prompt_toolkit.keys.Keys` objects and the values are + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + `{"c": lambda event: event.app.exit(result="custom")}` or + `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` + respectively. Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). """ diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index f7796e4b..26143e9c 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -77,18 +77,13 @@ def text( use a simple one by default. custom_key_binding: Dictionary of custom key bindings. The keys are either - strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event - - Example: - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - ``` - { - "c": lambda event: event.app.exit(result="custom"), - Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q"), - } - ``` + strings or `prompt_toolkit.keys.Keys` objects and the values are + callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. + The following will exit the application with the result "custom" when the + user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" + `{"c": lambda event: event.app.exit(result="custom")}` or + `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` + respectively. kwargs: Additional arguments, they will be passed to prompt toolkit. From 1af69a58267876888ae1c38cd7549f0854c950ea Mon Sep 17 00:00:00 2001 From: baturayo Date: Wed, 31 May 2023 14:03:49 +0200 Subject: [PATCH 08/24] update documentations --- questionary/prompts/checkbox.py | 22 ++++++++++++++-------- questionary/prompts/confirm.py | 22 ++++++++++++++-------- questionary/prompts/select.py | 23 +++++++++++++++-------- questionary/prompts/text.py | 22 ++++++++++++++-------- 4 files changed, 57 insertions(+), 32 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 94fa4218..19689c61 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -108,14 +108,20 @@ def checkbox( instruction: A message describing how to navigate the menu. - custom_key_binding: Dictionary of custom key bindings. The keys are either - strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - `{"c": lambda event: event.app.exit(result="custom")}` or - `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` - respectively. + custom_key_binding: A dictionary specifying custom key bindings for the prompt. + The dictionary should have key-value pairs where the key represents + the key combination or key code, and the value is a callable + that will be executed when the key is pressed. The callable should + take an `event` object as its argument, which provides + information about the key event. + + Example usages: + + - Exit with result "custom" when the user presses "c": + ``{"c": lambda event: event.app.exit(result="custom")}`` + + - Exit with result "ctrl-q" when the user presses "ctrl-q": + ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index 8a537b51..501b65b3 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -62,14 +62,20 @@ def confirm( accept their answer. If set to `True`, a valid input will be accepted without the need to press 'Enter'. - custom_key_binding: Dictionary of custom key bindings. The keys are either - strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - `{"c": lambda event: event.app.exit(result="custom")}` or - `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` - respectively. + custom_key_binding: A dictionary specifying custom key bindings for the prompt. + The dictionary should have key-value pairs where the key represents + the key combination or key code, and the value is a callable + that will be executed when the key is pressed. The callable should + take an `event` object as its argument, which provides + information about the key event. + + Example usages: + + - Exit with result "custom" when the user presses "c": + ``{"c": lambda event: event.app.exit(result="custom")}`` + + - Exit with result "ctrl-q" when the user presses "ctrl-q": + ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` Returns: :class:`Question`: Question instance, ready to be prompted (using `.ask()`). diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index 617c1a0d..eeefab04 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -112,14 +112,21 @@ def select( show_selected: Display current selection choice at the bottom of list. - custom_key_binding: Dictionary of custom key bindings. The keys are either - strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - `{"c": lambda event: event.app.exit(result="custom")}` or - `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` - respectively. + custom_key_binding: A dictionary specifying custom key bindings for the prompt. + The dictionary should have key-value pairs where the key represents + the key combination or key code, and the value is a callable + that will be executed when the key is pressed. The callable should + take an `event` object as its argument, which provides + information about the key event. + + Example usages: + + - Exit with result "custom" when the user presses "c": + ``{"c": lambda event: event.app.exit(result="custom")}`` + + - Exit with result "ctrl-q" when the user presses "ctrl-q": + ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` + Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). """ diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index 26143e9c..d88a7461 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -76,14 +76,20 @@ def text( lexer: Supply a valid lexer to style the answer. Leave empty to use a simple one by default. - custom_key_binding: Dictionary of custom key bindings. The keys are either - strings or `prompt_toolkit.keys.Keys` objects and the values are - callables that accept a `prompt_toolkit.key_binding.KeyBinding` event. - The following will exit the application with the result "custom" when the - user presses "c" and with the result "ctrl-q" when the user presses "ctrl-q" - `{"c": lambda event: event.app.exit(result="custom")}` or - `{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}` - respectively. + custom_key_binding: A dictionary specifying custom key bindings for the prompt. + The dictionary should have key-value pairs where the key represents + the key combination or key code, and the value is a callable + that will be executed when the key is pressed. The callable should + take an `event` object as its argument, which provides + information about the key event. + + Example usages: + + - Exit with result "custom" when the user presses "c": + ``{"c": lambda event: event.app.exit(result="custom")}`` + + - Exit with result "ctrl-q" when the user presses "ctrl-q": + ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` kwargs: Additional arguments, they will be passed to prompt toolkit. From cd3fabf74ec78d51bed292ecff40c0857af6f270 Mon Sep 17 00:00:00 2001 From: baturayo Date: Thu, 1 Jun 2023 10:18:07 +0200 Subject: [PATCH 09/24] Add customized instruction support to confirm --- questionary/prompts/confirm.py | 10 +++++++--- tests/prompts/test_confirm.py | 10 ++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index 6d5c415c..eed98179 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -22,6 +22,7 @@ def confirm( qmark: str = DEFAULT_QUESTION_PREFIX, style: Optional[Style] = None, auto_enter: bool = True, + instruction: Optional[str] = None, **kwargs: Any, ) -> Question: """A yes or no question. The user can either confirm or deny. @@ -58,6 +59,7 @@ def confirm( accept their answer. If set to `True`, a valid input will be accepted without the need to press 'Enter'. + instruction: A message describing how to navigate the menu. Returns: :class:`Question`: Question instance, ready to be prompted (using `.ask()`). """ @@ -71,9 +73,11 @@ def get_prompt_tokens(): tokens.append(("class:qmark", qmark)) tokens.append(("class:question", " {} ".format(message))) - if not status["complete"]: - instruction = YES_OR_NO if default else NO_OR_YES - tokens.append(("class:instruction", "{} ".format(instruction))) + if instruction is not None: + tokens.append(("class:instruction", instruction)) + elif not status["complete"]: + _instruction = YES_OR_NO if default else NO_OR_YES + tokens.append(("class:instruction", "{} ".format(_instruction))) if status["answer"] is not None: answer = YES if status["answer"] else NO diff --git a/tests/prompts/test_confirm.py b/tests/prompts/test_confirm.py index 3737cc40..7345c431 100644 --- a/tests/prompts/test_confirm.py +++ b/tests/prompts/test_confirm.py @@ -91,3 +91,13 @@ def test_confirm_not_autoenter_backspace(): result, cli = feed_cli_with_input("confirm", message, text, auto_enter=False) assert result is True + + +def test_confirm_instruction(): + message = "Foo message" + text = "Y" + "\r" + + result, cli = feed_cli_with_input( + "confirm", message, text, instruction="Foo instruction" + ) + assert result is True From 5690c80daf6dca600eb3267694b36e239c11333a Mon Sep 17 00:00:00 2001 From: baturayo Date: Thu, 1 Jun 2023 11:43:57 +0200 Subject: [PATCH 10/24] fix local variable instruction --- questionary/prompts/confirm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index bfbab390..9939fba8 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -93,7 +93,7 @@ def get_prompt_tokens(): tokens.append(("class:question", " {} ".format(message))) if instruction is not None: - tokens.append(("class:instruction", instruction)) + tokens.append(("class:instruction", "{} ".format(instruction))) elif not status["complete"]: _instruction = YES_OR_NO if default else NO_OR_YES tokens.append(("class:instruction", "{} ".format(_instruction))) From bde4f364288e607057cb7628aa5a9074286c754a Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:46:23 +0300 Subject: [PATCH 11/24] Update questionary/prompts/checkbox.py custom_key_bindings doc Co-authored-by: Kian Cross --- questionary/prompts/checkbox.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 19689c61..897f0ed9 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -108,21 +108,25 @@ def checkbox( instruction: A message describing how to navigate the menu. - custom_key_binding: A dictionary specifying custom key bindings for the prompt. - The dictionary should have key-value pairs where the key represents - the key combination or key code, and the value is a callable - that will be executed when the key is pressed. The callable should - take an `event` object as its argument, which provides - information about the key event. + custom_key_bindings: A dictionary specifying custom key bindings for the + prompt. The dictionary should have key-value pairs, + where the key represents the key combination or key + code, and the value is a callable that will be + executed when the key is pressed. The callable + should take an ``event`` object as its argument, + which will provide information about the key event. - Example usages: + Examples: - - Exit with result "custom" when the user presses "c": - ``{"c": lambda event: event.app.exit(result="custom")}`` + - Exit with a result of ``custom`` when the user + presses :kbd:`c`:: - - Exit with result "ctrl-q" when the user presses "ctrl-q": - ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` + {"c": lambda event: event.app.exit(result="custom")} + - Exit with a result of ``ctrl-q`` when the user + presses :kbd:`Ctrl` + :kbd:`q`:: + + {Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")} Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). """ From 2d5dc8a602e4a350f8e261c95480d2a5731d93fa Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:46:53 +0300 Subject: [PATCH 12/24] Update questionary/prompts/checkbox.py variable name change Co-authored-by: Kian Cross --- questionary/prompts/checkbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 897f0ed9..73d3abe7 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -38,7 +38,7 @@ def checkbox( use_jk_keys: bool = True, use_emacs_keys: bool = True, instruction: Optional[str] = None, - custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, + custom_key_bindings: Optional[Dict[Union[str, Keys], Callable]] = None, **kwargs: Any, ) -> Question: """Ask the user to select from a list of items. From fc4c12a09dfd5f0f2ab48570514f85d343c644cb Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:49:06 +0300 Subject: [PATCH 13/24] Update questionary/prompts/confirm.py update docs Co-authored-by: Kian Cross --- questionary/prompts/confirm.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index 9939fba8..c2f06418 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -63,20 +63,25 @@ def confirm( accept their answer. If set to `True`, a valid input will be accepted without the need to press 'Enter'. - custom_key_binding: A dictionary specifying custom key bindings for the prompt. - The dictionary should have key-value pairs where the key represents - the key combination or key code, and the value is a callable - that will be executed when the key is pressed. The callable should - take an `event` object as its argument, which provides - information about the key event. + custom_key_bindings: A dictionary specifying custom key bindings for the + prompt. The dictionary should have key-value pairs, + where the key represents the key combination or key + code, and the value is a callable that will be + executed when the key is pressed. The callable + should take an ``event`` object as its argument, + which will provide information about the key event. - Example usages: + Examples: - - Exit with result "custom" when the user presses "c": - ``{"c": lambda event: event.app.exit(result="custom")}`` + - Exit with a result of ``custom`` when the user + presses :kbd:`c`:: - - Exit with result "ctrl-q" when the user presses "ctrl-q": - ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` + {"c": lambda event: event.app.exit(result="custom")} + + - Exit with a result of ``ctrl-q`` when the user + presses :kbd:`Ctrl` + :kbd:`q`:: + + {Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")} instruction: A message describing how to navigate the menu. Returns: From affe3b41200caf405f81a9610ab1da86904123c9 Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:49:33 +0300 Subject: [PATCH 14/24] Update questionary/prompts/confirm.py variable change Co-authored-by: Kian Cross --- questionary/prompts/confirm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index c2f06418..3f9e4d33 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -25,7 +25,7 @@ def confirm( qmark: str = DEFAULT_QUESTION_PREFIX, style: Optional[Style] = None, auto_enter: bool = True, - custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, + custom_key_bindings: Optional[Dict[Union[str, Keys], Callable]] = None, instruction: Optional[str] = None, **kwargs: Any, ) -> Question: From 97f7955609d811f25ad9400d21921e856bbc9e98 Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:49:53 +0300 Subject: [PATCH 15/24] Update questionary/prompts/confirm.py variable change Co-authored-by: Kian Cross --- questionary/prompts/confirm.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/questionary/prompts/confirm.py b/questionary/prompts/confirm.py index 3f9e4d33..f71652ef 100644 --- a/questionary/prompts/confirm.py +++ b/questionary/prompts/confirm.py @@ -114,8 +114,8 @@ def exit_with_result(event): event.app.exit(result=status["answer"]) bindings = KeyBindings() - if custom_key_binding is not None: - for key, func in custom_key_binding.items(): + if custom_key_bindings is not None: + for key, func in custom_key_bindings.items(): bindings.add(key, eager=True)(func) @bindings.add(Keys.ControlQ, eager=True) From 3fe67194f78ac334017c76264498de91050684cb Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:50:16 +0300 Subject: [PATCH 16/24] Update questionary/prompts/text.py variable name change Co-authored-by: Kian Cross --- questionary/prompts/text.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index d88a7461..a9ae1da9 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -110,8 +110,8 @@ def get_prompt_tokens() -> List[Tuple[str, str]]: return result bindings = KeyBindings() - if custom_key_binding is not None: - for key, func in custom_key_binding.items(): + if custom_key_bindings is not None: + for key, func in custom_key_bindings.items(): bindings.add(key, eager=True)(func) p: PromptSession = PromptSession( From 80fa573636d782b01fddea81506ec65029740df7 Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:50:28 +0300 Subject: [PATCH 17/24] Update tests/prompts/test_checkbox.py variable name change Co-authored-by: Kian Cross --- tests/prompts/test_checkbox.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/prompts/test_checkbox.py b/tests/prompts/test_checkbox.py index f7c84c24..e88d0dbc 100644 --- a/tests/prompts/test_checkbox.py +++ b/tests/prompts/test_checkbox.py @@ -38,7 +38,7 @@ def test_select_with_custom_key_bindings(): message = "Foo message" kwargs = { "choices": ["foo", "bar", "bazz"], - "custom_key_binding": { + "custom_key_bindings": { KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") }, } From 0696d2bb5b0d0250d158213a8101de1eb325e1ce Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:50:36 +0300 Subject: [PATCH 18/24] Update tests/prompts/test_confirm.py variable name change Co-authored-by: Kian Cross --- tests/prompts/test_confirm.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/prompts/test_confirm.py b/tests/prompts/test_confirm.py index 03847d11..11c78be7 100644 --- a/tests/prompts/test_confirm.py +++ b/tests/prompts/test_confirm.py @@ -96,7 +96,7 @@ def test_confirm_not_autoenter_backspace(): def test_confirm_with_custom_key_bindings(): message = "Foo message" kwargs = { - "custom_key_binding": { + "custom_key_bindings": { KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") } } From 456738ca5e4d8ecdfff414c5caa5d047176989e3 Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:50:48 +0300 Subject: [PATCH 19/24] Update tests/prompts/test_select.py variable name change Co-authored-by: Kian Cross --- tests/prompts/test_select.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/prompts/test_select.py b/tests/prompts/test_select.py index fc460434..4a02a360 100644 --- a/tests/prompts/test_select.py +++ b/tests/prompts/test_select.py @@ -107,7 +107,7 @@ def test_select_with_custom_key_bindings(): message = "Foo message" kwargs = { "choices": ["foo", "bar", "bazz"], - "custom_key_binding": { + "custom_key_bindings": { KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") }, } From de1b9c898e96766c207fb82c2f5e5515c7df1f2c Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 15:51:04 +0300 Subject: [PATCH 20/24] Update tests/prompts/test_text.py variable name change Co-authored-by: Kian Cross --- tests/prompts/test_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/prompts/test_text.py b/tests/prompts/test_text.py index 677ee4b6..54c18e51 100644 --- a/tests/prompts/test_text.py +++ b/tests/prompts/test_text.py @@ -40,7 +40,7 @@ def test_text_validate(): def test_text_with_custom_key_bindings(): message = "What is your name" kwargs = { - "custom_key_binding": { + "custom_key_bindings": { KeyInputs.ONE: lambda event: event.app.exit(result="1-pressed") } } From bcb4bebb29ea0a6437f62333bc0ddd4279f1dfec Mon Sep 17 00:00:00 2001 From: baturayo Date: Fri, 30 Jun 2023 16:12:42 +0300 Subject: [PATCH 21/24] change custom_key_binding to custom_key_bindings --- questionary/prompts/checkbox.py | 4 ++-- questionary/prompts/select.py | 8 ++++---- questionary/prompts/text.py | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 73d3abe7..f7d89a5b 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -223,8 +223,8 @@ def perform_validation(selected_values: List[str]) -> bool: layout = common.create_inquirer_layout(ic, get_prompt_tokens, **kwargs) bindings = KeyBindings() - if custom_key_binding is not None: - for key, func in custom_key_binding.items(): + if custom_key_bindings is not None: + for key, func in custom_key_bindings.items(): bindings.add(key, eager=True)(func) @bindings.add(Keys.ControlQ, eager=True) diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index eeefab04..66efc0db 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -37,7 +37,7 @@ def select( use_emacs_keys: bool = True, show_selected: bool = False, instruction: Optional[str] = None, - custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, + custom_key_bindings: Optional[Dict[Union[str, Keys], Callable]] = None, **kwargs: Any, ) -> Question: """A list of items to select **one** option from. @@ -112,7 +112,7 @@ def select( show_selected: Display current selection choice at the bottom of list. - custom_key_binding: A dictionary specifying custom key bindings for the prompt. + custom_key_bindings: A dictionary specifying custom key bindings for the prompt. The dictionary should have key-value pairs where the key represents the key combination or key code, and the value is a callable that will be executed when the key is pressed. The callable should @@ -203,8 +203,8 @@ def get_prompt_tokens(): bindings = KeyBindings() - if custom_key_binding is not None: - for key, func in custom_key_binding.items(): + if custom_key_bindings is not None: + for key, func in custom_key_bindings.items(): bindings.add(key, eager=True)(func) @bindings.add(Keys.ControlQ, eager=True) diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index a9ae1da9..e52ef788 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -30,7 +30,7 @@ def text( multiline: bool = False, instruction: Optional[str] = None, lexer: Optional[Lexer] = None, - custom_key_binding: Optional[Dict[Union[str, Keys], Callable]] = None, + custom_key_bindings: Optional[Dict[Union[str, Keys], Callable]] = None, **kwargs: Any, ) -> Question: """Prompt the user to enter a free text message. @@ -76,7 +76,7 @@ def text( lexer: Supply a valid lexer to style the answer. Leave empty to use a simple one by default. - custom_key_binding: A dictionary specifying custom key bindings for the prompt. + custom_key_bindings: A dictionary specifying custom key bindings for the prompt. The dictionary should have key-value pairs where the key represents the key combination or key code, and the value is a callable that will be executed when the key is pressed. The callable should From 218742eb573c5b605c78f2e0575ebf8ef0bc7a53 Mon Sep 17 00:00:00 2001 From: Baturay Ofluoglu Date: Fri, 30 Jun 2023 16:22:38 +0300 Subject: [PATCH 22/24] Update questionary/prompts/checkbox.py Co-authored-by: Kian Cross From 7c515a54000692215e5d95b372886916b732a032 Mon Sep 17 00:00:00 2001 From: baturayo Date: Fri, 30 Jun 2023 16:27:38 +0300 Subject: [PATCH 23/24] update custom_key_bindings documentations --- questionary/prompts/select.py | 28 ++++++++++++++-------------- questionary/prompts/text.py | 28 ++++++++++++++-------------- 2 files changed, 28 insertions(+), 28 deletions(-) diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index 66efc0db..ca7fce03 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -112,20 +112,20 @@ def select( show_selected: Display current selection choice at the bottom of list. - custom_key_bindings: A dictionary specifying custom key bindings for the prompt. - The dictionary should have key-value pairs where the key represents - the key combination or key code, and the value is a callable - that will be executed when the key is pressed. The callable should - take an `event` object as its argument, which provides - information about the key event. - - Example usages: - - - Exit with result "custom" when the user presses "c": - ``{"c": lambda event: event.app.exit(result="custom")}`` - - - Exit with result "ctrl-q" when the user presses "ctrl-q": - ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` + custom_key_bindings: A dictionary specifying custom key bindings for the + prompt. The dictionary should have key-value pairs, + where the key represents the key combination or key + code, and the value is a callable that will be + executed when the key is pressed. The callable + should take an ``event`` object as its argument, + which will provide information about the key event. + Examples: + - Exit with a result of ``custom`` when the user + presses :kbd:`c`:: + {"c": lambda event: event.app.exit(result="custom")} + - Exit with a result of ``ctrl-q`` when the user + presses :kbd:`Ctrl` + :kbd:`q`:: + {Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")} Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index e52ef788..61868f45 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -76,20 +76,20 @@ def text( lexer: Supply a valid lexer to style the answer. Leave empty to use a simple one by default. - custom_key_bindings: A dictionary specifying custom key bindings for the prompt. - The dictionary should have key-value pairs where the key represents - the key combination or key code, and the value is a callable - that will be executed when the key is pressed. The callable should - take an `event` object as its argument, which provides - information about the key event. - - Example usages: - - - Exit with result "custom" when the user presses "c": - ``{"c": lambda event: event.app.exit(result="custom")}`` - - - Exit with result "ctrl-q" when the user presses "ctrl-q": - ``{Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")}`` + custom_key_bindings: A dictionary specifying custom key bindings for the + prompt. The dictionary should have key-value pairs, + where the key represents the key combination or key + code, and the value is a callable that will be + executed when the key is pressed. The callable + should take an ``event`` object as its argument, + which will provide information about the key event. + Examples: + - Exit with a result of ``custom`` when the user + presses :kbd:`c`:: + {"c": lambda event: event.app.exit(result="custom")} + - Exit with a result of ``ctrl-q`` when the user + presses :kbd:`Ctrl` + :kbd:`q`:: + {Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")} kwargs: Additional arguments, they will be passed to prompt toolkit. From abdf99a5f6d49d6ac2b64bb29d47bdc0502898ba Mon Sep 17 00:00:00 2001 From: baturayo Date: Fri, 30 Jun 2023 16:35:32 +0300 Subject: [PATCH 24/24] fix docs for select and text --- questionary/prompts/select.py | 5 +++++ questionary/prompts/text.py | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index ca7fce03..791d5a6d 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -119,12 +119,17 @@ def select( executed when the key is pressed. The callable should take an ``event`` object as its argument, which will provide information about the key event. + Examples: + - Exit with a result of ``custom`` when the user presses :kbd:`c`:: + {"c": lambda event: event.app.exit(result="custom")} + - Exit with a result of ``ctrl-q`` when the user presses :kbd:`Ctrl` + :kbd:`q`:: + {Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")} Returns: diff --git a/questionary/prompts/text.py b/questionary/prompts/text.py index 61868f45..f3d47634 100644 --- a/questionary/prompts/text.py +++ b/questionary/prompts/text.py @@ -83,12 +83,17 @@ def text( executed when the key is pressed. The callable should take an ``event`` object as its argument, which will provide information about the key event. + Examples: + - Exit with a result of ``custom`` when the user presses :kbd:`c`:: + {"c": lambda event: event.app.exit(result="custom")} + - Exit with a result of ``ctrl-q`` when the user presses :kbd:`Ctrl` + :kbd:`q`:: + {Keys.ControlQ: lambda event: event.app.exit(result="ctrl-q")} kwargs: Additional arguments, they will be passed to prompt toolkit.