bullet icon indicating copy to clipboard operation
bullet copied to clipboard

Allow UTF-8 printable chars , not just ASCII

Open a-luna opened this issue 4 years ago • 1 comments

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

a-luna avatar Feb 06 '21 16:02 a-luna

Can you review @bchao1 ?

Chars that still not works for me:

  • Å‚ - AltGr + l, do nothing or works as select all?
  • Å„ - AltGr + n, moves cursor to the left

Behoston avatar Jul 05 '23 21:07 Behoston