简体   繁体   English

如何为ncurses添加持久性?

[英]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

  1. Is there a way to read the entire block of text from ncurses that appears in a NWindow or NPanel or will I have to maintain my own buffer and essentially write/read twice (to my buffer and to ncurses)? 有没有办法从NWindow或NPanel中出现的ncurses读取整个文本块,或者我是否必须维护自己的缓冲区并基本上写入/读取两次(到我的缓冲区和ncurses)?
  2. Same question for COLOR_PAIR information. COLOR_PAIR信息的相同问题。

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. 下面的代码实际上非常适合保存和恢复颜色。

  • Run it once without arguments to write out a screen dump file /tmp/scr.dump . 无参数运行一次,写出屏幕转储文件/tmp/scr.dump
  • Run it again with the argument 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM