How lawful renders its respect progress bar?

How lawful renders its respect progress bar?

This day while working an lawful elephantine-toughen I asked myself how lawful
does this nice progress bar caught on the bottom line while smooth
writing scrolling text.

We wanted no more with a coworker to purchase a gaze at to reproduce it!

apt autoremove gif

Fortunately, while being very bored in the tube a number of years aid, I
wrote a Headless VT100
emulator
, so I
remembered some things and had a number of hints on the tactic it’d be completed, so
I started poking around with some Python code.

I remembered a number of instructions like
DECSTBM to scheme the
margins, and the diversified commands to drag the cursor up and down so I
started with this.

After some trials-and-errors bottom margin reservation and log
printing worked however the log were displayed on the toll road come the bottom
margin, now not the place the present started: survey possibilities are you’ll perchance shuffle an lawful
toughen
on the discontinuance of your terminal, it displays the progress bar at
the bottom, however the logs will delivery from the discontinuance, as if there grasp been no
progress bar.

While looking out to solve this with my coworker, we were discussing about
my implementation, and a random characteristic:

static void DECSC(struct lw_terminal *term_emul)
{
    /*TODO: Set 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 quickly we realized it changed into as soon as the missing fragment! Saving the cursor situation to restore it later!!

It soon began to survey like an grotesque undocumented, but practically working, … thing?

print(f"33733[0;{lines-1}r338")
strive: 
    for i in differ(250): 
        time.sleep(0.2)
        print("Hi there world", i)
        print(
            f"33733[{lines};0f", datetime.now().isoformat(), "338", sep="", discontinuance=""
        )
excluding KeyboardInterrupt: 
    drag
eventually: 
    print(f"33733[0;{lines}r33[{lines};0f33[0K338")

But wow, f"33733[0;{lines}r33[{lines};0f33[0K338" can also merely smooth
really be made more readable, it delivery to bother my eyes. I had to drag
and let it as is for a evening.

This day I am aid at it any other time, and tried to add some feedback, and delay
to really survey the tactic it behave microscopic by microscopic:

import time
import os
from datetime import datetime

columns, lines = os.get_terminal_size()


def write(s): 
    print(s, discontinuance="")
    time.sleep(1)


write("n")  # Discover certain the closing line is available.
write("337")  # Set cursor situation
write(f"33[0;{lines-1}r")  # Reserve the bottom line
write("338")  # Restore the cursor situation
write("33[1A")  # Transfer up one line

strive: 
    for i in differ(250): 
        time.sleep(0.2)
        write(f"Hi there {i}")
        write("337")  # Set cursor situation
        write(f"33[{lines};0f")  # Transfer cursor to the bottom margin
        write(datetime.now().isoformat())  # Write the date
        write("338")  # Restore cursor situation
        write("n")
excluding KeyboardInterrupt: 
    drag
eventually: 
    write("337")  # Set cursor situation
    write(f"33[0;{lines}r")  # Fall margin reservation
    write(f"33[{lines};0f")  # Transfer the cursor to the bottom line
    write("33[0K")  # Properly-organized that line
    write("338")  # Restore cursor situation

For the file here’s what’s extinct (33 is ESC):

  • ESC 7 is DECSC (Set Cursor)
  • ESC 8 is DECRC (Restore Cursor)
  • ESC [ Pn ; Pn r is DECSTBM (Residing High and Backside Margins)
  • ESC [ Pn A is CUU (Cursor Up)
  • ESC [ Pn ; Pn f is HVP (Horizontal and Vertical Space)
  • ESC [ Ps Ok is EL (Erase In Line)

Invent now not neglect the -u (unbuffered) Python flag even as you happen to need to survey it microscopic by microscopic:

progress bar started at the top

Began from the bottom so we survey it scroll:

progress bar started at the bottom

Undercover agent how on interruption (or fashioned exit) it cleans the progress bar
sooner than exiting, restoring the cursor on the correct space.

But hiya, shall we grasp be taught the lawful code!

Sure, it changed into as soon as less bright, but now that it really works, now we want to purchase a gaze at it!

So I lawful-score source lawful and chanced on, in install-progress.cc:

// scroll down a microscopic to lead clear of visual glitch when the shroud shroud
// situation shrinks by one row
std:: cout << "n";

// place cursor
std:: cout << "337";

// scheme scroll score 22 situation (this can also merely space the cursor in the discontinuance left)
std:: cout << "33[0;" << std:: to_string(nr_rows - 1) << "r";

// restore cursor but score certain its contained in the scrolling situation
std:: cout << "338";
static const char *move_cursor_up = "33[1A";
std:: cout << move_cursor_up;

Already appears to be like to be to be like acquainted to you?

Read More

Share your love