Friday, April 24, 2020

Node.js readline Module

Node.js 'readline' Module

readline module nodejs


The 'readline' module specifies an interface for reading the data streams line by line. The stream must be a readable stream like process.stdin. The module can be accessed by require(‘readline’).

var readline=require('readline');

For example,

var read = require('readline');
var  readline= read.createInterface({
  input: process.stdin,
  output: process.stdout
});

readline.question('How are you feeling today?', (youranswer) => {
  console.log(`you answered: ${youranswer}`);

  readline.close();
});
/*
Output:
How are you feeling today? Fantastic

you answered: Fantastic

*/


The readline.Interface class


The object of readline.Interface(extends EventEmitter) class can be created by using the readline.createInterface() method. This object contains one input Readable stream and one Writable output stream. The output stream is used to send data to print for the input supplied by the user. The data is read using the input stream. The program will not halt until the 'readline.Interface' is closed using the close() method, because then it will keep waiting for the next data to come.

Events


‘close’

This event is emitted as one of the following condition appears,
  • If the readline.close() method is invoked
  • If the input stream receives its 'end' event;
  • If the input stream receives ctrl+D to signal end-of-transmission (EOT);
  • If the input stream receives ctrl+C to signal SIGINT and there is no 'SIGINT' event listener registered on the readline.Interface instance.

'line'

This event is emitted when the input stream encounters an end-of-line input (\n, \r, or \r\n). Generally emitted when the <Enter>, or <Return> keys are pressed.

var read = require('readline');
var  readline= read.createInterface({
  input: process.stdin,
  output: process.stdout
});


readline.on('line', (input) => {
  console.log(`Received: ${input}`);
    readline.close();
});

/*
Output:
 will wait till enter press
Received: will wait till enter press
*/

'pause' 

This event is emitted if the input stream gets paused or the input stream has received the 'SIGCONT' event.

'resume'

Emitted as the input stream is resumed.

'SIGCONT' 

Emitted when a Node.js process running in the background(send to the background using ctrl+z) is brought back to the foreground.

'SIGINT' event

Emitted when the input stream has received ctrl+C input(e.g. SIGINT).

'SIGTSTP' event

Emitted when the input stream has received ctrl+Z input(e.g. SIGTSTP).

Methods


readline.setPrompt(prompt)

This method is used to set a prompt, for example when we run node with cmd, see >, that is Node.js default prompt.

readline.prompt([preserveCursor])

This method prepares readline.Interface instance for the user input and places the current setPrompt options to a new line. This method sets preserveCursor to true to prevent the cursor placement being reset to 0.

readline.question(query, callback)

This method is used to receive input from the user and then invokes callback with the user's response after it has been typed.

readline.pause()

This method pauses the readline input stream. This can be resumed later if needed, using the resume() method.

readline.resume()

This method is used to resume the paused readline input stream.

readline.close()

This method is used to closes the readline.Interface objects, and transfers the control to the input and output streams. The 'close' event is also emitted.

readline.write(data[, key])

This method is used to writes data to the output stream unless the output is set to null or undefined. The key argument is an object used to represent a key sequence in the case of TTY terminal.

readline.getCursorPos()

This method returns the exact position of the cursor in relative to the input prompt + string.

readline.clearLine(stream, dir[, callback])

This method is used to clear the current line of a specified TTY stream in a given direction identified by dir.

readline.createInterface(options)

This method is used to construct a new readline.Interface instance.

readline.moveCursor(stream, dx, dy[, callback])

This method is used to move the cursor relative to its current position in a given TTY stream.

readline.emitKeypressEvents()

Fires keypress events for the specified stream.

Example


var read = require('readline');
var readline = read.createInterface({
  input: process.stdin,
  output: process.stdout,
  prompt: 'ProgrammingHunk Says> '
});

readline.prompt();
console.log('=======================\n');
console.log('What are you using these days???\n');
readline.on('line', (line) => {
  switch (line.trim()) {
    case 'Node.js':
      console.log('You are using Node.js');
      break;
    default:
      console.log(`You are using '${line.trim()}' not Node.js`);
      break;
  }
 readline.prompt();
}).on('close', () => {

  console.log('||========ProgrammingHunk=======||');
  process.exit(0);
});

/*
Output:
 ProgrammingHunk Says> =======================

What are you using these days???

Node.js
You are using Node.js
ProgrammingHunk Says> react
You are using 'react' not Node.js
ProgrammingHunk Says> ||========ProgrammingHunk=======||
*/