2010-09-20 24 views
2

當我在Vim內部並鍵入:ls時,Vim列出緩衝區。最有可能的是使用def_prog_mode()endwin()進入「熟化模式」。我想知道它如何打印這些值。我最喜歡的是使用system("echo ....")這將是相當費力。 我試過printf - 沒有效果,和printwVim如何顯示:ls結果(詛咒/熟化模式)

我需要在我的應用程序中做同樣的事情,而不是創建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 

回答

2

你可能只需要刷新使用fflush標準輸入輸出緩衝。也就是說,使用printf編寫一些文本後,請致電fflush(stdout)以確保文本被髮送到終端。 ncurses可能調用setbufsetvbuf來啓用塊緩衝IO到終端,這不被視爲保存和恢復的tty模式的一部分。

+0

是啊真是棒極了!因爲我實際上使用了ruby並且沒有用C語言寫了18年,所以沒有想到'fflush'。謝謝。 – rahul 2010-09-21 06:40:29

+0

我有這個工作正常。但是,我看到在microemacs中,當鍵入C-x c進入shell時,它不會清除屏幕。由於'endwin()',Vim支持我的編程。如果我刪除'endwin',屏幕不會被清除。但返回後,它仍然滾動。我嘗試了def_shell_mode(???),但是這起來了。那麼如何保持現有的窗戶和麪板顯示並恢復乾淨。謝謝。 – rahul 2010-09-22 08:39:31

+0

好吧,明白了,我們必須停止並重新啓動ncurses。 – rahul 2010-09-22 10:42:37

1

如果調用函數setbuf(標準輸出,NULL)你不必調用fflush(標準輸出),每次...

setbuf以在頭文件stdio.h宣佈