1

I'd like to make a mini game / terminal saver like cmatrix but in the original concept it would use the actual content of the terminal. Is there a way to get the content currently displayed in the terminal where a script is started?

3 Answers 3

4

There is an escape sequence to query the terminal's contents (DECRQCRA). More precisely it reports the checksum of a rectangular area, but by issuing it for each 1x1 rectangle you could probably read back the onscreen contents. Due to its security implications most terminal emulators don't implement it.

However, in order to achieve a cmatrix effect with the onscreen contents, you don't need to read the cells. You could even use DECCARA for changing the attributes (e.g. invisibility, color, boldness) of already existing cells. This is again only supported by a few terminal emulators, e.g. Kitty.

By defining scrolling regions (upper and lower, as well as left and right margins), I guess you should be able to move individual characters vertically. I'm not sure though, I haven't tried. Again, it's probably only supported by a few terminal emulators.

2

Not in a general way, no. Programs interact with a terminal by sending it a stream of commands – they do not have direct access to the character grid.

The communication protocol does have some commands which return certain information back. However, I'm very sure that a command to return the full screen buffer does not exist, and if it were to exist, it would be considered a significant security risk. (For example, ssh'ing to a server would let it see what you did locally before that.) This already happened to something seemingly as minor as being able to query the terminal window's title.

(Contrast this with Windows "console", which always acts as a character buffer and provides official APIs for this.)

3
  • 1
    The more I think about it the more I think "ok... it'd be a major security issue". Too bad. Thanks for the answer!
    – lvictorino
    Sep 20, 2018 at 17:41
  • 1
    You should prefer egmont's answer, due to him having actual knowledge of terminal emulator development. Sep 20, 2018 at 18:13
  • Well, not much about these rarely used weird features :)
    – egmont
    Sep 20, 2018 at 19:41
1

Yes, there's a way to do it; but it requires programming against the NCurses library, or something similar. NCurses is a compatibility layer between the user's program and various different terminal APIs.

As for the attack vector of a script in a remote session being able to access the local terminal, that would be a problem for SSH to deal with, since it's relaying all data between the remote system and the local one — including terminal commands.

2
  • Ah I see. Thanks for your comment and suggestion. However I'd like the script to get the content of the terminal it has been started in. So it wouldn't be in charge of displaying the stuff in the first place :S
    – lvictorino
    Sep 20, 2018 at 17:57
  • Ncurses doesn't really count as it just maintains its own buffer internal to the program, which starts off blank when you start it. That's not quite the same as pre-existing screen contents... Sep 20, 2018 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .