如何在 ncurses 中滚动 window(stdscreen 除外)?
How to scroll a window (other than stdscreen) in ncurses?
我看到这个答案正在研究解决我的问题 ,但是,它只适用于 stdscreen。我实现了这个:
#include <ncurses.h>
int main(void)
{
int i = 2, height, width;
WINDOW *new;
initscr();
getmaxyx(stdscr, height, width);
new = newwin(height - 2, width - 2, 1, 1);
scrollok(new,TRUE);
while(1)
{
mvwprintw(new, i, 2, "%d - lots and lots of lines flowing down the terminal", i);
++i;
wrefresh(new);
}
endwin();
return 0;
}
但它不滚动。怎么了?
那是因为你使用 mvwprintw
将字符串放在 window 中的某个位置,所以当 i
大于 window 大小时,它就不会打印在屏幕上。
为了使用 scolling,您需要使用 wprintw
将文本放在当前光标位置。
#include <ncurses.h>
int main(void)
{
int i = 2, height, width;
WINDOW *new;
initscr();
getmaxyx(stdscr, height, width);
new = newwin(height - 2, width - 2, 1, 1);
scrollok(new,TRUE);
while(1)
{
wprintw(new, "%d - lots and lots of lines flowing down the terminal\n", i);
++i;
wrefresh(new);
}
endwin();
return 0;
}
如果你想用内容填充window然后使用箭头键上下滚动,你应该看看
mvprintw
函数首先尝试将光标移动到指示的位置,例如 wmove
。 wmove
函数永远不会导致滚动,并且尝试将其移过 window 的底线失败(引用自 wmove
手册):
These routines return ERR upon failure and OK (SVr4 specifies only "an integer value other than ERR") upon successful completion.
Specifically, they return an error if the window pointer
is null, or if the position is outside the window.
相反,要进行滚动,您必须在 window 的底部编写带有 换行符 字符(即 '\n'
)的文本。 wprintw
有用;依次调用 waddch
(引用后者的手册):
The addch, waddch, mvaddch and mvwaddch routines put the
character ch into the given window at its current window
position, which is then advanced. They are analogous to
putchar in stdio(3). If the advance is at the right margin:
...
At the bottom of the current scrolling region, and if
scrollok is enabled, the scrolling region is scrolled
up one line.
If ch is a tab, newline, or backspace, the cursor is moved appropriately within the window:
...
Newline does a clrtoeol, then moves the cursor to the
window left margin on the next line, scrolling the
window if on the last line.
我看到这个答案正在研究解决我的问题 ,但是,它只适用于 stdscreen。我实现了这个:
#include <ncurses.h>
int main(void)
{
int i = 2, height, width;
WINDOW *new;
initscr();
getmaxyx(stdscr, height, width);
new = newwin(height - 2, width - 2, 1, 1);
scrollok(new,TRUE);
while(1)
{
mvwprintw(new, i, 2, "%d - lots and lots of lines flowing down the terminal", i);
++i;
wrefresh(new);
}
endwin();
return 0;
}
但它不滚动。怎么了?
那是因为你使用 mvwprintw
将字符串放在 window 中的某个位置,所以当 i
大于 window 大小时,它就不会打印在屏幕上。
为了使用 scolling,您需要使用 wprintw
将文本放在当前光标位置。
#include <ncurses.h>
int main(void)
{
int i = 2, height, width;
WINDOW *new;
initscr();
getmaxyx(stdscr, height, width);
new = newwin(height - 2, width - 2, 1, 1);
scrollok(new,TRUE);
while(1)
{
wprintw(new, "%d - lots and lots of lines flowing down the terminal\n", i);
++i;
wrefresh(new);
}
endwin();
return 0;
}
如果你想用内容填充window然后使用箭头键上下滚动,你应该看看
mvprintw
函数首先尝试将光标移动到指示的位置,例如 wmove
。 wmove
函数永远不会导致滚动,并且尝试将其移过 window 的底线失败(引用自 wmove
手册):
These routines return ERR upon failure and OK (SVr4 specifies only "an integer value other than ERR") upon successful completion.
Specifically, they return an error if the window pointer is null, or if the position is outside the window.
相反,要进行滚动,您必须在 window 的底部编写带有 换行符 字符(即 '\n'
)的文本。 wprintw
有用;依次调用 waddch
(引用后者的手册):
The addch, waddch, mvaddch and mvwaddch routines put the character ch into the given window at its current window position, which is then advanced. They are analogous to putchar in stdio(3). If the advance is at the right margin:
...
At the bottom of the current scrolling region, and if scrollok is enabled, the scrolling region is scrolled up one line.
If ch is a tab, newline, or backspace, the cursor is moved appropriately within the window:
...
Newline does a clrtoeol, then moves the cursor to the window left margin on the next line, scrolling the window if on the last line.