jquast / blessed

Blessed is an easy, practical library for making python terminal apps
http://pypi.python.org/pypi/blessed
MIT License
1.18k stars 71 forks source link

terminal.cbreak() seems to have no effect #193

Closed jmkd3v closed 3 years ago

jmkd3v commented 3 years ago

I'm writing a program that has simple yes/no inputs that use Terminal.inkey(). It works, however it only updates after a newline is sent. This is an issue, since I'm looking for an output like this:

Question > Y
Question > N
Question > N

If I try to do this, it only shows the question and the key you entered after a newline character is sent, meaning the only way to get it to work properly was to do something like this:

Question?
Y
Question?
N
Question?
N

This is not how it was intended. I looked in the docs and found the context manager terminal.cbreak() but it seems to have no effect. I'm still having the same issue where nothing updates until a newline is sent. I'm using it properly, so I don't see why this is happening.

I'm on Windows 10 110.0.19042 Build 19042, using cmd.exe with Python 3.9.1 in a venv.

avylove commented 3 years ago

I'll try to take a look, but can you send a minimal code sample illustrating the behavior?

jmkd3v commented 3 years ago

Sure.

from blessed import Terminal
from sys import stdout
terminal = Terminal()

# This will not work:
with terminal.cbreak():
    stdout.write("ABCDEFG > ")
    key = terminal.inkey()
    stdout.write(str(key))

# This will work:
with terminal.cbreak():
    print("ABCDEFG > ")
    key = terminal.inkey()
    print(str(key))
avylove commented 3 years ago

Running 3.9.0 (Choco install) on Windows 10.0.19041, this works as expected, no new line needed. Exact same output as on Linux.

ABCDEFG > 3ABCDEFG >
3

I'm still not sure what you're trying to do. There is no difference related to cbreak or inkey in your two examples. The only difference is the way you are outputting to STDOUT, but cbreak operates on STDIN.

jmkd3v commented 3 years ago

Ah, I see. I believe I have misunderstood the documentation. Thank you very much for the help.

jquast commented 3 years ago

If you need to print without newline, empty the "end" argument to print, print("ABCDEFG > ", end='')

(I see this mistake enough it's probably something to clarify in the documentation)