Search code examples
pythoncurses

Get echo/noecho state in curses in python


I can set the echo state in curses with echo() and noecho(), but how can I see what state I'm currently in? Here's what I'm trying to do:

# get current echo state
if screen.echo_is_on():
    my_echo_state=False   # echo is currently off
    curses.echo()         # turn echo on
else:
    my_echo_state=True    # echo is already on

# get input from user
input_str = screen.getstr(r, c) 

# return echo to previous state
if my_echo_state==False:
    curses.noecho()

I'm looking for something to accomplish screen.echo_is_on().


Solution

  • There's no getter function for echo in the Python curses binding.

    • Python has methods that correspond to the curses C functions.
    • The state of echo (and some other functions) in the C binding are stored in the SCREEN struct, which traditionally is opaque.
    • other state information is stored in WINDOW which was not opaque.

    There are extensions to the original curses interface. NetBSD introduced the notion of opaque structures for WINDOW, etc. Later, ncurses adopted that as an option to help hide/control state information for threaded applications. To do this, ncurses added functions for accessing data from WINDOW (see curs_opaque(3x)).

    However, there are no new functions for accessing the contents of SCREEN. The enable/disable functions for those are described in curs_inopts(3x).

    If you need a getter, you could write one yourself, e.g., as a class which hides the actual calls to echo and noecho.