Node.js readline模块

Readline

  • Class: Interface
  • Event: ‘close’
  • Event: ‘line’
  • Event: ‘pause’
  • Event: ‘resume’
  • Event: ‘SIGCONT’
  • Event: ‘SIGINT’
  • Event: ‘SIGTSTP’
  • rl.close()
  • rl.pause()
  • rl.prompt([preserveCursor])
  • rl.question(query, callback)
  • rl.resume()
  • rl.setPrompt(prompt)
  • rl.write(data[, key])
  • readline.clearLine(stream, dir)
  • readline.clearScreenDown(stream)
  • readline.createInterface(options)
  • Use of the completer Function
  • readline.cursorTo(stream, x, y)
  • readline.emitKeypressEvents(stream[, interface])
  • readline.moveCursor(stream, dx, dy)
  • Example: Tiny CLI
  • Example: Read File Stream Line-by-Line

The readline module provides an interface for reading data from a Readable stream (such as process.stdin) one line at a time.

readline模块提供了一个接口用来从一个可读的流中读取数据(比如process.stdin),每次读取一行。

在nodejs的命令行模式下默认引入了readline模块,但是如果要执行node.js脚本文件的话还是需要显示引入readline模块。

注意:readline适合动态交互式流处理,类似于C++命令行中的cin和cout。不是我们平常意义上说的按行读取文件。当流被创建时,readline只会监听新增的行而忽略已有的行。

os.EOL表示换行符

const readline = require('readline');

The following simple example illustrates the basic use of the readline module.

下面这个简单的例子说明了readline模块的基本用法。

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});
rl.question('What do you think of Node.js? ', (answer) => {
    console.log('Thank you for you valuable feedback: ' + answer);
    rl.close();
});

Node.js readline模块_第1张图片

Note Once this code is invoked, the Node.js application will not terminate until the readline.Interface is closed because the interface waits for data to be received on the input stream.

注意:一旦该代码被调用,Node.js程序将不会终止知道readline接口被关闭,因为接口会在输入流中不停的等待数据。

Class: Interface

Instances of the readline.Interface class are constructed using the readline.createInterface() method. Every instance is associated with a single input Readable stream and a single output Writable stream. The output stream is used to print prompts for user input that arrives on, and is read from, the input stream.

readline.Interface实例是通过readline.createInterface()函数来构造。每一个实例都关联了一个唯一的可读输入流和一个唯一的可写输出流。输出流是为用户即将进行的输入打印提示信息,并且从输入流中读取数据。

Event: ‘close’

The ‘close’ event is emitted when one of the following occur:

  • The rl.close() method is called and the readline.Interface instance has relinquished control over the input and output streams;
  • The input stream receives its ‘end’ event;
  • The input stream receives -D to signal end-of-transmission (EOT);
  • The input stream receives -C to signal SIGINT and there is no SIGINT event listener registered on the readline.Interface instance.

The listener function is called without passing any arguments.
The readline.Interface instance should be considered to be “finished” once the ‘close’ event is emitted.

当以下的一种情况发生时,’close’事件会被触发:

  • rl.close() 方法被调用后,readline.Interaface实例便会放弃输入输出流的控制权;
  • 输入流收到它自己的’end’事件;
  • 输入流收到-D,发送传输结束信号(EOF,end-of-transmission);
  • 输入流收到-C,发送SIGINT信号,并且在readline.Interfacce实例上并没有注册SIGINT事件的监听器;

侦听函数被调用的时候,不传递任何参数。
一旦’close’事件被触发,readline.Interface实例就会被认为是已经终止了。

Event: ‘line’

The ‘line’ event is emitted whenever the input stream receives an end-of-line input (\n, \r, or \r\n). This usually occurs when the user presses the , or keys.

The listener function is called with a string containing the single line of received input.

无论何时,当输入流收到行结束符(\n, \r, 或者 \r\n)的时候,’line’事件都好被触发。它通常发生在用户按下按键时。

当输入单独的一行的字符串的时候,监听器函数会被调用。

Event: ‘pause’

The ‘pause’ event is emitted when one of the following occur:

  • The input stream is paused.
  • The input stream is not paused and receives the SIGCONT event. (See events SIGTSTP and SIGCONT)

The listener function is called without passing any arguments.

‘pause’事件会在下面任意一种情况发生时被触发:

  • 输入流停止。
  • 输入流没有停止,但是且接收到SIGCONT事件(参阅SIGTSTP 和 SIGCONT 事件)

该监听函数被调用时不会传递任何参数

Event: ‘resume’

The ‘resume’ event is emitted whenever the input stream is resumed.

The listener function is called without passing any arguments.

当输入流被唤醒时,’resume’事件会被触发。
该监听函数被调用时不会传递任何参数。

Event: ‘SIGCONT’

The ‘SIGCONT’ event is emitted when a Node.js process previously moved into the background using -Z (i.e. SIGTSTP) is then brought back to the foreground using fg(1).

If the input stream was paused before the SIGTSTP request, this event will not be emitted.

当使用-Z将Node.js进程移动到后台时,SIGCONT事件将会被触发。然后可以使用 fg(1) 命令将它移动到前台。

如果输入流在收到SIGTSIP之前就已经被暂停了,那么该事件就不会触发。

Event: ‘SIGINT’

The ‘SIGINT’ event is emitted whenever the input stream receives a -C input, known typically as SIGINT. If there are no ‘SIGINT’ event listeners registered when the input stream receives a SIGINT, the ‘pause’ event will be emitted.

The listener function is invoked without passing any arguments.

当输入流收到-C命令时,我们熟知的SIGINT事件就会被触发。如果当时并没有注册任何SIGINT事件监听器,那么当输入流接收到SIGINT信号时,’pause’事件就会被触发。

该监听函数被调用时不会传递任何参数。

rl.on('SIGINT', () => {
    rl.question('Are you sure you want to exit?', (answer) => {
        if(answer.match(/^y(es)?$/i)) {
            rl.pause();
        }
    });
});

Event: ‘SIGTSTP’

The ‘SIGTSTP’ event is emitted when the input stream receives a -Z input, typically known as SIGTSTP. If there are no SIGTSTP event listeners registered when the input stream receives a SIGTSTP, the Node.js process will be sent to the background.

When the program is resumed using fg(1), the ‘pause’ and SIGCONT events will be emitted. These can be used to resume the input stream.

The ‘pause’ and ‘SIGCONT’ events will not be emitted if the input was paused before the process was sent to the background.

The listener function is invoked without passing any arguments.

当输入流收到-Z命令时,我们熟知的SIGTSTP事件就会被触发。如果没有注册SIGTSTP时间监听器,那么当输入流收到SIGTSTP事件时,Node.js进程就将会被移动到后台。

如果输入在进程被发送到后台之前就已经被暂停了,那么’pause’和’SIGCONT’事件就将不会被触发。

该监听函数被调用时不会传递任何参数。

Example:Tiny CLI (小型命令行)

const readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    prompt: 'TinyCLI>'  
});
rl.prompt();
rl.on('line', (line) => {
    switch(line.trim()) {
    case 'hello':
        console.log('world');
        break;
    default:
        console.log(`Say what? I might have heard '${line.trim()}'`);
        break;
    }
    rl.prompt();
}).on('close', () => {
    console.log('Have a great day, Bye!');
    //没有exit则会继续监听输入,线程并不会退出
    //process.exit(0);
}).on('SIGINT', () => {
    console.log('Ctrl+C => SIGINT');
});

ctrl-c 发送 SIGINT 信号给前台进程组中的所有进程。常用于终止正在运行的程序。
ctrl-z 发送 SIGTSTP 信号给前台进程组中的所有进程,常用于挂起一个进程。
ctrl-d 不是发送信号,而是表示一个特殊的二进制值,表示 EOF。
ctrl-\ 发送 SIGQUIT 信号给前台进程组中的所有进程,终止前台进程并生成 core 文件。
kill -SIGCONT PID 发送 SIGCONT信号,让一个停止(stopped)的进程继续执行.

更多信号表请参看 http://blog.csdn.net/chy555chy/article/details/52626515

Example:Read File Stream Line-by-Line

const readline = require('readline');
const fs = require('fs');
const rl = readline.createInterface({
    input: fs.createReadStream('C:/Users/Administrator/Desktop/readlineDemo.js'),
    terminal: true
});
rl.on('line', (line) => {
    console.log('line = ', line);
}).on('close', () => {
    console.log('readline is closed');
});

当流创建的时候文件里的所有数据便会被都出来,存储在Interface对象的以数组的方式存储在history属性中。然后当输入流中新增了一行的时候,才会触发 ‘line’ 事件。

你可能感兴趣的:(————,Node.js)