【发布时间】:2016-11-09 01:11:46
【问题描述】:
在下面的示例中,我接受了一个假定为有效 .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 的示例,而不仅仅是链接回文档
任何想法为什么滚动不起作用,正如我在文档中看到的那样?
【问题讨论】:
-
如果你提供了一个完整的例子,有人可能会编译它,看看它实际上做了什么。
标签: c++ c++11 scroll terminal ncurses