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

Only Ascii characters accepted #76

Open samschott opened 3 years ago

samschott commented 3 years ago

It appears that only input not accepted. The basic issue lies in getchar:

https://github.com/bchao1/bullet/blob/75f620db198071eb2af98032c6d33804eb88503f/bullet/utils.py#L52-L55

where string.printable contains only printable ASCII characters. This leaves all people with wider utf-8 input in the cold.

a-luna commented 3 years ago

A response to the stackoverflow post, Test if a python string is printable, provides a clever way to check for non-printable characters:

def is_printable(s):
    return not any(repr(ch).startswith("'\\x") or repr(ch).startswith("'\\u") for ch in s)

Replacing the check in utils.py to use this function would allow any utf-8 printable input to be used:

if is_printable(c):
    return c
else:
    return UNDEFINED_KEY

I'll create a PR with this change, hopefully it will be accepted since allowing utf-8 input should be a requirement (IMO).

jab commented 3 years ago

str.startswith optionally takes a tuple of strings to try. Better to write the above as not any(repr(ch).startswith(("'\\x", "'\\u")) for ch in s)

a-luna commented 3 years ago

I had no idea that you could provide a tuple to startswith, thank you for pointing that out! The updated version will be in the PR.