refactor to 'case_sensitive' with default True

This commit is contained in:
Grant Ramsay 2023-11-08 08:12:49 +00:00
parent 9038abed04
commit 15cc59b1a2
No known key found for this signature in database
GPG Key ID: 40EDDC3F71696632
4 changed files with 24 additions and 23 deletions

View File

@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- Adds missing parameters to Panel.fit https://github.com/Textualize/rich/issues/3142
- Adds a `case_insensitive` parameter to `prompt.Prompt`. This allows the `choices` list to be accepted without regard to case. Defaults to `False`.
- Adds a `case_sensitive` parameter to `prompt.Prompt`. This determines if the
response is treated as case-sensitive. Defaults to `True`.
### Fixed
@ -48,7 +49,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Fixed Text.expand_tabs not expanding spans.
- Fixed TimeElapsedColumn from showing negative.
- Fix for escaping strings with a trailing backslash https://github.com/Textualize/rich/issues/2987
- Fixed exception in Markdown with partial table https://github.com/Textualize/rich/issues/3053
- Fixed exception in Markdown with partial table https://github.com/Textualize/rich/issues/3053
- Fixed the HTML export template so that the `<html>` tag comes before the `<head>` tag https://github.com/Textualize/rich/issues/3021
- Fixed issue with custom classes overwriting `__eq__` https://github.com/Textualize/rich/issues/2875
- Fix rich.pretty.install breakage in iPython https://github.com/Textualize/rich/issues/3013
@ -2021,10 +2022,10 @@ Major version bump for a breaking change to `Text.stylize signature`, which corr
[13.5.3]: https://github.com/textualize/rich/compare/v13.5.2...v13.5.3
[13.5.2]: https://github.com/textualize/rich/compare/v13.5.1...v13.5.2
[13.5.1]: https://github.com/textualize/rich/compare/v13.5.0...v13.5.1
[13.5.0]: https://github.com/textualize/rich/compare/v13.4.2...v13.5.0
[13.4.2]: https://github.com/textualize/rich/compare/v13.4.1...v13.4.2
[13.4.1]: https://github.com/textualize/rich/compare/v13.4.0...v13.4.1
[13.4.0]: https://github.com/textualize/rich/compare/v13.3.5...v13.4.0
[13.5.0]: https://github.com/textualize/rich/compare/v13.4.2...v13.5.0
[13.4.2]: https://github.com/textualize/rich/compare/v13.4.1...v13.4.2
[13.4.1]: https://github.com/textualize/rich/compare/v13.4.0...v13.4.1
[13.4.0]: https://github.com/textualize/rich/compare/v13.3.5...v13.4.0
[13.3.5]: https://github.com/textualize/rich/compare/v13.3.4...v13.3.5
[13.3.4]: https://github.com/textualize/rich/compare/v13.3.3...v13.3.4
[13.3.3]: https://github.com/textualize/rich/compare/v13.3.2...v13.3.3

View File

@ -18,10 +18,10 @@ If you supply a list of choices, the prompt will loop until the user enters one
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name", choices=["Paul", "Jessica", "Duncan"], default="Paul")
By default this is case sensitive, but you can set `case_insensitive=True` to make it case insensitive::
By default this is case sensitive, but you can set `case_sensitive=False` to make it case insensitive::
>>> from rich.prompt import Prompt
>>> name = Prompt.ask("Enter your name", choices=["Paul", "Jessica", "Duncan"], default="Paul", case_insensitive=True)
>>> name = Prompt.ask("Enter your name", choices=["Paul", "Jessica", "Duncan"], default="Paul", case_sensitive=False)
Now, it would accept "paul" or "Paul" as valid responses.

View File

@ -36,7 +36,7 @@ class PromptBase(Generic[PromptType]):
console (Console, optional): A Console instance or None to use global console. Defaults to None.
password (bool, optional): Enable password input. Defaults to False.
choices (List[str], optional): A list of valid choices. Defaults to None.
case_insensitive (bool, optional): Enable case insensitive matching of choices. Defaults to False.
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.
"""
@ -58,7 +58,7 @@ class PromptBase(Generic[PromptType]):
console: Optional[Console] = None,
password: bool = False,
choices: Optional[List[str]] = None,
case_insensitive: bool = False,
case_sensitive: bool = True,
show_default: bool = True,
show_choices: bool = True,
) -> None:
@ -71,7 +71,7 @@ class PromptBase(Generic[PromptType]):
self.password = password
if choices is not None:
self.choices = choices
self.case_insensitive = case_insensitive
self.case_sensitive = case_sensitive
self.show_default = show_default
self.show_choices = show_choices
@ -84,7 +84,7 @@ class PromptBase(Generic[PromptType]):
console: Optional[Console] = None,
password: bool = False,
choices: Optional[List[str]] = None,
case_insensitive: bool = False,
case_sensitive: bool = True,
show_default: bool = True,
show_choices: bool = True,
default: DefaultType,
@ -101,7 +101,7 @@ class PromptBase(Generic[PromptType]):
console: Optional[Console] = None,
password: bool = False,
choices: Optional[List[str]] = None,
case_insensitive: bool = False,
case_sensitive: bool = True,
show_default: bool = True,
show_choices: bool = True,
stream: Optional[TextIO] = None,
@ -116,7 +116,7 @@ class PromptBase(Generic[PromptType]):
console: Optional[Console] = None,
password: bool = False,
choices: Optional[List[str]] = None,
case_insensitive: bool = False,
case_sensitive: bool = True,
show_default: bool = True,
show_choices: bool = True,
default: Any = ...,
@ -132,7 +132,7 @@ class PromptBase(Generic[PromptType]):
console (Console, optional): A Console instance or None to use global console. Defaults to None.
password (bool, optional): Enable password input. Defaults to False.
choices (List[str], optional): A list of valid choices. Defaults to None.
case_insensitive (bool, optional): Enable case insensitive matching of choices. Defaults to False.
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.
stream (TextIO, optional): Optional text file open for reading to get input. Defaults to None.
@ -142,7 +142,7 @@ class PromptBase(Generic[PromptType]):
console=console,
password=password,
choices=choices,
case_insensitive=case_insensitive,
case_sensitive=case_sensitive,
show_default=show_default,
show_choices=show_choices,
)
@ -220,9 +220,9 @@ class PromptBase(Generic[PromptType]):
bool: True if choice was valid, otherwise False.
"""
assert self.choices is not None
if self.case_insensitive:
return value.strip().lower() in [choice.lower() for choice in self.choices]
return value.strip() in self.choices
if self.case_sensitive:
return value.strip() in self.choices
return value.strip().lower() in [choice.lower() for choice in self.choices]
def process_response(self, value: str) -> PromptType:
"""Process response from user, convert to prompt type.
@ -246,7 +246,7 @@ class PromptBase(Generic[PromptType]):
if not self.check_choice(value):
raise InvalidResponse(self.illegal_choice_message)
if self.case_insensitive:
if not self.case_sensitive:
# return the original choice, not the lower case version
return_value = self.response_type(
self.choices[
@ -391,8 +391,8 @@ if __name__ == "__main__": # pragma: no cover
doggie = Prompt.ask(
"What's the best Dog? (Case INSENSITIVE)",
choices=["BT", "Collie", "Labradoodle"],
case_insensitive=True,
choices=["Border Terrier", "Collie", "Labradoodle"],
case_sensitive=False,
)
print(f"doggie={doggie!r}")

View File

@ -29,7 +29,7 @@ def test_prompt_str_case_insensitive():
console=console,
choices=["foo", "bar"],
default="baz",
case_insensitive=True,
case_sensitive=False,
stream=io.StringIO(INPUT),
)
assert name == "foo"