I'm asking again the question that was asked on StackOverflow with no answers I event added a bounty without an effect (I've just deleted that one). I think that people here may have better knowledge about what is going on.
I'm working on a better ANSI escape code interpreter for my JavaScript library but having a problem with processing the top command (using a script command that dumps terminal output into a file).
I have a sample file from a MacOS that use alternative screen ESC [ ? 1049 h and l as documented here.
The problem is that the Linux version of the top command doesn't use an alternative screen only: \e[H\e[2J sequence which is: goto 1,1 and clear the screen. From my understanding, this should literally wipe everything that was printed on the terminal before that command. This is what I do in my parser/interpreter. But this is not what the Linux terminal is doing (I use an XFce terminal). It somehow clears part of the screen and scrolls down the page.
So my question is what are the exact steps (algorithm) to process the top command (\e[H\e[2J sequence) to render it like on a Linux terminal? I need to have a single string as the output of the session as it looks on my terminal.
Here is a sample output of the script command: https://jcubic.pl/screen_dump_linux_top.txt
I have testing code on CodePen:
that use this code:
$.terminal.format($.terminal.overtyping($.terminal.from_ansi(text))); if you look at the output of the top command on Linux. You will see that there is no output before the command. In comparison to the macOS file that prints everything (I know that the real terminal doesn't print an alternative screen when switching back to the primary screen, but I decided that I want that output there, maybe I add an option to toggle that).
I know that I can just check the terminal emulator source code, but it will probably take a lot more time, than just asking this.