bullet
bullet copied to clipboard
Allow UTF-8 printable chars , not just ASCII
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
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