{
  "source": "readline.markdown",
  "modules": [
    {
      "textRaw": "Readline",
      "name": "readline",
      "desc": "<pre><code>稳定度: 2 - 不稳定</code></pre>\n<p>To use this module, do <code>require(&apos;readline&apos;)</code>. Readline allows reading of a\nstream (such as <code>process.stdin</code>) on a line-by-line basis.\n\n</p>\n<p>要使用此模块，需要<code>require(&apos;readline&apos;)</code>.Readline程序允许逐行读取一个流内容(例如<code>process.stdin</code>).\n\n</p>\n<p>Note that once you&apos;ve invoked this module, your node program will not\nterminate until you&apos;ve closed the interface. Here&apos;s how to allow your\nprogram to gracefully exit:\n\n</p>\n<p>需要注意的是你一旦调用了这个模块，你的node程序将不会终止直到你关闭此接口。下面是如何让你的程序正常退出的方法:\n\n</p>\n<pre><code>  rl.close();\n});</code></pre>\n",
      "methods": [
        {
          "textRaw": "readline.createInterface(options)",
          "type": "method",
          "name": "createInterface",
          "desc": "<p>Creates a readline <code>Interface</code> instance. Accepts an &quot;options&quot; Object that takes\nthe following values:\n\n</p>\n<p>创建一个readline的接口实例. 接受一个Object类型参数，可传递以下几个值:\n\n</p>\n<ul>\n<li><p><code>input</code> - the readable stream to listen to (Required).</p>\n</li>\n<li><p><code>input</code> - 要监听的可读流 (必需).</p>\n</li>\n<li><p><code>output</code> - the writable stream to write readline data to (Required).</p>\n</li>\n<li><p><code>output</code> - 要写入 readline 的可写流 (必须).</p>\n</li>\n<li><p><code>completer</code> - an optional function that is used for Tab autocompletion. See\nbelow for an example of using this.</p>\n</li>\n<li><p><code>completer</code> - 用于 Tab 自动补全的可选函数。见下面使用的例子。</p>\n</li>\n<li><p><code>terminal</code> - pass <code>true</code> if the <code>input</code> and <code>output</code> streams should be\ntreated like a TTY, and have ANSI/VT100 escape codes written to it.\nDefaults to checking <code>isTTY</code> on the <code>output</code> stream upon instantiation.</p>\n</li>\n<li><p><code>terminal</code> - 如果希望 <code>input</code> 和 <code>output</code> 流像 TTY 一样对待，那么传递参数 <code>true</code> ，并且经由 ANSI/VT100 转码。\n默认情况下检查 <code>isTTY</code> 是否在 <code>output</code> 流上实例化。</p>\n</li>\n</ul>\n<p>The <code>completer</code> function is given a the current line entered by the user, and\nis supposed to return an Array with 2 entries:\n\n</p>\n<p>通过用户 <code>completer</code> 函数给定了一个当前行入口，并且期望返回一个包含两个条目的数组：\n\n</p>\n<ol>\n<li><p>An Array with matching entries for the completion.</p>\n</li>\n<li><p>一个匹配当前输入补全的字符串数组.</p>\n</li>\n<li><p>The substring that was used for the matching.</p>\n</li>\n<li><p>一个用于匹配的子字符串。</p>\n</li>\n</ol>\n<p>Which ends up looking something like:\n<code>[[substr1, substr2, ...], originalsubstring]</code>.\n\n</p>\n<p>最终像这种形式:\n<code>[[substr1, substr2, ...], originalsubstring]</code>.\n\n</p>\n<p>Example:\n\n</p>\n<p>示例：\n\n</p>\n<pre><code>function completer(line) {\n  var completions = &apos;.help .error .exit .quit .q&apos;.split(&apos; &apos;)\n  var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })\n  // show all completions if none found\n  return [hits.length ? hits : completions, line]\n}</code></pre>\n<p>Also <code>completer</code> can be run in async mode if it accepts two arguments:\n\n</p>\n<p><code>completer</code> 也可以运行在异步模式下，此时接受两个参数：\n\n</p>\n<pre><code>function completer(linePartial, callback) {\n  callback(null, [[&apos;123&apos;], linePartial]);\n}</code></pre>\n<p><code>createInterface</code> is commonly used with <code>process.stdin</code> and\n<code>process.stdout</code> in order to accept user input:\n\n</p>\n<p>为了接受用户的输入，<code>createInterface</code> 通常跟 <code>process.stdin</code> 和 <code>process.stdout</code> 一块使用：\n\n</p>\n<pre><code>var readline = require(&apos;readline&apos;);\nvar rl = readline.createInterface({\n  input: process.stdin,\n  output: process.stdout\n});</code></pre>\n<p>Once you have a readline instance, you most commonly listen for the\n<code>&quot;line&quot;</code> event.\n\n</p>\n<p>一旦你有一个 readline 实例，你通常会监听 &quot;line&quot; 事件。\n\n</p>\n<p>If <code>terminal</code> is <code>true</code> for this instance then the <code>output</code> stream will get\nthe best compatibility if it defines an <code>output.columns</code> property, and fires\na <code>&quot;resize&quot;</code> event on the <code>output</code> if/when the columns ever change\n(<code>process.stdout</code> does this automatically when it is a TTY).\n\n</p>\n<p>如果这个实例中<code>terminal</code>为<code>true</code>，而且<code>output</code>流定义了一个<code>output.columns</code>属性，那么<code>output</code>流将获得最好的兼容性，并且，当columns变化时(当它是TTY时，<code>process.stdout</code>会自动这样做)，会在<code>output</code>上触发一个 <code>&quot;resize&quot;</code>事件。\n\n</p>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "options"
                }
              ]
            }
          ]
        }
      ],
      "modules": [
        {
          "textRaw": "类: 接口",
          "name": "类:_接口",
          "desc": "<p>The class that represents a readline interface with an input and output\nstream.\n\n</p>\n<p>代表一个有输入输出流的 readline 接口的类。\n\n</p>\n",
          "methods": [
            {
              "textRaw": "rl.setPrompt(prompt)",
              "type": "method",
              "name": "setPrompt",
              "desc": "<p>Sets the prompt, for example when you run <code>node</code> on the command line, you see\n<code>&gt; </code>, which is node&apos;s prompt.\n\n</p>\n<p>设置提示符，例如当你在命令行运行 <code>node</code> 时，你会看到 <code>&gt; </code> ，这就是 node 的提示符。\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "prompt"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "rl.prompt([preserveCursor])",
              "type": "method",
              "name": "prompt",
              "desc": "<p>Readies readline for input from the user, putting the current <code>setPrompt</code>\noptions on a new line, giving the user a new spot to write. Set <code>preserveCursor</code>\nto <code>true</code> to prevent the cursor placement being reset to <code>0</code>.\n\n</p>\n<p>为用户输入准备好readline，将现有的<code>setPrompt</code>选项放到新的一行，让用户有一个新的地方开始输入。将<code>preserveCursor</code>设为<code>true</code>来防止光标位置被重新设定成<code>0</code>。\n\n</p>\n<p>This will also resume the <code>input</code> stream used with <code>createInterface</code> if it has\nbeen paused.\n\n</p>\n<p>如果暂停，也会使用 <code>createInterface</code> 重置 <code>input</code> 流。\n\n</p>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "preserveCursor",
                      "optional": true
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "rl.question(query, callback)",
              "type": "method",
              "name": "question",
              "desc": "<p>Prepends the prompt with <code>query</code> and invokes <code>callback</code> with the user&apos;s\nresponse. Displays the query to the user, and then invokes <code>callback</code>\nwith the user&apos;s response after it has been typed.\n\n</p>\n<p>预处理 <code>query</code>提示 ，用户应答时调用 <code>callback</code> . 当类型被确定后，将查询结果显示给用户, 然后在用户应答时调用 <code>callback</code>.\n\n</p>\n<p>This will also resume the <code>input</code> stream used with <code>createInterface</code> if\nit has been paused.\n\n</p>\n<p>如果暂停，也会使用 <code>createInterface</code> 重置 <code>input</code> 流。\n\n</p>\n<p>Example usage:\n\n</p>\n<p>使用示例：\n\n</p>\n<pre><code>interface.question(&apos;What is your favorite food?&apos;, function(answer) {\n  console.log(&apos;Oh, so your favorite food is &apos; + answer);\n});</code></pre>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "query"
                    },
                    {
                      "name": "callback"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "rl.pause()",
              "type": "method",
              "name": "pause",
              "desc": "<p>Pauses the readline <code>input</code> stream, allowing it to be resumed later if needed.\n\n</p>\n<p>暂停 readline 的输入流 (<code>input</code> stream), 如果有需要稍后还可以恢复。\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "rl.resume()",
              "type": "method",
              "name": "resume",
              "desc": "<p>Resumes the readline <code>input</code> stream.\n\n</p>\n<p>恢复 readline 的输入流 (<code>input</code> stream).\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "rl.close()",
              "type": "method",
              "name": "close",
              "desc": "<p>Closes the <code>Interface</code> instance, relinquishing control on the <code>input</code> and\n<code>output</code> streams. The &quot;close&quot; event will also be emitted.\n\n</p>\n<p>关闭接口实例 (<code>Interface</code> instance), 放弃控制输入输出流。&quot;close&quot; 事件会被触发。\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "rl.write(data, [key])",
              "type": "method",
              "name": "write",
              "desc": "<p>Writes <code>data</code> to <code>output</code> stream. <code>key</code> is an object literal to represent a key\nsequence; available if the terminal is a TTY.\n\n</p>\n<p>将 <code>data</code> 写入到 <code>output</code> 流。<code>key</code> 是一个代表键序列的对象；当终端是一个 TTY 时可用。\n\n</p>\n<p>This will also resume the <code>input</code> stream if it has been paused.\n\n</p>\n<p>如果暂停，也会重置 <code>input</code> 流。\n\n</p>\n<p>Example:\n\n</p>\n<p>示例：\n\n</p>\n<pre><code>rl.write(&apos;Delete me!&apos;);\n// 模仿 ctrl+u快捷键，删除之前所写行 \nrl.write(null, {ctrl: true, name: &apos;u&apos;});</code></pre>\n",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "data"
                    },
                    {
                      "name": "key",
                      "optional": true
                    }
                  ]
                }
              ]
            }
          ],
          "type": "module",
          "displayName": "类: 接口"
        },
        {
          "textRaw": "Events",
          "name": "events",
          "events": [
            {
              "textRaw": "Event: 'line'",
              "type": "event",
              "name": "line",
              "desc": "<p><code>function (line) {}</code>\n\n</p>\n<p><code>function (line) {}</code>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream receives a <code>\\n</code>, usually received when the\nuser hits enter, or return. This is a good hook to listen for user input.\n\n</p>\n<p>在 <code>input</code> 流接受了一个 <code>\\n</code> 时触发，通常在用户敲击回车或者返回时接收。\n这是一个监听用户输入的利器。\n\n</p>\n<p>Example of listening for <code>line</code>:\n\n</p>\n<p>监听 <code>line</code> 事件的示例:\n\n</p>\n<pre><code>rl.on(&apos;line&apos;, function (cmd) {\n  console.log(&apos;You just typed: &apos;+cmd);\n});</code></pre>\n",
              "params": []
            },
            {
              "textRaw": "Event: 'SIGINT'",
              "type": "event",
              "name": "SIGINT",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><code>function () {}</code>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream receives a <code>^C</code>, respectively known as\n<code>SIGINT</code>. If there is no <code>SIGINT</code> event listener present when the <code>input</code>\nstream receives a <code>SIGINT</code>, <code>pause</code> will be triggered.\n\n</p>\n<p>只要 <code>input</code>流 接收到<code>^C</code>就会被触发, 分别被认为<code>SIGINT</code>.当<code>input</code>流接收到<code>SIGINT</code>时, \n 如果没有 <code>SIGINT</code> 事件监听器，<code>pause</code> 将会被触发.\n\n</p>\n<p>Example of listening for <code>SIGINT</code>:\n\n</p>\n<p>监听 <code>SIGINT</code> 信号的示例：\n\n</p>\n<pre><code>rl.on(&apos;SIGINT&apos;, function() {\n  rl.question(&apos;Are you sure you want to exit?&apos;, function(answer) {\n    if (answer.match(/^y(es)?$/i)) rl.pause();\n  });\n});</code></pre>\n",
              "params": []
            },
            {
              "textRaw": "Event: 'SIGTSTP'",
              "type": "event",
              "name": "SIGTSTP",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><code>function () {}</code>\n\n</p>\n<p><strong>This does not work on Windows.</strong>\n\n</p>\n<p><strong>该功能不支持 windows 操作系统</strong>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream receives a <code>^Z</code>, respectively known as\n<code>SIGTSTP</code>. If there is no <code>SIGTSTP</code> event listener present when the <code>input</code>\nstream receives a <code>SIGTSTP</code>, the program will be sent to the background.\n\n</p>\n<p>只要<code>input</code>流接收到<code>^Z</code>时就被触发, 分别被认为<code>SIGTSTP</code>. 当<code>input</code>流接收到\n <code>SIGTSTP</code>时，如果没有<code>SIGTSTP</code> 事件监听器 ,程序会被发送到后台 .\n\n</p>\n<p>When the program is resumed with <code>fg</code>, the <code>pause</code> and <code>SIGCONT</code> events will be\nemitted. You can use either to resume the stream.\n\n</p>\n<p>当程序使用参数 <code>fg</code> 重启，<code>pause</code> 和 <code>SIGCONT</code> 事件将会被触发。\n你可以使用两者中任一事件来恢复流。\n\n</p>\n<p>The <code>pause</code> and <code>SIGCONT</code> events will not be triggered if the stream was paused\nbefore the program was sent to the background.\n\n</p>\n<p>在程序被发送到后台之前，如果流暂停，<code>pause</code> 和 <code>SIGCONT</code> 事件将不会被触发。\n\n</p>\n<p>Example of listening for <code>SIGTSTP</code>:\n\n</p>\n<p>监听 <code>SIGTSTP</code> 的示例：\n\n</p>\n<pre><code>rl.on(&apos;SIGTSTP&apos;, function() {\n  // 这将重载 SIGTSTP并防止程序转到\n  // 后台.\n  console.log(&apos;Caught SIGTSTP.&apos;);\n});</code></pre>\n",
              "params": []
            },
            {
              "textRaw": "Event: 'SIGCONT'",
              "type": "event",
              "name": "SIGCONT",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><code>function () {}</code>\n\n</p>\n<p><strong>This does not work on Windows.</strong>\n\n</p>\n<p><strong>该功能不支持 windows 操作系统</strong>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream is sent to the background with <code>^Z</code>,\nrespectively known as <code>SIGTSTP</code>, and then continued with <code>fg(1)</code>. This event\nonly emits if the stream was not paused before sending the program to the\nbackground.\n\n</p>\n<p>一旦 <code>input</code>流中含有 <code>^Z</code>并被发送到后台就会触发,分别被认为\n <code>SIGTSTP</code>, 然后继续执行<code>fg(1)</code>. 这一事件只有在流被发送后台之前没有暂停才会触发.\n\n</p>\n<p>Example of listening for <code>SIGCONT</code>:\n\n</p>\n<p>监听 <code>SIGCONT</code> 的示例:\n\n</p>\n<pre><code>rl.on(&apos;SIGCONT&apos;, function() {\n  // `prompt` 将会自动恢复流\n  rl.prompt();\n});</code></pre>\n",
              "params": []
            }
          ],
          "modules": [
            {
              "textRaw": "事件: 'pause'",
              "name": "事件:_'pause'",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><code>function () {}</code>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream is paused.\n\n</p>\n<p>不论何时，只要输入流被暂停就会触发。\n\n</p>\n<p>Also emitted whenever the <code>input</code> stream is not paused and receives the\n<code>SIGCONT</code> event. (See events <code>SIGTSTP</code> and <code>SIGCONT</code>)\n\n</p>\n<p>而在输入流未被暂停，但收到 <code>SIGCONT</code> 信号时也会触发。 (详见 <code>SIGTSTP</code> 和 <code>SIGCONT</code> 事件)\n\n</p>\n<p>Example of listening for <code>pause</code>:\n\n</p>\n<p>监听 <code>pause</code> 事件的示例：\n\n</p>\n<pre><code>rl.on(&apos;pause&apos;, function() {\n  console.log(&apos;Readline 输入暂停.&apos;);\n});</code></pre>\n",
              "type": "module",
              "displayName": "事件: 'pause'"
            },
            {
              "textRaw": "事件: 'resume'",
              "name": "事件:_'resume'",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><code>function () {}</code>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream is resumed.\n\n</p>\n<p>不论何时，只要输入流重新启用就会触发。\n\n</p>\n<p>Example of listening for <code>resume</code>:\n\n</p>\n<p>监听 <code>resume</code> 事件的示例：\n\n</p>\n<pre><code>rl.on(&apos;resume&apos;, function() {\n  console.log(&apos;Readline 恢复.&apos;);\n});</code></pre>\n",
              "type": "module",
              "displayName": "事件: 'resume'"
            },
            {
              "textRaw": "事件: 'close'",
              "name": "事件:_'close'",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><code>function () {}</code>\n\n</p>\n<p>Emitted when <code>close()</code> is called.\n\n</p>\n<p>当 <code>close()</code> 被调用时触发。\n\n</p>\n<p>Also emitted when the <code>input</code> stream receives its &quot;end&quot; event. The <code>Interface</code>\ninstance should be considered &quot;finished&quot; once this is emitted. For example, when\nthe <code>input</code> stream receives <code>^D</code>, respectively known as <code>EOT</code>.\n\n</p>\n<p>当 <code>input</code>流接收到&quot;结束&quot;事件时也会被触发. 一旦触发，应当认为<code>Interface</code>实例\n &quot;结束&quot; . 例如, 当<code>input</code>流接收到<code>^D</code>时, 分别被认为<code>EOT</code>.\n\n</p>\n<p>This event is also called if there is no <code>SIGINT</code> event listener present when\nthe <code>input</code> stream receives a <code>^C</code>, respectively known as <code>SIGINT</code>.\n\n</p>\n<p>当 <code>input</code> 流接收到一个 <code>^C</code> 时，即使没有 <code>SIGINT</code> 监听器，也会触发这个事件，分别被称为 <code>SIGINT</code> 。\n\n</p>\n",
              "type": "module",
              "displayName": "事件: 'close'"
            }
          ],
          "type": "module",
          "displayName": "Events"
        },
        {
          "textRaw": "示例: Tiny CLI",
          "name": "示例:_tiny_cli",
          "desc": "<p>Here&apos;s an example of how to use all these together to craft a tiny command\nline interface:\n\n</p>\n<p>这里有一个使用所有方法精心设计的小命令行程序：\n\n</p>\n<pre><code>rl.on(&apos;line&apos;, function(line) {\n  switch(line.trim()) {\n    case &apos;hello&apos;:\n      console.log(&apos;world!&apos;);\n      break;\n    default:\n      console.log(&apos;Say what? I might have heard `&apos; + line.trim() + &apos;`&apos;);\n      break;\n  }\n  rl.prompt();\n}).on(&apos;close&apos;, function() {\n  console.log(&apos;Have a great day!&apos;);\n  process.exit(0);\n});</code></pre>\n",
          "type": "module",
          "displayName": "示例: Tiny CLI"
        }
      ],
      "type": "module",
      "displayName": "Readline"
    }
  ]
}