诅咒:如何找到文字区域的高度

问题描述

我在pad中创建了一个curses,然后在其中填充了一堆文字。垫的高度是恒定的,但是,我想知道垫的书写部分有多少行或垫的高度。

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

解决方法

您可以通过检查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)

由于addstr是从原点开始的,因此您无需两次调用getyx。条件表达式用于换行。