【问题标题】:printing to right side or bottom side of terminal using (n)curses使用 (n)curses 打印到终端的右侧或底部
【发布时间】:2011-09-28 07:47:51
【问题描述】:

使用 n/curses 打印到终端窗口右侧和/或底部的标准方法是什么?

这是一个小草图:

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













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

C 或 Python 中的解决方案都很好。

谢谢!

【问题讨论】:

    标签: python c ncurses curses


    【解决方案1】:

    我编写了这段代码来自动解决这个问题。调用它而不是 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)
    

    【讨论】:

      【解决方案2】:

      我会选择:

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

      这有点不合时宜,这就是我进行大部分诅咒编程的方式。

      【讨论】:

      • 我应该在哪里导入mvprintw
      • 通常在顶部。我认为只是为python“导入诅咒”。已经有十年了。
      【解决方案3】:

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

      一个: move(int row, int col) 来自 ncurses 库。但是,如果您要在此语句之后执行一些 I/O,您将​​无法使用相应的“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");
      

      我相信这个可行。

      祝你好运!

      【讨论】:

      • 你的第二个不是使用 ncurses,它是绕过 ncurses。
      • @ninjalj 这可能有点微妙,但这就是它被列在 #2 的原因。
      • 如何计算 x 和 y?
      猜你喜欢
      • 2018-07-24
      • 1970-01-01
      • 2015-06-12
      • 2010-11-26
      • 1970-01-01
      • 2015-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多