簡體   English   中英

Ncurses不滾動stdscr,導致異常的終端行為

[英]Ncurses not scrolling stdscr, causing abnormal terminal behavior

在下面的示例中,我接收了一個假定為有效.txt文件的命令行arg,並將其輸出到function.cpp中的屏幕。 即使內容比終端高度長,它的讀取和輸出也很好。 但是滾動不起作用,並且Ncurses文檔不存在或太糟糕了。
簡介:我可以運行下面的代碼,但它會破壞終端,我必須強制退出。

void printFile(char fileName[]) {
  string line;
  string cantOpen = "Unable to open file.";
  int key;

  ifstream file;       //Stream to read from
  file.open(fileName); //Specify file to open/read

  initscr();
  scrollok(stdscr, TRUE);  //These lines are the ones I think are causing issues
  idlok(stdscr, TRUE);     //<<<
  keypad(stdscr, TRUE);    //<<<

  if(file.is_open()) {
    while(getline(file, line)) {  //Read file and output it (working fine)
      addstr(line.c_str());
      addch('\n');
      refresh();
    }
    file.close();
  } else {
    addstr(cantOpen.c_str());  //Inform user file wasn't opened
    refresh();
  }

  key = getch();
  if(key == KEY_SF) {            //Scroll down
    wscrl(stdscr, 1);
  } else if(key == KEY_SR) {     //Scroll up
    wscrl(stdscr, -1);
  } else if(key == KEY_ENTER) {  //Enter to exit
    endwin();
  }
}  

我嘗試過的事情:

  • 使用scrl(stdscr,x)代替wscrl(...)
  • 不檢查特定的擊鍵來執行向上或向下或退出
  • 試圖找到滾動stdscr的任何示例,這些示例不僅鏈接回文檔

我在文檔中看到了為什么無法滾動的任何想法?

我認為您誤會了這些例程的目的。 它們的設計目的不是為您提供比您更多的終端空間,它們只是在顯示屏上四處移動文本的一種方式。

scrollok()表示如果您嘗試打印太多行以輸出,它將使用終端的滾動區域(如果可用,則使用硬件,如果需要,則使用軟件)向上滾動區域,從而導致當前滾動區域頂部的數據丟失

wscrl()工作原理類似,導致滾動區域中的文本向上或向下滾動,從而導致數據丟失並用空白填充“新行”。

滾動后,必須將文本寫入新區域。

希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM