Today while running an apt full-upgrade
I asked myself how apt
does this nice progress bar stuck at the bottom line while still
writing scrolling text.
We needed no more with a coworker to try to reproduce it!
Fortunately, while being very bored in the tube a few years back, I
wrote a Headless VT100
emulator, so I
remembered some things and had a few hints on how it could be done, so
I started poking around with some Python code.
I remembered a few instructions like
DECSTBM to set the
margins, and the various commands to move the cursor up and down so I
started with this.
After some trials-and-errors bottom margin reservation and log
printing worked but the log were displayed on the line near the bottom
margin, not where the command started: notice you can run an apt
on the top of your terminal, it displays the progress bar at
upgrade
the bottom, but the logs will start from the top, as if there were no
progress bar.
While trying to solve this with my coworker, we were discussing about
my implementation, and looking at a random function:
static void DECSC(struct lw_terminal *term_emul)
{
/*TODO: Save graphic rendition and charset.*/
struct lw_terminal_vt100 *vt100;
vt100 = (struct lw_terminal_vt100 *)term_emul->user_data;
vt100->saved_x = vt100->x;
vt100->saved_y = vt100->y;
}
and soon we realized it was the missing piece! Saving the cursor position to restore it later!!
It soon started to look like an ugly undocumented, but almost working, … thing?
print(f"