[英]how to add persistence to ncurses?
PROBLEM 问题
I would like to add persistence to a ncurses program: write the last displayed screen to disk on exit, read that last displayed screen back from disk on entry . 我想在ncurses程序中添加持久性: 在退出时将最后显示的屏幕写入磁盘,在输入时从磁盘读回最后显示的屏幕 。 If possible, include the background and foreground colors.
如果可能,请包括背景和前景色。
QUESTION 题
ANSWER 回答
Rici's answer below is perfect, but I had to experiment a little to get the call orders right. Rici在下面的回答是完美的,但我必须尝试一点才能获得正确的通话订单。
USAGE 用法
The code below actually works great for saving and restoring color. 下面的代码实际上非常适合保存和恢复颜色。
/tmp/scr.dump
. /tmp/scr.dump
。 read
to read from the file. read
再次运行它以从文件中读取。 CODE 码
#include <ncurses.h>
#include <string.h>
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string);
int main(int argc, char *argv[])
{
bool read_mode = ( argc>1 && !strcmp( argv[1], "read" ));
initscr(); /* Start curses mode */
if(has_colors() == FALSE)
{
endwin();
printf("Your terminal does not support color\n");
return 1;
}
start_color(); /* Start color */
use_default_colors(); // allow for -1 to mean default color
init_pair(1, COLOR_RED, -1);
if ( read_mode )
{
refresh();
if ( scr_restore( "/tmp/scr.dump" )!=OK )
{
fprintf( stderr, "ERROR DURING RESTORE\n" );
return 1;
}
doupdate();
attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2 + 9, 0, 0, "Read from /tmp/scr.dump" );
attroff(COLOR_PAIR(1));
} else {
attron(COLOR_PAIR(1));
print_in_middle(stdscr, LINES / 2, 0, 0, "Viola !!! In color ...");
attroff(COLOR_PAIR(1));
if ( scr_dump( "/tmp/scr.dump" )!=OK )
{
fprintf( stderr, "ERROR WHILE DUMPING" );
return 1;
}
}
getch();
endwin();
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string)
{ int length, x, y;
float temp;
if(win == NULL)
win = stdscr;
getyx(win, y, x);
if(startx != 0)
x = startx;
if(starty != 0)
y = starty;
if(width == 0)
width = 80;
length = strlen(string);
temp = (width - length)/ 2;
x = startx + (int)temp;
mvwprintw(win, y, x, "%s", string);
refresh();
}
See man scr_dump
: 见
man scr_dump
:
scr_dump, scr_restore, scr_init, scr_set -
read (write) a curses screen from (to) a file
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.