Skip to content

Commit

Permalink
Merge pull request #104 from ElieDeloumeau/pointer_option
Browse files Browse the repository at this point in the history
  • Loading branch information
kiancross committed Dec 23, 2020
2 parents 875ee45 + e5be62a commit 11ceb8e
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion questionary/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "●"
Expand Down
18 changes: 12 additions & 6 deletions questionary/prompts/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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: Optional[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:
Expand Down Expand Up @@ -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.
Expand All @@ -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]]:
Expand Down
17 changes: 9 additions & 8 deletions questionary/prompts/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

from questionary.constants import (
DEFAULT_STYLE,
SELECTED_POINTER,
DEFAULT_SELECTED_POINTER,
INDICATOR_SELECTED,
INDICATOR_UNSELECTED,
INVALID_INPUT,
Expand Down Expand Up @@ -180,27 +180,27 @@ class InquirerControl(FormattedTextControl):
use_indicator: bool
use_shortcuts: bool
use_arrow_keys: bool
use_pointer: bool
pointer: Optional[str]
pointed_at: int
is_answered: bool

def __init__(
self,
choices: Sequence[Union[str, Choice, Dict[str, Any]]],
default: Optional[Union[str, Choice, Dict[str, Any]]] = None,
pointer: Optional[str] = DEFAULT_SELECTED_POINTER,
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,
):

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

if default is not None and default not in choices:
raise ValueError(
Expand Down Expand Up @@ -304,14 +304,15 @@ def append(index: int, choice: Choice):
selected = choice.value in self.selected_options

if index == self.pointed_at:
if self.use_pointer:
tokens.append(("class:pointer", " {} ".format(SELECTED_POINTER)))
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)))
Expand Down
8 changes: 7 additions & 1 deletion questionary/prompts/rawselect.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,6 +13,7 @@ def rawselect(
choices: Sequence[Union[str, Choice, Dict[str, Any]]],
default: Optional[str] = None,
qmark: str = DEFAULT_QUESTION_PREFIX,
pointer: Optional[str] = DEFAULT_SELECTED_POINTER,
style: Optional[Style] = None,
**kwargs: Any,
) -> Question:
Expand Down Expand Up @@ -50,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.
Expand All @@ -61,6 +66,7 @@ def rawselect(
choices,
default,
qmark,
pointer,
style,
use_shortcuts=True,
use_arrow_keys=False,
Expand Down
17 changes: 11 additions & 6 deletions questionary/prompts/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -19,11 +23,11 @@ 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: Optional[str] = DEFAULT_SELECTED_POINTER,
style: Optional[Style] = None,
use_shortcuts: bool = False,
use_arrow_keys: bool = True,
use_indicator: bool = False,
use_pointer: bool = True,
instruction: Optional[str] = None,
**kwargs: Any,
) -> Question:
Expand Down Expand Up @@ -64,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``
Expand All @@ -82,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()``).
"""
Expand All @@ -105,10 +110,10 @@ def select(
ic = InquirerControl(
choices,
default,
pointer=pointer,
use_indicator=use_indicator,
use_shortcuts=use_shortcuts,
use_arrow_keys=use_arrow_keys,
use_pointer=use_pointer,
initial_choice=default,
)

Expand Down

0 comments on commit 11ceb8e

Please sign in to comment.