Reputation: 1531
I know how I can update the last line of output (answered here).
I've noticed though that some programs update more than just the last line.
Here is an example from yarn's output:
All the waiting...
lines have a "rotating dot" at the beginning of them, none of which is in the last line.
How can one achieve this?
Upvotes: 0
Views: 343
Reputation: 4194
You can use ANSI Escape sequences to move the cursor...
Example : printing "\033[3A"
will make the cursor move three lines up
If I have understood what you want... This should help:
var writeEscape = a => process.stdout.write("\033[" + a);
var moveUp = n => writeEscape(n +"A")
var moveDown = n => writeEscape(n + "B")
var moveRight = n => writeEscape(n +"C")
var moveLeft = n => writeEscape(n + "D")
var waitFor = n => new Promise(r => setTimeout(r,n))
writeEscape("25l"); // hide cursor use writeEscape("","25h") to show it again
process.stdout.write("waiting\n")
process.stdout.write("some other text");
moveUp(1)
moveLeft(15) // "some other text".length = 15
var printWaiting = async() => {
writeEscape("K"); // erase the whole line
process.stdout.write("waiting")
await waitFor(1000)
process.stdout.write(".")
await waitFor(1000)
process.stdout.write(".")
await waitFor(1000)
process.stdout.write(".")
moveLeft(10) // "waiting".length = 7, "...".length = 3
await waitFor(1000)
printWaiting()
}
printWaiting();
I have used async/await functions... But setTimeout only will also work...
Upvotes: 1