C++ 如何向ncurses添加持久性?

C++ 如何向ncurses添加持久性?,c++,c,c++11,ncurses,C++,C,C++11,Ncurses,问题 我想为ncurses程序添加持久性:退出时将最后显示的屏幕写入磁盘,进入时从磁盘读取最后显示的屏幕。如果可能,包括背景色和前景色 问题 有没有办法从nWindows或NPanel中显示的ncurses中读取整个文本块,或者我必须维护自己的缓冲区,并且基本上写/读两次(到我的缓冲区和ncurses) 颜色对信息也有同样的问题 回答 是完美的,但我必须做一点实验,以获得正确的呼叫命令 用法 #include <ncurses.h> #include <string.h>

问题

我想为ncurses程序添加持久性:退出时将最后显示的屏幕写入磁盘,进入时从磁盘读取最后显示的屏幕。如果可能,包括背景色和前景色

问题

  • 有没有办法从nWindows或NPanel中显示的ncurses中读取整个文本块,或者我必须维护自己的缓冲区,并且基本上写/读两次(到我的缓冲区和ncurses)
  • 颜色对信息也有同样的问题
  • 回答

    是完美的,但我必须做一点实验,以获得正确的呼叫命令

    用法

    #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();
    }
    
    下面的代码实际上非常适合保存和恢复颜色

    • 在没有参数的情况下运行一次,写出一个屏幕转储文件
      /tmp/scr.dump
    • 使用参数
      read
      再次运行它以读取文件
    代码

    #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();
    }
    
    #包括
    #包括
    中间的无效打印(窗口*win、整型starty、整型startx、整型宽度、字符*字符串);
    int main(int argc,char*argv[])
    {
    bool read_mode=(argc>1&&!strcmp(argv[1],“read”);
    initscr();/*开始诅咒模式*/
    if(has_colors()==FALSE)
    {
    endwin();
    printf(“您的终端不支持颜色\n”);
    返回1;
    }
    开始颜色();/*开始颜色*/
    使用_default_colors();//允许-1表示默认颜色
    初始对(1,颜色为红色,-1);
    如果(读取模式)
    {
    刷新();
    if(scr\u restore(“/tmp/scr.dump”)!=OK)
    {
    fprintf(stderr,“还原期间出错”);
    返回1;
    }
    doupdate();
    attron(颜色对(1));
    在中间打印(stdscr,行/2+9,0,0,“从/tmp/scr.dump读取”);
    attroff(颜色对(1));
    }否则{
    attron(颜色对(1));
    在中间打印字母(标准字符,第/2行,第0行,第0行,“中提琴!!!彩色…”);
    attroff(颜色对(1));
    如果(scr\U转储(“/tmp/scr.dump”)!=OK)
    {
    fprintf(标准“转储时出错”);
    返回1;
    }
    }
    getch();
    endwin();
    }
    中间空白打印(窗口*win、整型starty、整型startx、整型宽度、字符*字符串)
    {int长度,x,y;
    浮子温度;
    if(win==NULL)
    win=stdscr;
    getyx(win,y,x);
    如果(startx!=0)
    x=startx;
    如果(starty!=0)
    y=星形;
    如果(宽度==0)
    宽度=80;
    长度=strlen(字符串);
    温度=(宽度-长度)/2;
    x=startx+(内部)温度;
    mvwprintw(win,y,x,“%s”,字符串);
    刷新();
    }
    
    请参见
    手动scr\u转储

       scr_dump, scr_restore, scr_init, scr_set -
           read (write) a curses screen from (to) a file
    

    为什么不存储足够的数据,以便从应用程序逻辑重建屏幕?@crowder我在我的OP中提到过这一点(在Q1下)-如果可能的话,我会尽量避免(毕竟,ncurses已经在内存中存储了所有信息!)还请注意,存储我自己的缓冲区本质上是对ncurses前端的重写,而不是实际写入屏幕。当然,但您必须存储的信息量可能要少得多,而且您必须重建一些上下文,以确定应用程序在还原时处于何种状态。例如,“更少”风格的应用程序只需要存储文件偏移量。更好的是,你的自定义存储信息不会因为屏幕大小的改变而失效,就像ncurses“state”那样。@crowder我尽量少写新代码,因为我对bug过敏-但是nm,看起来下面的rici有一个很好的解决方案…这个解决方案非常完美-我为OP中的其他人添加了代码,他们可能会发现它很有用