[英]C - keeping printed terminal output in place
I have a simple c program that is first printing information related to the users machine: 我有一个简单的c程序,它首先打印与用户计算机有关的信息:
Header Info 1
Header Info 2
Header Info 3
I then print columns of data related to the above: 然后,我打印与上述相关的数据列:
XX XX XX XX XX
XX XX XX XX XX
XX XX XX XX XX
XX XX XX XX XX
...
What I would like is to keep the top header information in place, then have the rest of the data print under it continuously. 我想要的是保留最前面的标题信息,然后将其余的数据连续打印在下面。 This way the header info does not scroll off the top of the screen. 这样,标题信息就不会滚动到屏幕顶部之外。 What is the easiest way to accomplish this? 最简单的方法是什么?
EDIT: I am on linux. 编辑:我在linux上。
Just based on your general description, you can do something like this. 仅根据您的一般描述,您可以执行以下操作。
#include <stdlib.h>
#include <signal.h>
#include <curses.h>
static int quit_flag;
void sigint_handler (int sig) {
quit_flag = 1;
}
int main () {
int i;
WINDOW *w;
signal(SIGINT, sigint_handler);
w = initscr();
scrollok(w, 1);
wsetscrreg(w, 4, LINES-1);
wprintw(w, "%s\n", "Info 1");
wprintw(w, "%s\n", "Info 2");
wprintw(w, "%s\n", "Info 3");
wprintw(w, "%s\n", "Info 4");
wrefresh(w);
i = 0;
wsetscrreg(w, 4, LINES-1);
while (++i) {
wprintw(w, "%d\n", i);
wrefresh(w);
if (quit_flag) break;
}
endwin();
return 0;
}
The suggested answer has a couple of problems: 建议的答案有两个问题:
initscr
manual page. 请参阅initscr
手册页中有关信号处理程序的部分。 This is a corrected version: 这是一个更正的版本:
#include <curses.h>
int main (void) {
int i;
WINDOW *w;
w = initscr();
scrollok(w, 1);
wsetscrreg(w, 4, LINES-1);
wprintw(w, "%s\n", "Info 1");
wprintw(w, "%s\n", "Info 2");
wprintw(w, "%s\n", "Info 3");
wprintw(w, "%s\n", "Info 4");
wrefresh(w);
i = 0;
while (++i) {
wprintw(w, "%d\n", i);
wrefresh(w);
}
endwin();
return 0;
}
Further, realizing that the return value from initscr
is stdscr
, the program can be simplified: 此外,认识到initscr
的返回值为stdscr
,可以简化程序:
#include <curses.h>
int main (void) {
int i;
initscr();
scrollok(stdscr, 1);
setscrreg(4, LINES-1);
printw("%s\n", "Info 1");
printw("%s\n", "Info 2");
printw("%s\n", "Info 3");
printw("%s\n", "Info 4");
refresh();
i = 0;
while (++i) {
printw("%d\n", i);
refresh();
}
endwin();
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.