Skip to content

Commit

Permalink
refactor: rename tips argument to long_instruction #7
Browse files Browse the repository at this point in the history
  • Loading branch information
kazhala committed Aug 29, 2021
1 parent 4a4b53b commit df4119e
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 40 deletions.
13 changes: 7 additions & 6 deletions InquirerPy/base/complex.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def __init__(
qmark: str = "?",
amark: str = "?",
instruction: str = "",
tips: str = "",
long_instruction: str = "",
transformer: Callable[[Any], Any] = None,
filter: Callable[[Any], Any] = None,
validate: Union[Callable[[Any], bool], Validator] = None,
Expand Down Expand Up @@ -93,20 +93,21 @@ def __init__(
self._application: Application
self._spinner_enable = spinner_enable
self._set_exception_handler = set_exception_handler
self._tips = tips
self._display_tips = True if tips else False
self._long_instruction = long_instruction
self._border = border
self._height_offset = 2 # prev prompt result + current prompt question
if self._border:
self._height_offset += 2
if self._tips:
if self._long_instruction:
self._height_offset += 1

self._is_vim_edit = Condition(lambda: self._editing_mode == EditingMode.VI)
self._is_invalid = Condition(lambda: self._invalid)
self._is_loading = Condition(lambda: self.loading)
self._is_spinner_enable = Condition(lambda: self._spinner_enable)
self._is_displaying_tips = Condition(lambda: self._display_tips)
self._is_displaying_long_instruction = Condition(
lambda: self._long_instruction != ""
)

self._spinner = SpinnerWindow(
loading=self._is_loading & self._is_spinner_enable,
Expand Down Expand Up @@ -299,7 +300,7 @@ def extra_lines_due_to_wrapping(self) -> int:
# message wrap
result += self.total_message_length // term_width
# long instruction wrap
result += len(self._tips) // term_width
result += len(self._long_instruction) // term_width

return result

Expand Down
11 changes: 2 additions & 9 deletions InquirerPy/base/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(
qmark: str = "?",
amark: str = "?",
instruction: str = "",
tips: str = "",
long_instruction: str = "",
border: bool = False,
transformer: Callable[[Any], Any] = None,
filter: Callable[[Any], Any] = None,
Expand Down Expand Up @@ -62,7 +62,7 @@ def __init__(
invalid_message=invalid_message,
validate=validate,
instruction=instruction,
tips=tips,
long_instruction=long_instruction,
wrap_lines=wrap_lines,
spinner_enable=spinner_enable,
spinner_pattern=spinner_pattern,
Expand Down Expand Up @@ -91,7 +91,6 @@ def __init__(
{"key": "c-p", "filter": ~self._is_vim_edit},
{"key": "k", "filter": self._is_vim_edit},
],
"toggle-tips": [{"key": "alt-t"}],
"toggle": [
{"key": "space", "filter": self._is_multiselect},
],
Expand All @@ -114,7 +113,6 @@ def __init__(
self.kb_func_lookup = {
"down": [{"func": self._handle_down}],
"up": [{"func": self._handle_up}],
"toggle-tips": [{"func": self._toggle_tips}],
"toggle": [{"func": self._toggle_choice}],
"toggle-down": [{"func": self._toggle_choice}, {"func": self._handle_down}],
"toggle-up": [{"func": self._toggle_choice}, {"func": self._handle_up}],
Expand Down Expand Up @@ -245,11 +243,6 @@ def _handle_up(self) -> bool:
return True
return False

def _toggle_tips(self) -> None:
"""Toggle tips display."""
if self._tips:
self._display_tips = not self._display_tips

@abstractmethod
def _toggle_choice(self) -> None:
"""Handle event when user attempting to toggle the state of the chocie."""
Expand Down
38 changes: 38 additions & 0 deletions InquirerPy/containers/instruction.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"""Module contains :class:`.InstructionWindow` which can be used to display long instructions."""

from typing import TYPE_CHECKING

from prompt_toolkit.layout.containers import ConditionalContainer, Window
from prompt_toolkit.layout.controls import FormattedTextControl

if TYPE_CHECKING:
from prompt_toolkit.filters.base import FilterOrBool
from prompt_toolkit.formatted_text.base import AnyFormattedText


class InstructionWindow(ConditionalContainer):
"""Conditional `prompt_toolkit` :class:`~prompt_toolkit.layout.Window` that displays long instructions.
Args:
message: Long instructions to display.
filter: Condition to display the instruction window.
"""

def __init__(self, message: str, filter: "FilterOrBool", wrap_lines: bool) -> None:
self._message = message
super().__init__(
Window(
FormattedTextControl(text=self._get_message),
dont_extend_height=True,
wrap_lines=wrap_lines,
),
filter=filter,
)

def _get_message(self) -> "AnyFormattedText":
"""Get long instruction to display.
Returns:
FormattedText in list of tuple format.
"""
return [("class:instruction", self._message)]
6 changes: 3 additions & 3 deletions InquirerPy/prompts/checkbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class CheckboxPrompt(ListPrompt):
disabled_symbol: Custom symbol which indicate the checkbox is not ticked.
border: Create border around the choice window.
instruction: Short instruction to display next to the `message`.
tips: Long instructions or tips to display in a floating window at the bottom.
long_instruction: Long instructions to display at the bottom of the prompt.
validate: Validation callable or class to validate user input.
invalid_message: Error message to display when input is invalid.
transformer: A callable to transform the result that gets printed in the terminal.
Expand Down Expand Up @@ -140,7 +140,7 @@ def __init__(
disabled_symbol: str = "%s " % INQUIRERPY_EMPTY_HEX_SEQUENCE,
border: bool = False,
instruction: str = "",
tips: str = "",
long_instruction: str = "",
transformer: Callable[[Any], Any] = None,
filter: Callable[[Any], Any] = None,
height: Union[int, str] = None,
Expand Down Expand Up @@ -175,7 +175,7 @@ def __init__(
qmark=qmark,
amark=amark,
instruction=instruction,
tips=tips,
long_instruction=long_instruction,
transformer=transformer,
filter=filter,
height=height,
Expand Down
6 changes: 3 additions & 3 deletions InquirerPy/prompts/expand.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ class ExpandPrompt(ListPrompt):
amark: Custom symbol that will be displayed infront of the question after its answered.
pointer: Custom symbol that will be used to indicate the current choice selection.
instruction: Short instruction to display next to the `message`.
tips: Long instructions or tips to display in a floating window at the bottom.
long_instruction: Long instructions to display at the bottom of the prompt.
validate: Validation callable or class to validate user input.
invalid_message: Error message to display when input is invalid.
transformer: A callable to transform the result that gets printed in the terminal.
Expand Down Expand Up @@ -210,7 +210,7 @@ def __init__(
help_msg: str = "Help, list all choices",
expand_pointer: str = "%s " % INQUIRERPY_POINTER_SEQUENCE,
instruction: str = "",
tips: str = "",
long_instruction: str = "",
transformer: Callable[[Any], Any] = None,
filter: Callable[[Any], Any] = None,
height: Union[int, str] = None,
Expand Down Expand Up @@ -253,7 +253,7 @@ def __init__(
qmark=qmark,
amark=amark,
instruction=instruction,
tips=tips,
long_instruction=long_instruction,
transformer=transformer,
filter=filter,
height=height,
Expand Down
16 changes: 8 additions & 8 deletions InquirerPy/prompts/fuzzy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

from InquirerPy.base import FakeDocument, InquirerPyUIListControl
from InquirerPy.base.list import BaseListPrompt
from InquirerPy.containers.instruction import InstructionWindow
from InquirerPy.containers.message import MessageWindow
from InquirerPy.containers.tips import TipsWindow
from InquirerPy.containers.validation import ValidationWindow
from InquirerPy.enum import INQUIRERPY_POINTER_SEQUENCE
from InquirerPy.exceptions import InvalidArgument
Expand Down Expand Up @@ -255,7 +255,7 @@ class FuzzyPrompt(BaseListPrompt):
amark: Custom symbol that will be displayed infront of the question after its answered.
pointer: Custom symbol that will be used to indicate the current choice selection.
instruction: Short instruction to display next to the `message`.
tips: Long instructions or tips to display in a floating window at the bottom.
long_instruction: Long instructions to display at the bottom of the prompt.
validate: Validation callable or class to validate user input.
invalid_message: Error message to display when input is invalid.
transformer: A callable to transform the result that gets printed in the terminal.
Expand Down Expand Up @@ -298,7 +298,7 @@ def __init__(
transformer: Callable[[Any], Any] = None,
filter: Callable[[Any], Any] = None,
instruction: str = "",
tips: str = "",
long_instruction: str = "",
multiselect: bool = False,
prompt: str = INQUIRERPY_POINTER_SEQUENCE,
marker: str = INQUIRERPY_POINTER_SEQUENCE,
Expand Down Expand Up @@ -345,7 +345,7 @@ def __init__(
invalid_message=invalid_message,
multiselect=multiselect,
instruction=instruction,
tips=tips,
long_instruction=long_instruction,
keybindings=keybindings,
cycle=cycle,
wrap_lines=wrap_lines,
Expand Down Expand Up @@ -420,11 +420,11 @@ def __init__(
),
ConditionalContainer(
Window(content=DummyControl()),
filter=~IsDone() & self._is_displaying_tips,
filter=~IsDone() & self._is_displaying_long_instruction,
),
TipsWindow(
message=self._tips,
filter=self._is_displaying_tips & ~IsDone(),
InstructionWindow(
message=self._long_instruction,
filter=self._is_displaying_long_instruction & ~IsDone(),
wrap_lines=self._wrap_lines,
),
]
Expand Down
16 changes: 8 additions & 8 deletions InquirerPy/prompts/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
from InquirerPy.base import InquirerPyUIListControl
from InquirerPy.base.complex import FakeDocument
from InquirerPy.base.list import BaseListPrompt
from InquirerPy.containers.instruction import InstructionWindow
from InquirerPy.containers.message import MessageWindow
from InquirerPy.containers.tips import TipsWindow
from InquirerPy.containers.validation import ValidationWindow
from InquirerPy.enum import INQUIRERPY_POINTER_SEQUENCE
from InquirerPy.separator import Separator
Expand Down Expand Up @@ -105,7 +105,7 @@ class ListPrompt(BaseListPrompt):
amark: Custom symbol that will be displayed infront of the question after its answered.
pointer: Custom symbol that will be used to indicate the current choice selection.
instruction: Short instruction to display next to the `message`.
tips: Long instructions or tips to display in a floating window at the bottom.
long_instruction: Long instructions to display at the bottom of the prompt.
validate: Validation callable or class to validate user input.
invalid_message: Error message to display when input is invalid.
transformer: A callable to transform the result that gets printed in the terminal.
Expand Down Expand Up @@ -145,7 +145,7 @@ def __init__(
amark: str = "?",
pointer: str = INQUIRERPY_POINTER_SEQUENCE,
instruction: str = "",
tips: str = "",
long_instruction: str = "",
transformer: Callable[[Any], Any] = None,
filter: Callable[[Any], Any] = None,
height: Union[int, str] = None,
Expand Down Expand Up @@ -185,7 +185,7 @@ def __init__(
qmark=qmark,
amark=amark,
instruction=instruction,
tips=tips,
long_instruction=long_instruction,
transformer=transformer,
filter=filter,
validate=validate,
Expand Down Expand Up @@ -235,11 +235,11 @@ def __init__(
),
ConditionalContainer(
Window(content=DummyControl()),
filter=~IsDone() & self._is_displaying_tips,
filter=~IsDone() & self._is_displaying_long_instruction,
),
TipsWindow(
message=self._tips,
filter=self._is_displaying_tips & ~IsDone(),
InstructionWindow(
message=self._long_instruction,
filter=self._is_displaying_long_instruction & ~IsDone(),
wrap_lines=self._wrap_lines,
),
]
Expand Down
6 changes: 3 additions & 3 deletions InquirerPy/prompts/rawlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class RawlistPrompt(ListPrompt):
amark: Custom symbol that will be displayed infront of the question after its answered.
pointer: Custom symbol that will be used to indicate the current choice selection.
instruction: Short instruction to display next to the `message`.
tips: Long instructions or tips to display in a floating window at the bottom.
long_instruction: Long instructions to display at the bottom of the prompt.
validate: Validation callable or class to validate user input.
invalid_message: Error message to display when input is invalid.
transformer: A callable to transform the result that gets printed in the terminal.
Expand Down Expand Up @@ -162,7 +162,7 @@ def __init__(
amark: str = "?",
pointer: str = " ",
instruction: str = "",
tips: str = "",
long_instruction: str = "",
transformer: Callable[[Any], Any] = None,
filter: Callable[[Any], Any] = None,
height: Union[int, str] = None,
Expand Down Expand Up @@ -203,7 +203,7 @@ def __init__(
qmark=qmark,
amark=amark,
instruction=instruction,
tips=tips,
long_instruction=long_instruction,
transformer=transformer,
filter=filter,
height=height,
Expand Down

0 comments on commit df4119e

Please sign in to comment.