What is readline2?
The readline2 npm package is a Node.js module that provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time. It is a more feature-rich and flexible alternative to the built-in readline module in Node.js.
What are readline2's main functionalities?
Basic Line Reading
This feature allows you to read a line of input from the user and then process it. The example code demonstrates how to prompt the user for their name and then print a greeting.
const readline = require('readline2');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question('What is your name? ', (answer) => {
console.log(`Hello, ${answer}!`);
rl.close();
});
Handling Multiple Lines
This feature allows you to handle multiple lines of input. The example code demonstrates how to listen for 'line' events and process each line of input as it is received.
const readline = require('readline2');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (input) => {
console.log(`Received: ${input}`);
});
rl.on('close', () => {
console.log('Interface closed');
});
Custom Completer
This feature allows you to provide custom tab completion for user input. The example code demonstrates how to set up a completer that suggests commands like 'help', 'error', and 'exit'.
const readline = require('readline2');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
completer: (line) => {
const completions = 'help error exit'.split(' ');
const hits = completions.filter((c) => c.startsWith(line));
return [hits.length ? hits : completions, line];
}
});
rl.question('Type a command: ', (answer) => {
console.log(`You typed: ${answer}`);
rl.close();
});
Other packages similar to readline2
readline
The built-in readline module in Node.js provides similar functionality for reading lines of input from a Readable stream. However, readline2 offers additional features and flexibility, making it a more powerful alternative.
prompt
The prompt package is another alternative for getting user input in Node.js. It provides a more user-friendly interface for prompting users for input, including support for validation and default values. However, it may not offer the same level of control over the input stream as readline2.
inquirer
Inquirer is a popular package for creating interactive command-line interfaces. It provides a higher-level abstraction for prompting users with various types of questions, such as lists, checkboxes, and password inputs. While it offers more advanced features for user interaction, it may be overkill for simple line reading tasks that readline2 handles well.
readline2 
Node.js (v0.8 and v0.10) had some bugs and issues with the default Readline module.
This module include fixes seen in later version (0.11-0.12 and iojs) and ease some undesirable behavior one could see using the readline to create interatives prompts. This means readline2
change some behaviors and as so is not meant to be an exact drop-in replacement.
This project is extracted from the core of Inquirer.js interactive prompt interface to be available as a standalone module.
Documentation
Installation: npm install --save readline2
readline2.createInterface(options); -> {Interface}
Present the same API as Node.js readline.createInterface()
Improvements
- Default
options.input
as process.stdin
- Default
options.output
as process.stdout
interface.stdout
is wrapped in a MuteStream- Prevent
up
and down
keys from moving through history inside the readline - Fix cursor position after a line refresh when the
Interface
prompt contains ANSI colors - Correctly return the cursor position when faced with implicit line returns
License
Copyright (c) 2012 Simon Boudrias (twitter: @vaxilart)
Licensed under the MIT license.