简体   繁体   中英

Ncurses not scrolling stdscr, causing abnormal terminal behavior

In the example below, I take in a command line arg that is assumed to be a valid .txt file, and output it to the screen in function.cpp. It reads and outputs just fine - even if the content is longer than the terminal height. But scrolling doesn't work, and Ncurses documentation is either nonexistant or horrible.
Summary: I can run the code below, but it just breaks the terminal and I have to force quit.

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();
  }
}  

Things I've tried:

  • Using scrl(stdscr, x) instead of wscrl(...)
  • Not checking for specific key strokes to perform up or down or exit
  • Tried finding any examples of scrolling the stdscr that don't just link back to the documentation

Any ideas why scrolling isn't working as I see it described in documentation?

I think you are misunderstanding the purpose of these routines. They are not designed to give you more terminal space than you have -- they are only a way to move text around on the display.

scrollok() means that if you try to print too many lines to output, it will use the terminal's scroll region (hardware if available, software if necessary) to scroll the region up, causing loss of data at the top of the current scroll region .

wscrl() works similarly, causing the text in the scroll region to scroll up or down, causing loss of data and filling the 'new lines' with blanks.

Once you have scrolled, you must write text to the new areas.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM