使用C中的Curses保存字符串和N行历史记录的连续键盘输入



我想知道是否有人知道我如何修改下面的代码,使我能够等待输入文本条目,将其存储在相应的变量中,并跳到其正下方的下一行,而不删除之前写的内容,除非超过了创建的窗口的大小。

#include <curses.h>
int main()
{
WINDOW *wnd;
char txt[100];
// INICIALIZACIÓN DE LAS VENTANAS.
initscr();
cbreak();
noecho();
refresh();
start_color();
init_pair(1,COLOR_WHITE,COLOR_RED);
wnd = newwin(10,100,1,1);
wattron(wnd,COLOR_PAIR(1));
wbkgd(wnd,COLOR_PAIR(1) | ' ');
wclear(wnd);
box(wnd,ACS_VLINE,ACS_HLINE);
wmove(wnd,1,1);
wprintw(wnd,">> ");
wrefresh(wnd);
wmove(wnd,1,6);
echo();
wgetstr(wnd,txt);
noecho();
return 0;
}

我现在编程的是,一旦检测到第一个intro,就会将值存储在char中,但会关闭ncurses窗口。。。

有人知道我该怎么修才能得到我想要的东西吗?

您需要围绕wgetstr()进行一些循环。所以,忽略你想要什么样的逻辑,像这样:

while (<your condition>)
{
wgetstr(window, text);
<do something with the text you read>
}

您是说当光标位于窗口底部时滚动窗口吗?

#include <ncurses.h> // <-------------- I use ncurses
int main()
{
WINDOW *wnd;
char txt[100];
int  line;
// INICIALIZACIÓN DE LAS VENTANAS.
initscr();
cbreak();
noecho();
refresh();
start_color();
init_pair(1,COLOR_WHITE,COLOR_RED);
wnd = newwin(10,100,1,1);
scrollok(wnd, TRUE);       // <--------- Allow scrolling
wattron(wnd,COLOR_PAIR(1));
wbkgd(wnd,COLOR_PAIR(1) | ' ');
wclear(wnd);
box(wnd,ACS_VLINE,ACS_HLINE);
line = 1; // <------------- First line in the window
while (1) {
wmove(wnd,line,1);
wprintw(wnd,">> ");
wnoutrefresh(wnd);
doupdate(); // <----- Refresh all the preceding modifications
echo();
wgetstr(wnd,txt);
noecho();
if (line < 8) {  // <------- Check bottom of window
line ++;
} else {
box(wnd,COLOR_PAIR(1) | ' ', COLOR_PAIR(1) | ' '); // <--- Clear old box before scrolling
wscrl(wnd, 1); // <------ scroll 1 line up
box(wnd,ACS_VLINE,ACS_HLINE); // <------ Redo the box
}
}
endwin();
return 0;
}

最新更新