使用(n)curses在终端右侧或底部打印

4 投票
3 回答
2610 浏览
提问于 2025-04-16 20:49

在使用n/curses的时候,怎么才能把内容打印到终端窗口的右边或者底部呢?

这里有个简单的示意:

Terminal window:
================================================================================
                                                                         [ MSG ]













message number 2                                                   here is more
================================================================================

用C语言或者Python都可以。

谢谢!

3 个回答

0

我写了这段代码来自动解决这个问题。你可以用它来替代scr.addstr(),这样它就会自动处理正确的addstr和insstr命令,让它正常工作。

def cwrite(scr, row, col, str, attr=0):
    max = scr.getmaxyx()
    if row < max[0] - 1:
        scr.addstr(row, col, str, attr)
    elif row == max[0] - 1:
        if len(str) + col >= max[1]:
            offset = max[1] - col - 2
            scr.addstr(row, col, str[:offset])
            scr.addstr(row, max[1] - 2, str[offset + 1], attr)
            scr.insstr(row, max[1] - 1, str[offset], attr)
        else:
            scr.addstr(row, col, str, attr)
1

我知道有两种方法,但只有一种我比较确定:

第一种:move(int row, int col) 这个是来自ncurses库的。不过,如果你在这条语句后面要进行一些输入输出操作,使用相应的'mv'函数会更好。例如,

move(y, x);
addch(ch);

可以用下面的代码替换:

mvaddch(y, x, ch);

注意:我只是听说过这个,但自己没有测试过。

第二种:

printf("\033[%d;%df", y, x);
fflush(stdout);
printf("Hello, I will be placed at (x,y)\n");

我确定这个方法是有效的。

祝你好运!

3

我会选择:

mvprintw(COLS-length("msg"),1,"msg");
mvprintw(0,LINES-1,"message number 2");
mvprintw(COLS-length("here is more"),LINES-1,"here is more");

这有点随意,其实我大多数的编程都是这样随便写的。

撰写回答