{
  "source": "doc/api/readline.markdown",
  "modules": [
    {
      "textRaw": "Readline",
      "name": "readline",
      "stability": 3,
      "stabilityText": "Stable",
      "desc": "<p>To use this module, do <code>require(&apos;readline&apos;)</code>. Readline allows reading of a\nstream (such as STDIN) on a line-by-line basis.\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, and the STDIN stream. Here&apos;s how\nto allow your program to gracefully terminate:\n\n</p>\n<pre><code>var rl = require(&apos;readline&apos;);\n\nvar i = rl.createInterface(process.stdin, process.stdout, null);\ni.question(&quot;What do you think of node.js?&quot;, function(answer) {\n  // TODO: Log the answer in a database\n  console.log(&quot;Thank you for your valuable feedback.&quot;);\n\n  // These two lines together allow the program to terminate. Without\n  // them, it would run forever.\n  i.close();\n  process.stdin.destroy();\n});</code></pre>\n",
      "methods": [
        {
          "textRaw": "rl.createInterface(input, output, completer)",
          "type": "method",
          "name": "createInterface",
          "desc": "<p>Takes two streams and creates a readline interface. The <code>completer</code> function\nis used for autocompletion. When given a substring, it returns <code>[[substr1,\nsubstr2, ...], originalsubstring]</code>.\n\n</p>\n<p>Also <code>completer</code> can be run in async mode if it accepts two arguments:\n\n</p>\n<p>  function completer(linePartial, callback) {\n    callback(null, [[&apos;123&apos;], linePartial]);\n  }\n\n</p>\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<pre><code>var readline = require(&apos;readline&apos;),\n  rl = readline.createInterface(process.stdin, process.stdout);</code></pre>\n",
          "signatures": [
            {
              "params": [
                {
                  "name": "input"
                },
                {
                  "name": "output"
                },
                {
                  "name": "completer"
                }
              ]
            }
          ]
        }
      ],
      "classes": [
        {
          "textRaw": "Class: Interface",
          "type": "class",
          "name": "Interface",
          "desc": "<p>The class that represents a readline interface with a stdin and stdout\nstream.\n\n</p>\n",
          "methods": [
            {
              "textRaw": "rl.setPrompt(prompt, length)",
              "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",
              "signatures": [
                {
                  "params": [
                    {
                      "name": "prompt"
                    },
                    {
                      "name": "length"
                    }
                  ]
                }
              ]
            },
            {
              "textRaw": "rl.prompt()",
              "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.\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "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> with the\nuser&apos;s response after it has been typed.\n\n</p>\n<p>Example usage:\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.close()",
              "type": "method",
              "name": "close",
              "desc": "<p>  Closes tty.\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "rl.pause()",
              "type": "method",
              "name": "pause",
              "desc": "<p>  Pauses tty.\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "rl.resume()",
              "type": "method",
              "name": "resume",
              "desc": "<p>  Resumes tty.\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            },
            {
              "textRaw": "rl.write()",
              "type": "method",
              "name": "write",
              "desc": "<p>  Writes to tty.\n\n</p>\n",
              "signatures": [
                {
                  "params": []
                }
              ]
            }
          ],
          "events": [
            {
              "textRaw": "Event: 'line'",
              "type": "event",
              "name": "line",
              "desc": "<p><code>function (line) {}</code>\n\n</p>\n<p>Emitted whenever the <code>in</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>Example of listening for <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: 'close'",
              "type": "event",
              "name": "close",
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p>Emitted whenever the <code>in</code> stream receives a <code>^C</code> or <code>^D</code>, respectively known\nas <code>SIGINT</code> and <code>EOT</code>. This is a good way to know the user is finished using\nyour program.\n\n</p>\n<p>Example of listening for <code>close</code>, and exiting the program afterward:\n\n</p>\n<pre><code>rl.on(&apos;close&apos;, function() {\n  console.log(&apos;goodbye!&apos;);\n  process.exit(0);\n});</code></pre>\n<p>Here&apos;s an example of how to use all these together to craft a tiny command\nline interface:\n\n</p>\n<pre><code>var readline = require(&apos;readline&apos;),\n  rl = readline.createInterface(process.stdin, process.stdout),\n  prefix = &apos;OHAI&gt; &apos;;\n\nrl.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.setPrompt(prefix, prefix.length);\n  rl.prompt();\n}).on(&apos;close&apos;, function() {\n  console.log(&apos;Have a great day!&apos;);\n  process.exit(0);\n});\nconsole.log(prefix + &apos;Good to see you. Try typing stuff.&apos;);\nrl.setPrompt(prefix, prefix.length);\nrl.prompt();</code></pre>\n<p>Take a look at this slightly more complicated\n<a href=\"https://gist.github.com/901104\">example</a>, and\n<a href=\"https://github.com/cloudhead/http-console\">http-console</a> for a real-life use\ncase.\n</p>\n",
              "params": []
            }
          ]
        }
      ],
      "type": "module",
      "displayName": "Readline"
    }
  ]
}
