Reputation: 59415
I am trying to write multiple lines to the console in Node.JS then clear all of the lines I wrote.
process.stdout.write(['a', 'b', 'c'].join('\n'))
setInterval(() => {
process.stdout.clearLine(0);
process.stdout.write(['d', 'e', 'f'].join('\n'));
}, 1000)
I've seen some solutions that clear the whole console, I do not want this. I just want to clear what I've written to the screen.
Upvotes: 4
Views: 1673
Reputation: 491
I don't know of there is better way to do this, but what I do is move the cursor up 1 line at the time and than clear that line.
const clearLines = (n) => {
for (let i = 0; i < n; i++) {
//first clear the current line, then clear the previous line
const y = i === 0 ? null : -1
process.stdout.moveCursor(0, y)
process.stdout.clearLine(1)
}
process.stdout.cursorTo(0)
}
Upvotes: 4