| by Arround The Web | No comments

How Does Readline “clearScreenDown()” Work in Node.js?

Node.js comes with a useful “readline” module that reads the entered data from the readable stream and returns it as the output. This module sequentially reads the input stream. It provides a convenient way to take the user input and read it as an output. It performs all of the particular operations with the help of its several pre-defined methods.

All “readline” methods perform special tasks related to their names and functionalities such as “createInterface()” creates a readline interface, “cursorTo()” moves the cursor, “clearLine()” clears the line, and much more.

This blog will provide a detailed view of the readline “clearScreenDown()” method.

How Does Readline “clearScreenDown()” Work in Node.js?

The “clearScreenDown()” is the pre-defined method of the “readline” module that clears the screen based on the mouse cursor position. It clears the output screen below the current position of the mouse cursor. The working of the “clearScreenDown()” method relies on its basic syntax which is written below:

readline.clearScreenDown(stream[, callback])

 
The “clearScreenDown()” method works on the following two parameters:

    • stream: It specifies the writable stream that uses the “process.stdout” property as its argument to clear the output screen.
    • callback: It defines a function that executes after the completion of the specified task.

Return Value: The “clearScreenDown()” method provides a “boolean” value “true” if the output screen becomes clear otherwise “false”.

Now, use the above-defined method practically.

Example: Applying the “clearScreenDown()” Method to Clear the Output Screen

This example applies the “clearScreenDown()” method to clear the output screen below the cursor:

const readline = require("readline");
const rl = readline.createInterface({
 input: process.stdin,
 output: process.stdout
})
rl.question("Name: ", function(a) {
 readline.moveCursor(process.stdout,0,-2);
 readline.clearScreenDown(process.stdout);
 console.log("Hello ", a);
rl.close();
});

 
The explanation of the above code lines is as follows:

    • Firstly, the “require()” method adds the “readline” module to the current Node.js project.
    • Next, the “createInterface()” method specifies the “input” and “output” streams as an object. The “input” stream uses the “process.stdin” property for taking user input and the “output” stream utilizes the “process.stdout” property for reading the input stream.
    • Once all is done, the entered value will be printed as the standard output of the given input stream.
    • After that, the “rl.question()” method takes the user input by specifying the question as the first and the callback function as its second argument.
    • In the definition of the given callback function, the “moveCursor()” method navigates the cursor to the desired location relative to the x and y-axis.
    • The “clearScreenDown()” method clears the output screen below the current position of the cursor.
    • Lastly, the “console.log()” method displays the user-entered value along with the specified string and the “rl.close()” method closes the created Interface.

Note: Create a “.js” file of any name and write the above code lines into it. For instance, we have created “index.js”.

Output

Execute the “index.js” file to see the output of the “clearScreenDown()” method:

node index.js

 
The below output clears the output screen below the cursor’s current position on calling the “clearScreenDown()” method and only prints the entered value:


That’s all about the working of the readline “clearScreenDown()” in Node.js.

Conclusion

The “clearScreenDown()” method works on the “writable stream” to clear the output screen according to the cursor position and the “callback” function that executes once all are done. It clears the output screen that is located below the cursor’s current position. This blog has practically explained the working of the “clearScreenDown()” in Node.js.

Share Button

Source: linuxhint.com

Leave a Reply