2013-06-28 62 views
-1

問題如何添加持久化到ncurses?

我想持久性添加到ncurses的程序:寫最後顯示屏幕上退出磁盤,請閱讀最後從磁盤上的條目顯示屏幕的背面。如果可能,請包含背景和前景色。

問題

  1. 有沒有辦法從出現在NWindow或NPanel或將我必須保持我自己的緩衝區,基本上讀/寫兩次的ncurses閱讀整個文本塊(以我的緩衝區和ncurses)?
  2. COLOR_PAIR信息的同樣問題。

ANSWER

Rici's answer below是完美的,但我不得不嘗試一點點地得到調用單吧。

用法

下面的代碼實際上的偉大工程保存和恢復的顏色。

  • 運行一次不帶參數寫出來的屏幕轉儲文件/tmp/scr.dump
  • 用參數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(); 
} 
+0

爲什麼不只是儲存足夠的重建從您的應用程序邏輯的屏幕? – crowder

+0

@crowder我曾經提到過,作爲我的OP中的一個選項(在Q1中) - 如果可能的話,我試圖避免它(畢竟,ncurses已經將所有信息存儲在內存中!)還要注意,存儲我自己的緩衝區本質上是重寫ncurses前端而不實際寫入屏幕。 – kfmfe04

+0

當然,但您必須存儲的信息量可能要少得多,無論如何,您必須重新構建一些上下文來確定應用程序在還原時的狀態。例如,一個「少」風格的應用程序只需要存儲文件偏移量。更好的是,您的自定義存儲信息不會因屏幕大小變化而失效,因爲ncurses「狀態」會。 – crowder

回答

2

man scr_dump

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

+1優秀 - TYVM - 我認爲這可能就是我需要 – kfmfe04

+1

這個解決方案是完美的 - 我已經將代碼添加到OP,供其他可能會發現它的人使用 – kfmfe04