bchao1 / bullet

🚅 Interactive prompts made simple. Build a prompt like stacking blocks.
https://pypi.org/project/bullet/
MIT License
3.55k stars 113 forks source link

Allow UTF-8 printable chars , not just ASCII #78

Open a-luna opened 3 years ago

a-luna commented 3 years ago

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.

This PR adds a new function to utils.py, based on a stackoverflow response (Test if a python string is printable):

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¡¢ 漢字 ♀♂

Closes #76

Behoston commented 1 year ago

Can you review @bchao1 ?

Chars that still not works for me: