Currently, if a user provides a non-ASCII, UTF-8 printable character (e.g., any of the following: äüöëñ0¡¢漢字♀♂) to an Input prompt, the prompt immediately exits and returns None in place of the non-ASCII character.
def is_printable(s: str) -> bool:
"""Determine if a string contains only printable characters.
Args:
s: The string to verify.
Returns:
bool: `True` if all characters in `s` are printable. `False` if any
characters in `s` can not be printed.
"""
# Ref: https://stackoverflow.com/a/50731077
return not any(repr(ch).startswith(("'\\x", "'\\u")) for ch in s)
A call to the is_printable function replaces the line in getchar that checks if each character exists in string.printable :
if is_printable(c):
return c
else:
return UNDEFINED_KEY
Here's an example of the new behavior:
Example
from bullet import Input
prompt = Input("Enter a string with UTF-8 printable characters: ")
user_input = prompt.launch()
print(user_input)
Output
Enter a string with UTF-8 printable characters: abcdef äüöëñ 0¡¢ 漢字 ♀♂
abcdef äüöëñ 0¡¢ 漢字 ♀♂
Currently, if a user provides a non-ASCII, UTF-8 printable character (e.g., any of the following:
äüöëñ0¡¢漢字♀♂
) to anInput
prompt, the prompt immediately exits and returnsNone
in place of the non-ASCII character.This PR adds a new function to
utils.py
, based on a stackoverflow response (Test if a python string is printable):A call to the
is_printable
function replaces the line ingetchar
that checks if each character exists instring.printable
:Here's an example of the new behavior:
Example
Output
Closes #76