Search code examples
pythonterminalncursescurses

Python Ncurses printing a single char to position


I am not so into ncurses, but it should be working on C, I do not know what is wrong, I just want to print some character to screen continuosly but I cannot find how to fix this error:

    File "capture.py", line 37, in <module>
     stdscr.move(y,x)
    _curses.error: wmove() returned ERR  

Code:

(irrelevant parts of the code are removed)
import curses

stdscr = curses.initscr()
curses.noecho();

palette = [' ', ' ', '.', '.', '/', 'c', '(', '@', '#', '8']

# index is something between 0 and len(palette), not important 
for x in xrange(50):
    for y in xrange(30):
        stdscr.move(y,x)
        sdtscr.addch(palette[index])
stdscr.refresh()

Solution

  • If you read the documentation for curses move (e.g., http://linux.die.net/man/3/move):

    These routines return ERR upon failure and OK (SVr4 specifies only "an integer value other than ERR") upon successful completion.

    Specifically, they return an error if the window pointer is null, or if the position is outside the window.

    The first doesn't seem likely to come up in Python, so the second is probably your problem. And a quick test shows that running your code works fine on a terminal that's 30 rows or taller, but fails on a typical 24- or 25-row terminal.

    If you want to make it easier to debug, first wrap the whole thing in a try/finally: curses.endscr() (so your terminal isn't left in a mess, possibly making it impossible to see the output). Then wrap the call to stdscr.move in a try/except: that logs x and y so you know where it fails. I'd also make the "30" into a command-line argument for quicker testing. Here's a version with all those changes:

    #!/usr/bin/python
    
    import sys
    import curses
    
    height = int(sys.argv[1]) if len(sys.argv) > 1 else 24
    
    try:
        stdscr = curses.initscr()
        curses.noecho();
    
        palette = [' ', ' ', '.', '.', '/', 'c', '(', '@', '#', '8']
    
        index = 0
        for x in xrange(50):
            for y in xrange(height):
                index = (index + 1) % len(palette)
                try:
                    stdscr.move(y,x)
                except Exception as e:
                    stdscr.refresh()
                    curses.endwin()
                    print
                    print x, y, e
                    sys.exit(1)
                stdscr.addch(palette[index])
        stdscr.refresh()
    finally:
        curses.endwin()
    

    Now python cursetest 30 prints:

    0 25 wmove() returned ERR
    

    So, as I suspected, it's failing at x=0, y=25.

    And if I stretch my terminal out to 80x50, it works, but now python cursetest 60 fails:

    0 50 wmove() returned ERR
    

    For that matter, if I shrink my terminal to 40x50, python cursetest 30 fails at the horizontal edge rather than the vertical:

    40 0 wmove() returned ERR
    

    If you want to check for this in advance rather than trying to catch the error when it happens, try calling getmaxyx() on the window; if y<30 you can display a nice error message and quit (or do something different, or whatever).

    Finally, a quick check shows that this doesn't work in C either. Of course there's no exception thrown, and you can ignore the error that's returned if you want, but you just end up writing to position (24, 49) 300 times in a row. (And you could do the same thing in Python by doing a try/catch/pass around the move, if you really wanted…)