"update command-line output, i.e. for progress" Code Answer

2

This can be done using ANSI Escape Sequences -- see here for a list.

In PHP, you'll use "33" when it's indicated ESC on that page.


In your case, you could use something like this :

echo "Progress :      ";  // 5 characters of padding at the end
for ($i=0 ; $i<=100 ; $i++) {
    echo "33[5D";      // Move 5 characters backward
    echo str_pad($i, 3, ' ', STR_PAD_LEFT) . " %";    // Output is always 5 characters long
    sleep(1);           // wait for a while, so we see the animation
}


I simplified a bit, making sure I always have 5 extra characters, and always displaying the same amount of data, to always move backwards by the same number of chars...

But, of course, you should be able to do much more complicated, if needed ;-)

And there are many other interesting escape sequences : colors, for instance, can enhance your output quite a bit ;-)

By imufun1-06c5e2a49e22 on October 5 2022

Answers related to “update command-line output, i.e. for progress”

Only authorized users can answer the search term. Please sign in first, or register a free account.