Temporarily exit cbreak mode?
ericswpark opened this issue · comments
Is there a way to temporarily "exit" cbreak mode like in curses with something like term.nocbreak()
? I need to get user input with the standard Python input()
function but the cbreak mode "hides" input echo on the screen.
I tried using with term.raw()
with the input statement but that ended up locking up my terminal. Is there a way to temporarily exit cbreak mode within a function?
No, Termnal.cbreak is implemented as a context manager, and is generally used in a with
block. If you can do it this way, your code can be pretty simple.
with term.cbreak():
# In cbreak
If you need more control, you can preserve the context manager and enter and exit manually. I would recommend guarding it with try
finally
to avoid remaining in cbreak if you have an unhandled exception.
ctx = None
try:
if need_cbreak:
ctx = term.cbreak()
ctx.__enter__()
# In cbreak
finally:
if ctx is not None:
ctx.__exit__(None, None, None)
Thank you! Using a context manager variable worked for my use case. I think it might be helpful if this was in the documentation.
I'll close the issue now. Thanks again!