def test_style(self): style = get_style() self.assertEqual( style, InquirerPyStyle(get_sample_style()), ) os.environ["INQUIRERPY_STYLE_QUESTIONMARK"] = "#000000" os.environ["INQUIRERPY_STYLE_ANSWERMARK"] = "#000000" os.environ["INQUIRERPY_STYLE_ANSWER"] = "#111111" os.environ["INQUIRERPY_STYLE_QUESTION"] = "#222222" os.environ["INQUIRERPY_STYLE_ANSWERED_QUESTION"] = "#222222" os.environ["INQUIRERPY_STYLE_INSTRUCTION"] = "#333333" os.environ["INQUIRERPY_STYLE_INPUT"] = "#444444" os.environ["INQUIRERPY_STYLE_POINTER"] = "#555555" os.environ["INQUIRERPY_STYLE_CHECKBOX"] = "#66666" os.environ["INQUIRERPY_STYLE_SEPARATOR"] = "#777777" os.environ["INQUIRERPY_STYLE_SKIPPED"] = "#888888" os.environ["INQUIRERPY_STYLE_FUZZY_PROMPT"] = "#999999" os.environ["INQUIRERPY_STYLE_FUZZY_INFO"] = "#aaaaaa" os.environ["INQUIRERPY_STYLE_MARKER"] = "#bbbbbb" os.environ["INQUIRERPY_STYLE_FUZZY_BORDER"] = "#cccccc" os.environ["INQUIRERPY_STYLE_FUZZY_MATCH"] = "#dddddd" os.environ["INQUIRERPY_STYLE_VALIDATOR"] = "#dddddd" os.environ["INQUIRERPY_STYLE_SPINNER_PATTERN"] = "#ssssss" os.environ["INQUIRERPY_STYLE_SPINNER_TEXT"] = "#llllll" os.environ["INQUIRERPY_STYLE_LONG_INSTRUCTION"] = "#kkkkkk" style = get_style() self.assertEqual( style, InquirerPyStyle( { "questionmark": "#000000", "answermark": "#000000", "answer": "#111111", "input": "#444444", "question": "#222222", "answered_question": "#222222", "instruction": "#333333", "long_instruction": "#kkkkkk", "pointer": "#555555", "checkbox": "#66666", "separator": "#777777", "skipped": "#888888", "fuzzy_prompt": "#999999", "fuzzy_info": "#aaaaaa", "marker": "#bbbbbb", "validation-toolbar": "#dddddd", "fuzzy_match": "#dddddd", "frame.border": "#cccccc", "spinner_pattern": "#ssssss", "spinner_text": "#llllll", "bottom-toolbar": "noreverse", }, ), )
def test_format_style(self): raw = { "questionmark": "#000000", "answermark": "#mmmmmm", "answer": "#111111", "input": "#444444", "question": "#222222", "answered_question": "#222222", "instruction": "#333333", "long_instruction": "#kkkkkk", "pointer": "#555555", "checkbox": "#66666", "separator": "#777777", "skipped": "#888888", "fuzzy_prompt": "#999999", "fuzzy_info": "#aaaaaa", "marker": "#bbbbbb", "validator": "#dddddd", "fuzzy_match": "#dddddd", "fuzzy_border": "#cccccc", "spinner_pattern": "#ssssss", "spinner_text": "#llllll", "bottom-toolbar": "noreverse", } style = get_style(raw) raw["frame.border"] = raw.pop("fuzzy_border") raw["validation-toolbar"] = raw.pop("validator") self.assertEqual( style, InquirerPyStyle(raw), )
async def prompt_async( questions: InquirerPyQuestions, style: Dict[str, str] = None, vi_mode: bool = False, raise_keyboard_interrupt: bool = True, keybindings: Dict[str, List[Dict[str, Any]]] = None, style_override: bool = True, ) -> InquirerPySessionResult: """Classic syntax entrypoint to create a prompt session via asynchronous method. Refer to :func:`InquirerPy.resolver.prompt` for detailed documentations. """ result: InquirerPySessionResult = {} if not keybindings: keybindings = {} questions = _get_questions(questions=questions) question_style = get_style(style, style_override) for index, original_question in enumerate(questions): try: question, question_type, question_name, message = _get_question( original_question=original_question, result=result, index=index) if question is None: continue args = { "message": message, "style": question_style, "vi_mode": vi_mode, "raise_keyboard_interrupt": raise_keyboard_interrupt, "session_result": result, "keybindings": { **keybindings, **question.pop("keybindings", {}) }, } result[question_name] = await question_mapping[question_type]( **args, **question).execute_async() except KeyError: raise RequiredKeyNotFound return result
def test_constructor_custom(self, mocked_style, mocked_validator): input_prompt = InputPrompt( message=lambda _: "Enter your name", style=get_style({"questionmark": "#111111"}, style_override=False), qmark="[?]", amark="*", default=lambda _: "1", vi_mode=True, validate=NumberValidator(), ) style = get_sample_style() style["questionmark"] = "#111111" self.assertEqual(input_prompt._message, "Enter your name") mocked_style.assert_has_calls([call(style)]) self.assertEqual(input_prompt._default, "1") self.assertEqual(input_prompt._qmark, "[?]") self.assertEqual(input_prompt._amark, "*") self.assertEqual(input_prompt._editing_mode, EditingMode.VI) mocked_validator.assert_not_called()
def prompt( questions: InquirerPyQuestions, style: Dict[str, str] = None, vi_mode: bool = False, raise_keyboard_interrupt: bool = True, keybindings: Dict[str, List[Dict[str, Any]]] = None, style_override: bool = True, ) -> InquirerPySessionResult: """Classic syntax entrypoint to create a prompt session. Resolve user provided list of questions, display prompts and get the results. Args: questions: A list of :ref:`pages/prompt:question` to ask. Refer to documentation for more info. style: A :class:`dict` containing the style specification for the prompt. Refer to :ref:`pages/style:Style` for more info. vi_mode: Use vim keybindings for the prompt instead of the default emacs keybindings. Refer to :ref:`pages/kb:Keybindings` for more info. raise_keyboard_interrupt: Raise the :class:`KeyboardInterrupt` exception when `ctrl-c` is pressed. If false, the result will be `None` and the question is skiped. keybindings: List of custom :ref:`pages/kb:Keybindings` to apply. Refer to documentation for more info. style_override: Override all default styles. When providing any style customisation, all default styles are removed when this is True. Returns: A dictionary containing all of the question answers. The key is the name of the question and the value is the user answer. If the `name` key is not present as part of the question, then the question index will be used as the key. Raises: RequiredKeyNotFound: When the question is missing required keys. InvalidArgument: When the provided `questions` argument is not a type of :class:`list` nor :class:`dictionary`. Examples: >>> from InquirerPy import prompt >>> from InquirerPy.validator import NumberValidator >>> questions = [ ... { ... "type": "input", ... "message": "Enter your age:", ... "validate": NumberValidator(), ... "invalid_message": "Input should be number.", ... "default": "18", ... "name": "age", ... "filter": lambda result: int(result), ... "transformer": lambda result: "Adult" if int(result) >= 18 else "Youth", ... }, ... { ... "type": "rawlist", ... "message": "What drinks would you like to buy:", ... "default": 2, ... "choices": lambda result: ["Soda", "Cidr", "Water", "Milk"] ... if result["age"] < 18 ... else ["Wine", "Beer"], ... "name": "drink", ... }, ... { ... "type": "list", ... "message": "Would you like a bag:", ... "choices": ["Yes", "No"], ... "when": lambda result: result["drink"] in {"Wine", "Beer"}, ... }, ... {"type": "confirm", "message": "Confirm?", "default": True}, ... ] >>> result = prompt(questions=questions) """ result: InquirerPySessionResult = {} if not keybindings: keybindings = {} questions = _get_questions(questions=questions) question_style = get_style(style, style_override) for index, original_question in enumerate(questions): try: question, question_type, question_name, message = _get_question( original_question=original_question, result=result, index=index) if question is None: continue args = { "message": message, "style": question_style, "vi_mode": vi_mode, "raise_keyboard_interrupt": raise_keyboard_interrupt, "session_result": result, "keybindings": { **keybindings, **question.pop("keybindings", {}) }, } result[question_name] = question_mapping[question_type]( **args, **question).execute() except KeyError: raise RequiredKeyNotFound return result
def __init__( self, message: InquirerPyMessage, style: InquirerPyStyle = None, vi_mode: bool = False, qmark: str = "?", amark: str = "?", instruction: str = "", validate: InquirerPyValidate = None, invalid_message: str = "Invalid input", transformer: Callable[[Any], Any] = None, filter: Callable[[Any], Any] = None, default: Any = "", wrap_lines: bool = True, raise_keyboard_interrupt: bool = True, mandatory: bool = True, mandatory_message: str = "Mandatory prompt", session_result: InquirerPySessionResult = None, ) -> None: self._mandatory = mandatory self._mandatory_message = mandatory_message self._result = session_result or {} self._message = (message if not isinstance(message, Callable) else cast(Callable, message)(self._result)) self._instruction = instruction self._default = (default if not isinstance(default, Callable) else default(self._result)) self._style = Style.from_dict( style.dict if style else get_style().dict) self._qmark = qmark self._amark = amark self._status = {"answered": False, "result": None, "skipped": False} self._kb = KeyBindings() self._lexer = "class:input" self._transformer = transformer self._filter = filter self._wrap_lines = wrap_lines self._editing_mode = (EditingMode.VI if vi_mode or bool( os.getenv("INQUIRERPY_VI_MODE", False)) else EditingMode.EMACS) if isinstance(validate, Validator): self._validator = validate else: self._validator = Validator.from_callable( validate if validate else lambda _: True, invalid_message, move_cursor_to_end=True, ) self._raise_kbi = not os.getenv("INQUIRERPY_NO_RAISE_KBI", not raise_keyboard_interrupt) self._is_rasing_kbi = Condition(lambda: self._raise_kbi) self._kb_maps = { "answer": [{ "key": Keys.Enter }], "interrupt": [ { "key": "c-c", "filter": self._is_rasing_kbi }, { "key": "c-d", "filter": ~self._is_rasing_kbi }, ], "skip": [{ "key": "c-z" }, { "key": "c-c", "filter": ~self._is_rasing_kbi }], } self._kb_func_lookup = { "answer": [{ "func": self._handle_enter }], "interrupt": [{ "func": self._handle_interrupt }], "skip": [{ "func": self._handle_skip }], }