From 8431c82b42e8957fd84a4cb27afbb4f269702d7e Mon Sep 17 00:00:00 2001 From: Vivek Keshore Date: Fri, 8 Nov 2024 11:12:09 +0530 Subject: [PATCH] REQUEST-3552: Added a choices separator to Prompt.ask. This argument will allow the choices to be separated by anything other than the default "/" in the console. --- rich/prompt.py | 10 +++++++++- tests/test_prompt.py | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/rich/prompt.py b/rich/prompt.py index c7cf25ba..80a72668 100644 --- a/rich/prompt.py +++ b/rich/prompt.py @@ -39,6 +39,7 @@ class PromptBase(Generic[PromptType]): case_sensitive (bool, optional): Matching of choices should be case-sensitive. Defaults to True. show_default (bool, optional): Show default in prompt. Defaults to True. show_choices (bool, optional): Show choices in prompt. Defaults to True. + choices_separator (str, optional): Separator for choices. Defaults to "/". """ response_type: type = str @@ -61,6 +62,7 @@ class PromptBase(Generic[PromptType]): case_sensitive: bool = True, show_default: bool = True, show_choices: bool = True, + choices_separator: str = "/", ) -> None: self.console = console or get_console() self.prompt = ( @@ -74,6 +76,7 @@ class PromptBase(Generic[PromptType]): self.case_sensitive = case_sensitive self.show_default = show_default self.show_choices = show_choices + self.choices_separator = choices_separator @classmethod @overload @@ -87,6 +90,7 @@ class PromptBase(Generic[PromptType]): case_sensitive: bool = True, show_default: bool = True, show_choices: bool = True, + choices_separator: str = "/", default: DefaultType, stream: Optional[TextIO] = None, ) -> Union[DefaultType, PromptType]: @@ -104,6 +108,7 @@ class PromptBase(Generic[PromptType]): case_sensitive: bool = True, show_default: bool = True, show_choices: bool = True, + choices_separator: str = "/", stream: Optional[TextIO] = None, ) -> PromptType: ... @@ -119,6 +124,7 @@ class PromptBase(Generic[PromptType]): case_sensitive: bool = True, show_default: bool = True, show_choices: bool = True, + choices_separator: str = "/", default: Any = ..., stream: Optional[TextIO] = None, ) -> Any: @@ -135,6 +141,7 @@ class PromptBase(Generic[PromptType]): case_sensitive (bool, optional): Matching of choices should be case-sensitive. Defaults to True. show_default (bool, optional): Show default in prompt. Defaults to True. show_choices (bool, optional): Show choices in prompt. Defaults to True. + choices_separator (str, optional): Separator for choices. Defaults to "/". stream (TextIO, optional): Optional text file open for reading to get input. Defaults to None. """ _prompt = cls( @@ -145,6 +152,7 @@ class PromptBase(Generic[PromptType]): case_sensitive=case_sensitive, show_default=show_default, show_choices=show_choices, + choices_separator=choices_separator, ) return _prompt(default=default, stream=stream) @@ -172,7 +180,7 @@ class PromptBase(Generic[PromptType]): prompt.end = "" if self.show_choices and self.choices: - _choices = "/".join(self.choices) + _choices = self.choices_separator.join(self.choices) choices = f"[{_choices}]" prompt.append(" ") prompt.append(choices, "prompt.choices") diff --git a/tests/test_prompt.py b/tests/test_prompt.py index 11bffa71..76a2bbe0 100644 --- a/tests/test_prompt.py +++ b/tests/test_prompt.py @@ -21,6 +21,41 @@ def test_prompt_str(): assert output == expected +def test_prompt_str_choices_separator(): + # Default separator is '/' + INPUT = "egg\nfoo" + console = Console(file=io.StringIO()) + name = Prompt.ask( + "what is your name", + console=console, + choices=["foo", "bar"], + default="baz", + stream=io.StringIO(INPUT), + ) + assert name == "foo" + expected = "what is your name [foo/bar] (baz): Please select one of the available options\nwhat is your name [foo/bar] (baz): " + output = console.file.getvalue() + print(repr(output)) + assert output == expected + + # Custom separator + INPUT = "egg\nfoo" + console = Console(file=io.StringIO()) + name = Prompt.ask( + "what is your name", + console=console, + choices=["foo", "bar"], + default="baz", + choices_separator=" | ", + stream=io.StringIO(INPUT), + ) + assert name == "foo" + expected = "what is your name [foo | bar] (baz): Please select one of the available options\nwhat is your name [foo | bar] (baz): " + output = console.file.getvalue() + print(repr(output)) + assert output == expected + + def test_prompt_str_case_insensitive(): INPUT = "egg\nFoO" console = Console(file=io.StringIO())