curses fails when calling addch on the bottom right corner
For the future readers. After the @Thomas Dickey answer, I have added the following snippet to my code.
try:
screen.addch(mlines, mcols, 'c')
except _curses.error as e:
pass
Now my code looks like:
import curses
import _curses
def do_curses(screen):
curses.noecho()
curses.curs_set(0)
screen.keypad(1)
(line, col) = 12, 0
screen.addstr(line, col, "Hello world!")
line += 1
screen.addstr(line, col, "Hello world!", curses.A_REVERSE)
screen.addch(0, 0, "c")
(mlines, mcols) = screen.getmaxyx()
mlines -= 1
mcols -= 1
try:
screen.addch(mlines, mcols, 'c')
except _curses.error as e:
pass
while True:
event = screen.getch()
if event == ord("q"):
break
curses.endwin()
if __name__ == "__main__":
curses.wrapper(do_curses)
That is expected behavior (a quirk) because addch
attempts to wrap to the next line after adding a character. There is a comment in lib_addch.c dealing with this:
/*
* The _WRAPPED flag is useful only for telling an application that we've just
* wrapped the cursor. We don't do anything with this flag except set it when
* wrapping, and clear it whenever we move the cursor. If we try to wrap at
* the lower-right corner of a window, we cannot move the cursor (since that
* wouldn't be legal). So we return an error (which is what SVr4 does).
* Unlike SVr4, we can successfully add a character to the lower-right corner
* (Solaris 2.6 does this also, however).
*/
window.insch(...)
can place a character at the lower right of a window without advancing the cursor. Any character at that position will be bumped to the right without causing an error.