2

当我在 Vim 中输入 时:ls,Vim 会列出缓冲区。很可能它正在使用def_prog_mode()and进入“熟模式” endwin()。我想知道它是如何打印这些值的。我得出的最好的结果是使用system("echo ....")它会非常费力。我试过了printf- 没有效果,而且printw.

我需要在我的应用程序中做同样的事情,而不是创建 Windows 或弹出窗口,我想像 Vim 那样列出内部信息。

这是我尝试过的示例,来自http://gist.github.com/587622

#include <ncurses.h>

// it seems system echo is the only way to print some stuff in cooked mode
// i am trying to figure out how VIM displays the result of :ls

int main()
{       
        initscr();                      /* Start curses mode              */
        printw("Hello World !!! Hit a key\n");    /* Print Hello World              */
        refresh();                      /* Print it on to the real screen */
        getch();
        def_prog_mode();                /* Save the tty modes             */
        endwin();                       /* End curses mode temporarily    */
        int i = 0;
        for (i = 0; i < 5; i++) {
            system("echo inside cooked mode");
        }
        //printf("helllow there\n");
        //system("/bin/ls");              /* Do whatever you like in cooked mode */
        //system("read");
        //system("/bin/sh");              /* Do whatever you like in cooked mode */
        //system("echo hit a key");              /* Do whatever you like in cooked mode */
        //printw("Hit a key buddy\n");    /* Print Hello World              */
        reset_prog_mode();              /* Return to the previous tty mode*/
        getch();
                                        /* stored by def_prog_mode()      */
        refresh();                      /* Do refresh() to restore the    */
                                        /* Screen contents                */
        printw("After cooked mode.\nKey to quit");     /* Back to curses use the full    */
        getch();
        refresh();                      /* capabilities of curses         */
        endwin();                       /* End curses mode                */

        return 0;
}

编译它使用:

gcc -o a.out -lncurses a.c && ./a.out
4

2 回答 2

2

您可能只需要使用fflush. 也就是说,在使用printf写一些文本之后,调用fflush(stdout)以确保文本被发送到终端。ncurses 可能正在调用setbufsetvbuf启用到终端的块缓冲 IO,这不被视为保存和恢复的 tty 模式的一部分。

于 2010-09-20T21:47:11.910 回答
1

如果您调用 setbuf(stdout, NULL) 您不必每次都调用 fflush(stdout) ...

setbuf 在头文件 stdio.h 中声明

于 2011-03-21T19:18:51.063 回答