From 41901f996e09234ffba19f3aa2305a17f09c9fd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Deloumeau-Prigent?= Date: Wed, 23 Dec 2020 11:39:05 +0100 Subject: [PATCH 1/4] add pointer option fix lint --- questionary/constants.py | 2 +- questionary/prompts/common.py | 6 ++++-- questionary/prompts/rawselect.py | 4 +++- questionary/prompts/select.py | 8 +++++++- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/questionary/constants.py b/questionary/constants.py index 7188ada9..91fa4af9 100644 --- a/questionary/constants.py +++ b/questionary/constants.py @@ -16,7 +16,7 @@ INSTRUCTION_MULTILINE = "(Finish with 'Alt+Enter' or 'Esc, Enter')\n>" # Selection token used to indicate the selection cursor in a list -SELECTED_POINTER = "»" +DEFAULT_SELECTED_POINTER = "»" # Item prefix to identify selected items in a checkbox list INDICATOR_SELECTED = "●" diff --git a/questionary/prompts/common.py b/questionary/prompts/common.py index c6502547..77464e31 100644 --- a/questionary/prompts/common.py +++ b/questionary/prompts/common.py @@ -14,7 +14,7 @@ from questionary.constants import ( DEFAULT_STYLE, - SELECTED_POINTER, + DEFAULT_SELECTED_POINTER, INDICATOR_SELECTED, INDICATOR_UNSELECTED, INVALID_INPUT, @@ -188,6 +188,7 @@ def __init__( self, choices: Sequence[Union[str, Choice, Dict[str, Any]]], default: Optional[Union[str, Choice, Dict[str, Any]]] = None, + pointer: str = DEFAULT_SELECTED_POINTER, use_indicator: bool = True, use_shortcuts: bool = False, use_arrow_keys: bool = True, @@ -201,6 +202,7 @@ def __init__( self.use_arrow_keys = use_arrow_keys self.use_pointer = use_pointer self.default = default + self.pointer = pointer if default is not None and default not in choices: raise ValueError( @@ -305,7 +307,7 @@ def append(index: int, choice: Choice): if index == self.pointed_at: if self.use_pointer: - tokens.append(("class:pointer", " {} ".format(SELECTED_POINTER))) + tokens.append(("class:pointer", " {} ".format(self.pointer))) else: tokens.append(("class:text", " ")) diff --git a/questionary/prompts/rawselect.py b/questionary/prompts/rawselect.py index 22a9f5af..0e63e5d9 100644 --- a/questionary/prompts/rawselect.py +++ b/questionary/prompts/rawselect.py @@ -2,7 +2,7 @@ from prompt_toolkit.styles import Style -from questionary.constants import DEFAULT_QUESTION_PREFIX +from questionary.constants import DEFAULT_QUESTION_PREFIX, DEFAULT_SELECTED_POINTER from questionary.prompts import select from questionary.prompts.common import Choice from questionary.question import Question @@ -13,6 +13,7 @@ def rawselect( choices: Sequence[Union[str, Choice, Dict[str, Any]]], default: Optional[str] = None, qmark: str = DEFAULT_QUESTION_PREFIX, + pointer: str = DEFAULT_SELECTED_POINTER, style: Optional[Style] = None, **kwargs: Any, ) -> Question: @@ -61,6 +62,7 @@ def rawselect( choices, default, qmark, + pointer, style, use_shortcuts=True, use_arrow_keys=False, diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index eafc9910..0f621550 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -8,7 +8,11 @@ from prompt_toolkit.styles import Style, merge_styles from questionary import utils -from questionary.constants import DEFAULT_QUESTION_PREFIX, DEFAULT_STYLE +from questionary.constants import ( + DEFAULT_QUESTION_PREFIX, + DEFAULT_SELECTED_POINTER, + DEFAULT_STYLE, +) from questionary.prompts import common from questionary.prompts.common import Choice, InquirerControl, Separator from questionary.question import Question @@ -19,6 +23,7 @@ def select( choices: Sequence[Union[str, Choice, Dict[str, Any]]], default: Optional[Union[str, Choice, Dict[str, Any]]] = None, qmark: str = DEFAULT_QUESTION_PREFIX, + pointer: str = DEFAULT_SELECTED_POINTER, style: Optional[Style] = None, use_shortcuts: bool = False, use_arrow_keys: bool = True, @@ -105,6 +110,7 @@ def select( ic = InquirerControl( choices, default, + pointer=pointer, use_indicator=use_indicator, use_shortcuts=use_shortcuts, use_arrow_keys=use_arrow_keys, From 8c2392cf2e7ca5200af1776c8302f264e0953970 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie=20Deloumeau-Prigent?= Date: Wed, 23 Dec 2020 15:53:18 +0100 Subject: [PATCH 2/4] remove use_pointerand add docstring --- questionary/prompts/checkbox.py | 18 ++++++++++++------ questionary/prompts/common.py | 6 ++---- questionary/prompts/rawselect.py | 4 ++++ questionary/prompts/select.py | 9 ++++----- 4 files changed, 22 insertions(+), 15 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 596d5e5b..95faadec 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -7,7 +7,12 @@ from prompt_toolkit.formatted_text import FormattedText from questionary import utils -from questionary.constants import DEFAULT_QUESTION_PREFIX, DEFAULT_STYLE, INVALID_INPUT +from questionary.constants import ( + DEFAULT_QUESTION_PREFIX, + DEFAULT_SELECTED_POINTER, + DEFAULT_STYLE, + INVALID_INPUT, +) from questionary.prompts import common from questionary.prompts.common import Choice, InquirerControl, Separator from questionary.question import Question @@ -19,8 +24,8 @@ def checkbox( default: Optional[str] = None, validate: Callable[[List[str]], Union[bool, str]] = lambda a: True, qmark: str = DEFAULT_QUESTION_PREFIX, + pointer: str = DEFAULT_SELECTED_POINTER, style: Optional[Style] = None, - use_pointer: bool = True, initial_choice: Optional[Union[str, Choice, Dict[str, Any]]] = None, **kwargs: Any, ) -> Question: @@ -70,12 +75,13 @@ def checkbox( qmark: Question prefix displayed in front of the question. By default this is a ``?``. + pointer: Pointer symbol in front of the currently highlighted element. + By default this is a ``»``. + Use ``None`` to disable it. + style: A custom color and style for the question parts. You can configure colors as well as font types for different elements. - use_pointer: Flag to enable the pointer in front of the currently - highlighted element. - initial_choice: A value corresponding to a selectable item in the choices, to initially set the pointer position to. @@ -98,7 +104,7 @@ def checkbox( raise ValueError("validate must be callable") ic = InquirerControl( - choices, default, use_pointer=use_pointer, initial_choice=initial_choice + choices, default, pointer=pointer, initial_choice=initial_choice ) def get_prompt_tokens() -> List[Tuple[str, str]]: diff --git a/questionary/prompts/common.py b/questionary/prompts/common.py index 77464e31..f111e5eb 100644 --- a/questionary/prompts/common.py +++ b/questionary/prompts/common.py @@ -180,7 +180,7 @@ class InquirerControl(FormattedTextControl): use_indicator: bool use_shortcuts: bool use_arrow_keys: bool - use_pointer: bool + pointer: str pointed_at: int is_answered: bool @@ -192,7 +192,6 @@ def __init__( use_indicator: bool = True, use_shortcuts: bool = False, use_arrow_keys: bool = True, - use_pointer: bool = True, initial_choice: Optional[Union[str, Choice, Dict[str, Any]]] = None, **kwargs: Any, ): @@ -200,7 +199,6 @@ def __init__( self.use_indicator = use_indicator self.use_shortcuts = use_shortcuts self.use_arrow_keys = use_arrow_keys - self.use_pointer = use_pointer self.default = default self.pointer = pointer @@ -306,7 +304,7 @@ def append(index: int, choice: Choice): selected = choice.value in self.selected_options if index == self.pointed_at: - if self.use_pointer: + if self.pointer is not None: tokens.append(("class:pointer", " {} ".format(self.pointer))) else: tokens.append(("class:text", " ")) diff --git a/questionary/prompts/rawselect.py b/questionary/prompts/rawselect.py index 0e63e5d9..66beee67 100644 --- a/questionary/prompts/rawselect.py +++ b/questionary/prompts/rawselect.py @@ -51,6 +51,10 @@ def rawselect( qmark: Question prefix displayed in front of the question. By default this is a ``?``. + pointer: Pointer symbol in front of the currently highlighted element. + By default this is a ``»``. + Use ``None`` to disable it. + style: A custom color and style for the question parts. You can configure colors as well as font types for different elements. diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index 0f621550..73fd6cc2 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -28,7 +28,6 @@ def select( use_shortcuts: bool = False, use_arrow_keys: bool = True, use_indicator: bool = False, - use_pointer: bool = True, instruction: Optional[str] = None, **kwargs: Any, ) -> Question: @@ -69,6 +68,10 @@ def select( qmark: Question prefix displayed in front of the question. By default this is a ``?``. + pointer: Pointer symbol in front of the currently highlighted element. + By default this is a ``»``. + Use ``None`` to disable it. + instruction: A hint on how to navigate the menu. It's ``(Use shortcuts)`` if only ``use_shortcuts`` is set to True, ``(Use arrow keys or shortcuts)`` if ``use_arrow_keys`` @@ -87,9 +90,6 @@ def select( use_arrow_keys: Allow usage of arrow keys to select item. - use_pointer: Flag to enable the pointer in front of the currently - highlighted element. - Returns: :class:`Question`: Question instance, ready to be prompted (using ``.ask()``). """ @@ -114,7 +114,6 @@ def select( use_indicator=use_indicator, use_shortcuts=use_shortcuts, use_arrow_keys=use_arrow_keys, - use_pointer=use_pointer, initial_choice=default, ) From 1155cf1a10a0d0d93571db0a5d996553691c854b Mon Sep 17 00:00:00 2001 From: Kian Cross Date: Wed, 23 Dec 2020 15:45:21 +0000 Subject: [PATCH 3/4] Shift choices to align if a long pointer is used --- questionary/prompts/common.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/questionary/prompts/common.py b/questionary/prompts/common.py index f111e5eb..3a4703e0 100644 --- a/questionary/prompts/common.py +++ b/questionary/prompts/common.py @@ -307,11 +307,12 @@ def append(index: int, choice: Choice): if self.pointer is not None: tokens.append(("class:pointer", " {} ".format(self.pointer))) else: - tokens.append(("class:text", " ")) + tokens.append(("class:text", " " * 3)) tokens.append(("[SetCursorPosition]", "")) else: - tokens.append(("class:text", " ")) + pointer_length = len(self.pointer) if self.pointer is not None else 1 + tokens.append(("class:text", " " * (2 + pointer_length))) if isinstance(choice, Separator): tokens.append(("class:separator", "{}".format(choice.title))) From e5be62a7879d00c17afc5c6d6d437634b3eb1f3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89lie?= Date: Wed, 23 Dec 2020 16:47:35 +0100 Subject: [PATCH 4/4] Optional argument pointer Co-authored-by: Kian Cross --- questionary/prompts/checkbox.py | 2 +- questionary/prompts/common.py | 4 ++-- questionary/prompts/rawselect.py | 2 +- questionary/prompts/select.py | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/questionary/prompts/checkbox.py b/questionary/prompts/checkbox.py index 95faadec..e3f21f65 100644 --- a/questionary/prompts/checkbox.py +++ b/questionary/prompts/checkbox.py @@ -24,7 +24,7 @@ def checkbox( default: Optional[str] = None, validate: Callable[[List[str]], Union[bool, str]] = lambda a: True, qmark: str = DEFAULT_QUESTION_PREFIX, - pointer: str = DEFAULT_SELECTED_POINTER, + pointer: Optional[str] = DEFAULT_SELECTED_POINTER, style: Optional[Style] = None, initial_choice: Optional[Union[str, Choice, Dict[str, Any]]] = None, **kwargs: Any, diff --git a/questionary/prompts/common.py b/questionary/prompts/common.py index 3a4703e0..1991eb6a 100644 --- a/questionary/prompts/common.py +++ b/questionary/prompts/common.py @@ -180,7 +180,7 @@ class InquirerControl(FormattedTextControl): use_indicator: bool use_shortcuts: bool use_arrow_keys: bool - pointer: str + pointer: Optional[str] pointed_at: int is_answered: bool @@ -188,7 +188,7 @@ def __init__( self, choices: Sequence[Union[str, Choice, Dict[str, Any]]], default: Optional[Union[str, Choice, Dict[str, Any]]] = None, - pointer: str = DEFAULT_SELECTED_POINTER, + pointer: Optional[str] = DEFAULT_SELECTED_POINTER, use_indicator: bool = True, use_shortcuts: bool = False, use_arrow_keys: bool = True, diff --git a/questionary/prompts/rawselect.py b/questionary/prompts/rawselect.py index 66beee67..cf2029b8 100644 --- a/questionary/prompts/rawselect.py +++ b/questionary/prompts/rawselect.py @@ -13,7 +13,7 @@ def rawselect( choices: Sequence[Union[str, Choice, Dict[str, Any]]], default: Optional[str] = None, qmark: str = DEFAULT_QUESTION_PREFIX, - pointer: str = DEFAULT_SELECTED_POINTER, + pointer: Optional[str] = DEFAULT_SELECTED_POINTER, style: Optional[Style] = None, **kwargs: Any, ) -> Question: diff --git a/questionary/prompts/select.py b/questionary/prompts/select.py index 73fd6cc2..d0964994 100644 --- a/questionary/prompts/select.py +++ b/questionary/prompts/select.py @@ -23,7 +23,7 @@ def select( choices: Sequence[Union[str, Choice, Dict[str, Any]]], default: Optional[Union[str, Choice, Dict[str, Any]]] = None, qmark: str = DEFAULT_QUESTION_PREFIX, - pointer: str = DEFAULT_SELECTED_POINTER, + pointer: Optional[str] = DEFAULT_SELECTED_POINTER, style: Optional[Style] = None, use_shortcuts: bool = False, use_arrow_keys: bool = True,