In NodeJS, one can use the readline.clearScreenDown()
method to clear the screen based on the position of the cursor. This method clears the screen below the current position of the cursor.
readline.clearScreenDown(stream[, callback])
stream
: It refers to <stream.writable>
. Mostly process.stdout
is passed as the parameter because we want to clear the output screen.
callback
: When the operation completes, readline.clearScreenDown
invokes a callback function.
true
if the screen is cleared and write object becomes clear. Returns false
otherwise.Let’s have a look at the implementation of this method.
You can change the cursor position using
readline.movecursor()
method.
// require readlineconst readline = require("readline");// create an interfaceconst rl = readline.createInterface(process.stdin, process.stdout);// run a question. On Calling clearScreenDown() method// the terminal clears the screen andremoves this question, and only// prints the answer.rl.question("What is your Age? ", function(a) {readline.moveCursor(process.stdout,0,-2);readline.clearScreenDown(process.stdout);console.log("Hey, your Age is", a);rl.close();});
One of the applications of clearScreenDown()
is to clear the entire screen by moving the cursor to first position (index 0).
const clearScreen = () => {
// values is the number of lines that cursor needs to move.
readline.cursorTo(process.stdout, 0, values)
readline.clearScreenDown(process.stdout)
}
Free Resources