三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 348|回复: 0
打印 上一主题 下一主题

Node.js 逐行读取

[复制链接]

1562

主题

1564

帖子

4904

积分

博士

Rank: 8Rank: 8

积分
4904
跳转到指定楼层
楼主
发表于 2017-8-8 08:14:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
使用 require('readline'),可以使用这个模块。逐行读取(Readline)可以逐行读取流(比如process.stdin)
一旦你开启了这个模块,node 程序将不会终止,直到你关闭接口。以下的代码展示了如何优雅的退出程序:
  1. var readline = require('readline');

  2. var rl = readline.createInterface({
  3.   input: process.stdin,
  4.   output: process.stdout
  5. });

  6. rl.question("What do you think of node.js? ", function(answer) {
  7.   // TODO: Log the answer in a database
  8.   console.log("Thank you for your valuable feedback:", answer);

  9.   rl.close();
  10. });
复制代码
readline.createInterface(options)

创建一个逐行读取(Readline) Interface 实例. 参数 "options" 对象有以下值:
input - 监听的可读流 (必填).
output - 逐行读取(Readline)数据要写入的可写流(可选).
completer - 用于 Tab 自动补全的可选函数。参见下面的例子。
terminal - 如果希望和 TTY 一样,对待 input 和 output 流,设置为 true。 并且由 ANSI/VT100 转码。默认情况下,检查 isTTY 是否在 output 流上实例化。
completer 给出当前行的入口,应该返回包含2条记录的数组
一个匹配当前输入补全的字符串数组
用来匹配的子字符串
最终像这样:[[substr1, substr2, ...], originalsubstring].
例子:
  1. function completer(line) {
  2.   var completions = '.help .error .exit .quit .q'.split(' ')
  3.   var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })
  4.   // show all completions if none found
  5.   return [hits.length ? hits : completions, line]
  6. }
复制代码
同时, completer 可以异步运行,此时接收到2个参数:
  1. function completer(linePartial, callback) {
  2.   callback(null, [['123'], linePartial]);
  3. }
复制代码
为了接受用户输入,createInterface 通常和 process.stdin ,process.stdout一起使用:
  1. var readline = require('readline');
  2. var rl = readline.createInterface({
  3.   input: process.stdin,
  4.   output: process.stdout
  5. });
复制代码
如果你有逐行读取(Readline)实例, 通常会监听"line" 事件.
如果这个实例参数 terminal = true,而且定义了 output.columns 属性,那么 output 流将会最佳兼容性,并且,当 columns 变化时(当它是 TTY 时,process.stdout 会自动这么做),会在 output 流上触发 "resize" 事件。
Class: Interface
代表一个包含输入/输出流的逐行读取(Readline)接口的类,
rl.setPrompt(prompt)
设置提示符,比如当你再命令行里运行 node 时,可以看到 node 的提示符 >。
rl.prompt([preserveCursor])
为用户输入准备好逐行读取(Readline),将当前 setPrompt 选项方法哦新的行中,让用户有新的地方输入。设置 preserveCursor 为 true,防止当前的游标重置为 0。
如果暂停,使用 createInterface也可以重置input 输入流。
调用 createInterface 时,如果 output 设置为 null 或 undefined ,不会重新写提示符。
rl.question(query, callback)
预先提示 query,用户应答后触发 callback。给用户显示 query 后,用户应答被输入后,调用 callback。
如果暂停,使用 createInterface也可以重置input 输入流。
调用 createInterface 时,如果 output 设置为 null 或 undefined ,不会重新写提示符。
例子:
  1. interface.question('What is your favorite food?', function(answer) {
  2.   console.log('Oh, so your favorite food is ' + answer);
  3. });
复制代码
rl.pause()
暂停逐行读取(Readline)的 input 输入流, 如果需要可以重新启动。
注意,这不会立即暂停流。调用 pause 后还会有很多事件触发,包含 line。
rl.resume()
恢复 逐行读取(Readline) input 输入流.
rl.close()
关闭 Interface 实例, 放弃控制输入输出流。会触发"close" 事件。
rl.write(data[, key])
调用createInterface 后,将数据 data 写到 output 输出流,除非 output 为 null,或未定义undefined 。key 是一个代表键序列的对象;当终端是一个 TTY 时可用。
暂停 input 输入流后,这个方法可以恢复。
例子:
  1. rl.write('Delete me!');
  2. // Simulate ctrl+u to delete the line written previously
  3. rl.write(null, {ctrl: true, name: 'u'});
复制代码


回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2025-6-27 00:18 , Processed in 0.030212 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表