Search code examples
pythoncursespython-curses

curses: how to find the height of written text area


I created a pad in curses then I filled it with a bunch of text. the height of the pad is constant, however, I would like to know how many lines there are in the written part of the pad or the height of it.

rows, cols = std.getmaxyx()
text_win = cur.newpad(rows*3, cols)
text_win.addstr("some stuff")

Solution

  • You can do this by inspecting the result from getyx:

    rows, cols = std.getmaxyx()
    text_win = cur.newpad(rows*3, cols)
    text_win.addstr("some stuff")
    cury, curx = text_win.getyx()
    used_rows = cury + (1 if curx == 0 else 0)
    

    Since the addstr started at the origin, you don't need to call getyx twice. The conditional expression accounts for line-wrapping.