get the text in the display with ncurses
Asked Answered
C

3

5

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..

Churr answered 17/6, 2010 at 19:51 Comment(1)
the answer is still correct and should be accepted :-)Scarlettscarp
H
12

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.

Hesperidin answered 21/6, 2010 at 23:29 Comment(0)
D
3

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:

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:

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".

Deist answered 16/11, 2017 at 9:52 Comment(0)
H
0

As mentioned in the other answers, you can use the inch(int) or the mvinch(int, int) methods and & them with A_CHARTEXT

Here is some example code that was missing from the previous answers

int row = 0;
char buffer[101];

/* Read first 100 characters from row 0 */
for (int i = 0; i < 100; ++i) {
    buffer[i] = mvinch(row, i) & A_CHARTEXT;
}
buffer[100] = '\0';
Hidrosis answered 26/9, 2024 at 15:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.