From df4119e6beaafb1b461a1f369f6cb725ba839c5d Mon Sep 17 00:00:00 2001 From: Kevin Zhuang Date: Sun, 29 Aug 2021 11:44:21 +1000 Subject: [PATCH] refactor: rename `tips` argument to `long_instruction` #7 --- InquirerPy/base/complex.py | 13 +++++----- InquirerPy/base/list.py | 11 ++------ InquirerPy/containers/instruction.py | 38 ++++++++++++++++++++++++++++ InquirerPy/prompts/checkbox.py | 6 ++--- InquirerPy/prompts/expand.py | 6 ++--- InquirerPy/prompts/fuzzy.py | 16 ++++++------ InquirerPy/prompts/list.py | 16 ++++++------ InquirerPy/prompts/rawlist.py | 6 ++--- 8 files changed, 72 insertions(+), 40 deletions(-) create mode 100644 InquirerPy/containers/instruction.py diff --git a/InquirerPy/base/complex.py b/InquirerPy/base/complex.py index 97e7f65..4d33e98 100644 --- a/InquirerPy/base/complex.py +++ b/InquirerPy/base/complex.py @@ -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, @@ -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, @@ -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 diff --git a/InquirerPy/base/list.py b/InquirerPy/base/list.py index 791e1b7..6f84261 100644 --- a/InquirerPy/base/list.py +++ b/InquirerPy/base/list.py @@ -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, @@ -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, @@ -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}, ], @@ -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}], @@ -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.""" diff --git a/InquirerPy/containers/instruction.py b/InquirerPy/containers/instruction.py new file mode 100644 index 0000000..0d7bbd2 --- /dev/null +++ b/InquirerPy/containers/instruction.py @@ -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)] diff --git a/InquirerPy/prompts/checkbox.py b/InquirerPy/prompts/checkbox.py index 27271f1..f42f6d9 100644 --- a/InquirerPy/prompts/checkbox.py +++ b/InquirerPy/prompts/checkbox.py @@ -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. @@ -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, @@ -175,7 +175,7 @@ def __init__( qmark=qmark, amark=amark, instruction=instruction, - tips=tips, + long_instruction=long_instruction, transformer=transformer, filter=filter, height=height, diff --git a/InquirerPy/prompts/expand.py b/InquirerPy/prompts/expand.py index a3ae7df..6ed8c6f 100644 --- a/InquirerPy/prompts/expand.py +++ b/InquirerPy/prompts/expand.py @@ -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. @@ -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, @@ -253,7 +253,7 @@ def __init__( qmark=qmark, amark=amark, instruction=instruction, - tips=tips, + long_instruction=long_instruction, transformer=transformer, filter=filter, height=height, diff --git a/InquirerPy/prompts/fuzzy.py b/InquirerPy/prompts/fuzzy.py index 15e9124..0e0b5f8 100644 --- a/InquirerPy/prompts/fuzzy.py +++ b/InquirerPy/prompts/fuzzy.py @@ -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 @@ -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. @@ -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, @@ -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, @@ -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, ), ] diff --git a/InquirerPy/prompts/list.py b/InquirerPy/prompts/list.py index b4de2e8..25b74d6 100644 --- a/InquirerPy/prompts/list.py +++ b/InquirerPy/prompts/list.py @@ -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 @@ -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. @@ -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, @@ -185,7 +185,7 @@ def __init__( qmark=qmark, amark=amark, instruction=instruction, - tips=tips, + long_instruction=long_instruction, transformer=transformer, filter=filter, validate=validate, @@ -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, ), ] diff --git a/InquirerPy/prompts/rawlist.py b/InquirerPy/prompts/rawlist.py index b45eab4..5e32bcd 100644 --- a/InquirerPy/prompts/rawlist.py +++ b/InquirerPy/prompts/rawlist.py @@ -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. @@ -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, @@ -203,7 +203,7 @@ def __init__( qmark=qmark, amark=amark, instruction=instruction, - tips=tips, + long_instruction=long_instruction, transformer=transformer, filter=filter, height=height,