curses 移动光标到非空白位置

curses moving cursor to non blank position

我正在用 C 编写一个简单的文本编辑器,在 cygwin 上使用 curses,现在我有一个充满文本行的屏幕,有些行是部分行,现在当我将光标向上或向下移动一行时,我如果 previous/next 行是部分行,想让它移动到非空白位置,怎么办? 任何帮助将不胜感激。谢谢。

curses(以及ncurses 和PDCurses)都支持winch 函数,它允许应用程序读取存储在当前光标位置的字符。同样,curses cando 的所有版本都将一些字符表示为多个单元格。因此,存储行长并尝试将其用作屏幕上的列号可能会产生令人不满意的结果。

举个例子,您可以这样做(为了简单起见,所有内联且仅针对标准屏幕 stdscr — 实际程序并非如此):

int y, x, xc;
bool partial = TRUE;
getyx(stdscr, y, x);
if (y > 0) {
    y--;
    for (xc = x; xc < COLS; ++xc) {
        move(y, xc);
        if ((inch() & A_CHARTEXT) != ' ') {
            partial = FALSE;
            break; /* found a nonblank cell at or beyond current x */
        }
    }
    if (partial) {
        for (xc = x; xc >= 0; --xc) {
            move(y, xc);
            if ((inch() & A_CHARTEXT) != ' ') {
                break; /* found the last nonblank cell on the line */
            }
        }
    }
}