我想持久性添加到ncurses的程序:寫最後顯示屏幕上退出磁盤,請閱讀最後從磁盤上的條目顯示屏幕的背面。如果可能,請包含背景和前景色。
問題
- 有沒有辦法從出現在NWindow或NPanel或將我必須保持我自己的緩衝區,基本上讀/寫兩次的ncurses閱讀整個文本塊(以我的緩衝區和ncurses)?
- 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();
}
爲什麼不只是儲存足夠的重建從您的應用程序邏輯的屏幕? – crowder
@crowder我曾經提到過,作爲我的OP中的一個選項(在Q1中) - 如果可能的話,我試圖避免它(畢竟,ncurses已經將所有信息存儲在內存中!)還要注意,存儲我自己的緩衝區本質上是重寫ncurses前端而不實際寫入屏幕。 – kfmfe04
當然,但您必須存儲的信息量可能要少得多,無論如何,您必須重新構建一些上下文來確定應用程序在還原時的狀態。例如,一個「少」風格的應用程序只需要存儲文件偏移量。更好的是,您的自定義存儲信息不會因屏幕大小變化而失效,因爲ncurses「狀態」會。 – crowder