Consider the following code (written in PHP, but that's not important. Answers may use pure Bash). The goal is to fill a terminal line with characters for the entire width of the line.
Now, if you minimise the terminal width during execution, excess characters from the previous time have to be cleared out to avoid a mess.
However, then, the previously echoed line will have been split into multiple lines, and echoing
\r will return the cursor to the last of those lines, rather than the first, leaving behind garbage.
Is there any simple cure to this? The only thing I can think of is echoing the ANSI code for "CURSOR UP" as many times as the line was broken (minus one).
#!/usr/bin/env php <?php // Make sure window is 80 characters wide when running script echo "00000000001111111111222222222233333333334444444444555555555566666666667777777777"; sleep(3); // Narrow window here (to 40-50 characters or so) echo "\033[2K"; // CLEAR LINE echo "\033[A"; // CURSOR UP echo "\033[2K"; // CLEAR LINE echo "\r"; // GOTO BEGINNING OF LINE echo "Now we're back were we begun (where 000... used to be)"; ?>