Closed impredicative closed 1 year ago
this is exactly provided by input
, so why do you want it in readchar
?
readchar
is mostly meant for usecase where you don't want this and need finer controll, so you would write the functions yourself anyway?
this is exactly provided by input, so why do you want it in readchar?
Huh. That's not true. With input
I have to press Enter. With readchar
I don't have to press Enter. This makes all the difference.
so you would write the functions yourself anyway?
When one is coming from having used input
, an input prompt is probably the most common expected usage. What you're saying is as if Python didn't have input
, and just had readchars
(multiple) instead.
readchars
most common usecase is something like this:
print("type a: ", end='')
while True:
k = readchar()
if k == 'a':
break
print(k, end='')
print("well done")
a input like function would re-prompt for every wrong input and is usually undesirable and the exact prosessing is specific to the usecase, so it cant be part of the libary.
So whats the usecase where this needs a prompt-function and it doesn't make more sens to prompt yourself, matcing your usecase
So whats the usecase
I have to sometimes seek user confirmation for a very urgent action. There is no time to spare for the user to press Enter as with input
. As soon as the y
key is received using input_char
(to indicate confirmation), the intended action is to be urgently executed.
I can defnetly see this usecase, but it is still quite special to you and not really something thats usefull inside the libary for a lot of people. You already have the function written, so just use it. Same for other users that will have slightly different requirement.
Also if you want your response so urgently, you will probably want to also implement a timeout. and than it becomes even more specific to your usecase...
but if you want to code it up, feel free to implement a generic approach and send in a PR
sidenote, you can shorten your function to this:
def input_char(prompt: str, /, *, echo: bool = True) -> str:
print(prompt, end='', flush=True)
char = readchar.readchar()
if echo:
print(char)
return char
you will probably want to also implement a timeout
Although I don't have a timeout, I do check the time used to receive the confirmation. If the user takes more than say five seconds to provide a character (from the time it was requested), it is considered invalid. In this case, the missile launch is aborted.
print(prompt, end='', flush=True)
Thank you. I now use this.
I do need still a newline at the end even if the char is not echoed, which is why I have a print()
.
It may make sense to provide an input prompt functionality.
For example, with Python
input
, I can do:This serves two purposes:
To get this with
readchar
, I would have to use something like: