Tuesday 2 October 2018

node.js: Exploring readline module

‘readline’ module provides an interface for reading data from a readable stream, one line at a time.

Let’s write a simple application, that reads data from standard input and write data to standard output.

Step 1: Import the ‘readline’ module.
var readline = require('readline');

Step 2: Create a new readline.Interface instance that reads data from standard input and write data to standard output.
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

Step 3: Ask the question and read the answer from user.
rl.question('What is your name? ', (answer) => {
  console.log(`Hello: ${answer}, How are you?`);
  rl.close();
});

First argument of question function, displays the question by writing it to the standard output and waits for the user input. Once user enters the input, it invokes the callback function by passing the user input as argument.

HelloWorld.js
/* Import readline module */
var readline = require('readline');

/*
 * create a new readline.Interface instance that reads data from standard input
 * and write data to standard output
 */
var rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});

/* Ask a question to the user */
rl.question('What is your name? ', (answer) => {
  console.log(`Hello: ${answer}, How are you?`);
  rl.close();
});

Output
What is your name? Krishna
Hello: Krishna, How are you?

Below table summarizes the events provided by readline module.
S.No
Event
Description
1
Close event is emitted in one of the following conditions.
a.   When rl.close() method is called and the readline.Interface instance
b.   The input stream receives its 'end' event;
c.   The input stream receives <ctrl>-D to signal end-of-transmission (EOT);
d.   The input stream receives <ctrl>-C to signal SIGINT and there is no 'SIGINT' event listener registered on the readline.Interface instance.
The listener/callback function is called without passing any arguments.
2
line event is emitted, whenever the input stream receives an end-of-line input (\n, \r, or \r\n).

In case of standard input, this event is fired, when user press <Enter>, or <Return> keys.
3
The 'pause' event is emitted when one of the following occur:

a.   The input stream is paused.
b.   The input stream is not paused and receives the 'SIGCONT' event.

The listener function is called without passing any arguments.
4
The 'resume' event is emitted whenever the input stream is resumed.

The listener function is called without passing any arguments.
5
SIGCONT
The 'SIGCONT' event is emitted when a Node.js process previously moved into the background using <ctrl>-Z (i.e. SIGTSTP) is then brought back to the foreground.

The 'SIGCONT' event is not supported on Windows.
6
The 'SIGINT' event is emitted whenever the input stream receives a <ctrl>-C input
7
SIGTSTP
The 'SIGTSTP' event is emitted when the input stream receives a <ctrl>-Z input.

Below table summarizes the functions provided my readline module.
S.No
Method and Description
1
closes the readline.Interface instance and relinquishes control over the input and output streams. This method emits the close event.
2
pause methog is used to pause the input stream, you can resume the stream by calling resume() method.
3
rl.prompt prompt the user with information. rl.prompt() will resume the input stream if it has been paused.
4
This method displays the query by writing it to the output, waits for user input to be provided on input, then invokes the callback function passing the provided input as the first argument
5
rl.resume()
The rl.resume() method resumes the input stream if it has been paused.
6
This method sets the prompt that will be written to output whenever rl.prompt() is called.
7
This method writes either data or a key sequence identified by key to the output. The key argument is supported only if output is a TTY text terminal.
8
method clears current line of given TTY stream in a specified direction identified by dir.
9
The readline.clearScreenDown() method clears the given TTY stream from the current position of the cursor down.
10
This method returns a new readline.Interface instance.
11
This method moves cursor to the specified position in a TTY stream.
12
This method causes the given Readable stream to begin emitting 'keypress' events.
13
This method moves the cursor relative to its current position in a given TTY stream.




Previous                                                 Next                                                 Home

No comments:

Post a Comment