Search code examples
pythoncursespython-curses

output on a new line in python curses


I am using curses module in python to display output in real time by reading a file. The string messages are output to the console using addstr() function but I am not able to achieve printing to a newline wherever I need.

sample code:

import json
import curses
w=curses.initscr()

try:
    while True:
        with open('/tmp/install-report.json') as json_data:
            beta = json.load(json_data)
            w.erase()
            w.addstr("\nStatus Report for Install process\n=========\n\n")
            for a1, b1 in beta.iteritems():
                w.addstr("{0} : {1}\n".format(a1, b1))
            w.refresh()
finally:
    curses.endwin()

The above is not really outputting the strings to a new line (notice the \n in addstr()) with each iteration. On the contrary, the script fails off with error if I resize the terminal window.

w.addstr("{0} ==> {1}\n".format(a1, b1))
_curses.error: addstr() returned ERR

Solution

  • There's not enough program to offer more than general advice:

    • you will get an error when printing to the end of the screen if your script does not enable scrolling (see window.scroll).
    • if you resize the terminal window, you will have to read the keyboard to dispose of any KEY_RESIZE (and ignore errors).

    Regarding the expanded question, these features would be used something like this:

    import json
    import curses
    w=curses.initscr()
    w.scrollok(1) # enable scrolling
    w.timeout(1)  # make 1-millisecond timeouts on `getch`
    
    try:
        while True:
            with open('/tmp/install-report.json') as json_data:
                beta = json.load(json_data)
                w.erase()
                w.addstr("\nStatus Report for Install process\n=========\n\n")
                for a1, b1 in beta.iteritems():
                    w.addstr("{0} : {1}\n".format(a1, b1))
                ignore = w.getch()  # wait at most 1msec, then ignore it
    finally:
        curses.endwin()