What is the readline

Readline allows you to read data line by line from a readable stream, such as process.stdin. The readline module is introduced by default in Node.js command line mode, but if you run scripts in Node.js, you need to import the module manually by requiring (‘readline’).

How do I use readline

Create an instance

First, create an interface instance that provides an Object parameter. Input: readable stream to listen to (required) Output: writable stream to write to readline (required) Completer: Optional function for Tab auto-completion (not commonly used) terminal: If you want input and output streams to be treated like TTY, pass the argument true and transcode it via ANSI/VT100. By default, isTTY is checked to see if it is instantiated on the output stream. HistorySize: The maximum number of rows reserved for history. To disable this value, it is set to 0. The default is 30. (not often used)

The first method: pass only two mandatory arguments

  const readLine = require('readline');
  let rl = readLine.createInterface(process.stdin, process.stdout);
Copy the code

Second way: You can pass in additional parameters that are not required

  const readLine = require('readline');
  let rl = readLine.createinterface ({input: process.stdin, output: process.stdout, // other parameters omitted})Copy the code

The instance

  const readLine = require('readline');
  let rl = readLine.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: '> Please enter '}) // call rl.prompt(); // The console displays: > Please enterCopy the code

Note: Once this code is called, node.js will not terminate until the readline interface is closed, because the interface is constantly waiting for data in the input stream.

methods

All of the following instances are created according to the following RL:

  const readLine = require('readline');
  let rl = readLine.createInterface({
    input: process.stdin,
    output: process.stdout
  })
Copy the code
1, rl. SetPrompt ([prompt])

Set the prompt. When you run the file using Node and call rl.prompt(), you will see the prompt you set on the terminal.

  rl.setPrompt('> Please enter ')
Copy the code

Note: This method has the same effect as the instance parameter configuration above

2, rl. Prompt ([preserveCursor])

Prepare the readline for user input by placing the existing setPrompt option on a new line to give the user a new place to start typing. Set preserveCursor to True to prevent the cursor position from being reset to 0. For newline input, that is, to enter information on a new line, add \n in prompt.

rl.prompt(); // The console displays: > Please enterCopy the code
3, rl. Write (data [key])

When creating an instance with readline.createInterface(), if the input stream is null or undefined, data is written to the output stream. Key is a literal object representing a sequence of keys, available if the terminal is a TTY.

  rl.write('Delete me! ');
  // Simulate ctrl+u to delete the line written previously
  rl.write(null, {ctrl: true, name: 'u'});
Copy the code
4, rl. Question (query, callback)

The pre-specified prompt, query, will trigger the execution of the specified callback function when user input is answered.

  rl.question('How old are you? \n'.function(prompt) {
    console.log('Here's what you typed in:' + prompt);
    rl.close();
  })
Copy the code
5, rl. Pause ()

Readline’s input stream can be resumed by pausing it, as can rl.resume(), rl.write(), rl.question(), and rl.prompt(). Calling this method raises the pause event.

6, rl. Resume ()

Restore the input stream of readLine. Calling this method triggers the Resume event.

7, rl. Close ()

Close the instance created by readLine without controlling the input/output stream. Calling this method fires the close event.

The event

1. Line events

Emitted whenever the input stream receives a line terminator (\n, \r, or \r\n), that is, when the user presses the Enter or return key after typing. Can be used to listen for user input.

  rl.on('line'.function (line) {
    console.log('Here's what the user typed:' + line);
  })
Copy the code
2. Pause events

Raised when the input stream is paused, i.e. when rL.pause () is called, or when the input stream is not paused, or when SIGCONT/SIGINT is received and no SIGCONT/SIGINT listeners are registered on the readLine.interfacce instance. And if the input stream is paused before SIGTSIP is received, the event will not fire.

  rl.on('pause'.function() {
    console.log('Pause the input stream... ')})Copy the code
Resume event

Resume (), or rl.write(), rl.question(), and rl.prompt() are called.

  rl.on('resume'.function() {
    console.log('Restore the input stream... ')})Copy the code
4. Close event

This event is emitted when the rl.close() method is called, or an “end” event is received by the input stream. The close event is triggered in any of the following cases. Once the close event is triggered, the readline.Interaface instance is considered terminated. 1) When the rl.close() method is called, the readline.Interaface instance relinquish control of the input/output stream. 2) The input stream receives its own “end” event. 3) The input stream receives the -C signal, sends the SIGINT signal, and does not register the SIGINT listener on the readline.Interfacce instance. 4) The input stream receives -D and sends the end of transmission signal (EOF, end-of-transmission); 5), EOT: delimiter, that is, enter a single or multiple “>” and press Enter.

  rl.on('close'.function() {
    console.log('Close the input stream... '); process.exit(0); Exit the current process})Copy the code
5. SIGCOUT (this event does not work on Windows)

The SIGCONT event will be emitted when the Node.js process is moved to the background using -z. It can then be moved to the foreground using the fg(1) command. If the input stream has been paused before SIGTSIP is received, the event will not fire.

6, SIGINT

The well-known SIGINT event is emitted when the input stream receives a -c command. If no SIGINT event listeners are registered, both the ‘pause’ event and the ‘close’ event will be emitted when the SIGINT signal is received by the input stream.

7. SIGTSTP (this event does not work on Windows)

When the input stream receives the -z command, the well-known SIGTSTP event is fired. If SIGTSTP event listeners are not registered, the Node.js process will be moved to the background when SIGTSTP events are received by the input stream. If input is paused before the process is sent to the background, the ‘pause’ and ‘SIGCONT’ events will not be emitted.

See the official documentation reference case for joke Readline