开发者

get the text in the display with ncurses

Is there开发者_开发问答 any way to get back the characters outputted into a variable on ncurses ?

let's say I do:

printw("test");

then I want to be able to:

somefunc(strbuffer);
printf("%s",strbuffer); // test

I need a function to get back all characters on the screen into a variable, scr_dump get's close but the output format is unreadable..


If you put stuff on the screen using curses functions (e.g. addch, mvaddch, addstr) you can use inchstr) and related functions to read the characters from the screen (extracting them with AND'ing the returned value with A_CHARTEXT).

However, if you use printf or any other non-curses method of puting text on the screen (including a system call to another program that uses curses) you will not be able to read the content of the screen.

Curses maintains the current screen contents internally and the inchstr functions use the internal representation of the screen to find the current contents.


There are two sets of functions for retrieving data from the screen. If your printw uses only (as in the question) text which is represented as an 8-bit encoding (ASCII, POSIX, ISO-8859-1), then inch and inchstr work:

  • inch retrieves a single cell along with its attributes
  • inchstr retrieves multiple cells along with their attributes

or more simply using instr and its variations. These functions return the data without additional need for masking the attributes from the characters.

However, if the data uses a multibyte encoding (such as UTF-8), then you must use a different interface for retrieving the characters. These are the equivalents of inch and inchstr:

  • in_wch, etc. - extract a complex character and rendition from a window

  • in_wchstr, etc. - get an array of complex characters and renditions from a curses window

A complex character is a structure, which X/Open Curses treats as opaque. You must use getcchar to extract data (such as a wide-character string) from each cell's data.

A (little) more simply, you can read the wide-character string information from a window:

  • inwstr, etc. - get a string of wchar_t characters from a curses window

  • there is no single-character form; you must retrieve data as a one-character string.

In summary, while your application can put data as an array of char (or individual chtype values), in a UTF-8 environment it must retrieve it as complex characters or wide-characters. If you happen to be using Linux, you can generally treat wchar_t as Unicode values. Given data as an array of wchar_t values, you would use other (non-curses) functions to obtain a multibyte (UTF-8) string.

Since the question said ncurses rather than simply curses, it's appropriate to point out that applications using ncurses can differ from X/Open Curses in the way they put data on the screen (which can affect your expectations about retrieving it). In ncurses, addch (and similar char-oriented functions) will handle bytes in a multi-byte string such as UTF-8, storing the result as wide-characters. None of the other X/Open Curses implementations to date do this. The others treat those bytes as independent, and may represent them as invalid wide-characters.

By the way, since the question was asked in 2010, ncurses' scr_dump format has been extended, making it "readable".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜