~ubuntu-branches/ubuntu/trusty/nodejs/trusty

« back to all changes in this revision

Viewing changes to doc/api/readline.json

  • Committer: Package Import Robot
  • Author(s): Jérémy Lal
  • Date: 2013-08-14 00:16:46 UTC
  • mfrom: (7.1.40 sid)
  • Revision ID: package-import@ubuntu.com-20130814001646-bzlysfh8sd6mukbo
Tags: 0.10.15~dfsg1-4
* Update 2005 patch, adding a handful of tests that can fail on
  slow platforms.
* Add 1004 patch to fix test failures when writing NaN to buffer
  on mipsel.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{
 
2
  "source": "doc/api/readline.markdown",
 
3
  "modules": [
 
4
    {
 
5
      "textRaw": "Readline",
 
6
      "name": "readline",
 
7
      "stability": 2,
 
8
      "stabilityText": "Unstable",
 
9
      "desc": "<p>To use this module, do <code>require(&#39;readline&#39;)</code>. Readline allows reading of a\nstream (such as <code>process.stdin</code>) on a line-by-line basis.\n\n</p>\n<p>Note that once you&#39;ve invoked this module, your node program will not\nterminate until you&#39;ve closed the interface. Here&#39;s how to allow your\nprogram to gracefully exit:\n\n</p>\n<pre><code>var readline = require(&#39;readline&#39;);\n\nvar rl = readline.createInterface({\n  input: process.stdin,\n  output: process.stdout\n});\n\nrl.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;, answer);\n\n  rl.close();\n});</code></pre>\n",
 
10
      "methods": [
 
11
        {
 
12
          "textRaw": "readline.createInterface(options)",
 
13
          "type": "method",
 
14
          "name": "createInterface",
 
15
          "desc": "<p>Creates a readline <code>Interface</code> instance. Accepts an &quot;options&quot; Object that takes\nthe following values:\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>output</code> - the writable stream to write readline data to (Required).</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>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</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<ol>\n<li><p>An Array with matching entries for the completion.</p>\n</li>\n<li><p>The substring that was used for the matching.</p>\n</li>\n</ol>\n<p>Which ends up looking something like:\n<code>[[substr1, substr2, ...], originalsubstring]</code>.\n\n</p>\n<p>Example:\n\n</p>\n<pre><code>function completer(line) {\n  var completions = &#39;.help .error .exit .quit .q&#39;.split(&#39; &#39;)\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<pre><code>function completer(linePartial, callback) {\n  callback(null, [[&#39;123&#39;], 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<pre><code>var readline = require(&#39;readline&#39;);\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>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",
 
16
          "signatures": [
 
17
            {
 
18
              "params": [
 
19
                {
 
20
                  "name": "options"
 
21
                }
 
22
              ]
 
23
            }
 
24
          ]
 
25
        }
 
26
      ],
 
27
      "classes": [
 
28
        {
 
29
          "textRaw": "Class: Interface",
 
30
          "type": "class",
 
31
          "name": "Interface",
 
32
          "desc": "<p>The class that represents a readline interface with an input and output\nstream.\n\n</p>\n",
 
33
          "methods": [
 
34
            {
 
35
              "textRaw": "rl.setPrompt(prompt, length)",
 
36
              "type": "method",
 
37
              "name": "setPrompt",
 
38
              "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&#39;s prompt.\n\n</p>\n",
 
39
              "signatures": [
 
40
                {
 
41
                  "params": [
 
42
                    {
 
43
                      "name": "prompt"
 
44
                    },
 
45
                    {
 
46
                      "name": "length"
 
47
                    }
 
48
                  ]
 
49
                }
 
50
              ]
 
51
            },
 
52
            {
 
53
              "textRaw": "rl.prompt([preserveCursor])",
 
54
              "type": "method",
 
55
              "name": "prompt",
 
56
              "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>This will also resume the <code>input</code> stream used with <code>createInterface</code> if it has\nbeen paused.\n\n</p>\n",
 
57
              "signatures": [
 
58
                {
 
59
                  "params": [
 
60
                    {
 
61
                      "name": "preserveCursor",
 
62
                      "optional": true
 
63
                    }
 
64
                  ]
 
65
                }
 
66
              ]
 
67
            },
 
68
            {
 
69
              "textRaw": "rl.question(query, callback)",
 
70
              "type": "method",
 
71
              "name": "question",
 
72
              "desc": "<p>Prepends the prompt with <code>query</code> and invokes <code>callback</code> with the user&#39;s\nresponse. Displays the query to the user, and then invokes <code>callback</code>\nwith the user&#39;s response after it has been typed.\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>Example usage:\n\n</p>\n<pre><code>interface.question(&#39;What is your favorite food?&#39;, function(answer) {\n  console.log(&#39;Oh, so your favorite food is &#39; + answer);\n});</code></pre>\n",
 
73
              "signatures": [
 
74
                {
 
75
                  "params": [
 
76
                    {
 
77
                      "name": "query"
 
78
                    },
 
79
                    {
 
80
                      "name": "callback"
 
81
                    }
 
82
                  ]
 
83
                }
 
84
              ]
 
85
            },
 
86
            {
 
87
              "textRaw": "rl.pause()",
 
88
              "type": "method",
 
89
              "name": "pause",
 
90
              "desc": "<p>Pauses the readline <code>input</code> stream, allowing it to be resumed later if needed.\n\n</p>\n",
 
91
              "signatures": [
 
92
                {
 
93
                  "params": []
 
94
                }
 
95
              ]
 
96
            },
 
97
            {
 
98
              "textRaw": "rl.resume()",
 
99
              "type": "method",
 
100
              "name": "resume",
 
101
              "desc": "<p>Resumes the readline <code>input</code> stream.\n\n</p>\n",
 
102
              "signatures": [
 
103
                {
 
104
                  "params": []
 
105
                }
 
106
              ]
 
107
            },
 
108
            {
 
109
              "textRaw": "rl.close()",
 
110
              "type": "method",
 
111
              "name": "close",
 
112
              "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",
 
113
              "signatures": [
 
114
                {
 
115
                  "params": []
 
116
                }
 
117
              ]
 
118
            },
 
119
            {
 
120
              "textRaw": "rl.write(data, [key])",
 
121
              "type": "method",
 
122
              "name": "write",
 
123
              "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>This will also resume the <code>input</code> stream if it has been paused.\n\n</p>\n<p>Example:\n\n</p>\n<pre><code>rl.write(&#39;Delete me!&#39;);\n// Simulate ctrl+u to delete the line written previously\nrl.write(null, {ctrl: true, name: &#39;u&#39;});</code></pre>\n",
 
124
              "signatures": [
 
125
                {
 
126
                  "params": [
 
127
                    {
 
128
                      "name": "data"
 
129
                    },
 
130
                    {
 
131
                      "name": "key",
 
132
                      "optional": true
 
133
                    }
 
134
                  ]
 
135
                }
 
136
              ]
 
137
            }
 
138
          ]
 
139
        }
 
140
      ],
 
141
      "modules": [
 
142
        {
 
143
          "textRaw": "Events",
 
144
          "name": "events",
 
145
          "events": [
 
146
            {
 
147
              "textRaw": "Event: 'line'",
 
148
              "type": "event",
 
149
              "name": "line",
 
150
              "desc": "<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>Example of listening for <code>line</code>:\n\n</p>\n<pre><code>rl.on(&#39;line&#39;, function (cmd) {\n  console.log(&#39;You just typed: &#39;+cmd);\n});</code></pre>\n",
 
151
              "params": []
 
152
            },
 
153
            {
 
154
              "textRaw": "Event: 'pause'",
 
155
              "type": "event",
 
156
              "name": "pause",
 
157
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream is paused.\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>Example of listening for <code>pause</code>:\n\n</p>\n<pre><code>rl.on(&#39;pause&#39;, function() {\n  console.log(&#39;Readline paused.&#39;);\n});</code></pre>\n",
 
158
              "params": []
 
159
            },
 
160
            {
 
161
              "textRaw": "Event: 'resume'",
 
162
              "type": "event",
 
163
              "name": "resume",
 
164
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p>Emitted whenever the <code>input</code> stream is resumed.\n\n</p>\n<p>Example of listening for <code>resume</code>:\n\n</p>\n<pre><code>rl.on(&#39;resume&#39;, function() {\n  console.log(&#39;Readline resumed.&#39;);\n});</code></pre>\n",
 
165
              "params": []
 
166
            },
 
167
            {
 
168
              "textRaw": "Event: 'close'",
 
169
              "type": "event",
 
170
              "name": "close",
 
171
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p>Emitted when <code>close()</code> is called.\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>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",
 
172
              "params": []
 
173
            },
 
174
            {
 
175
              "textRaw": "Event: 'SIGINT'",
 
176
              "type": "event",
 
177
              "name": "SIGINT",
 
178
              "desc": "<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>Example of listening for <code>SIGINT</code>:\n\n</p>\n<pre><code>rl.on(&#39;SIGINT&#39;, function() {\n  rl.question(&#39;Are you sure you want to exit?&#39;, function(answer) {\n    if (answer.match(/^y(es)?$/i)) rl.pause();\n  });\n});</code></pre>\n",
 
179
              "params": []
 
180
            },
 
181
            {
 
182
              "textRaw": "Event: 'SIGTSTP'",
 
183
              "type": "event",
 
184
              "name": "SIGTSTP",
 
185
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><strong>This does not work on 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>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>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>Example of listening for <code>SIGTSTP</code>:\n\n</p>\n<pre><code>rl.on(&#39;SIGTSTP&#39;, function() {\n  // This will override SIGTSTP and prevent the program from going to the\n  // background.\n  console.log(&#39;Caught SIGTSTP.&#39;);\n});</code></pre>\n",
 
186
              "params": []
 
187
            },
 
188
            {
 
189
              "textRaw": "Event: 'SIGCONT'",
 
190
              "type": "event",
 
191
              "name": "SIGCONT",
 
192
              "desc": "<p><code>function () {}</code>\n\n</p>\n<p><strong>This does not work on 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>Example of listening for <code>SIGCONT</code>:\n\n</p>\n<pre><code>rl.on(&#39;SIGCONT&#39;, function() {\n  // `prompt` will automatically resume the stream\n  rl.prompt();\n});</code></pre>\n<h2>Example: Tiny CLI</h2>\n<p>Here&#39;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(&#39;readline&#39;),\n    rl = readline.createInterface(process.stdin, process.stdout);\n\nrl.setPrompt(&#39;OHAI&gt; &#39;);\nrl.prompt();\n\nrl.on(&#39;line&#39;, function(line) {\n  switch(line.trim()) {\n    case &#39;hello&#39;:\n      console.log(&#39;world!&#39;);\n      break;\n    default:\n      console.log(&#39;Say what? I might have heard `&#39; + line.trim() + &#39;`&#39;);\n      break;\n  }\n  rl.prompt();\n}).on(&#39;close&#39;, function() {\n  console.log(&#39;Have a great day!&#39;);\n  process.exit(0);\n});</code></pre>\n",
 
193
              "params": []
 
194
            }
 
195
          ],
 
196
          "type": "module",
 
197
          "displayName": "Events"
 
198
        }
 
199
      ],
 
200
      "type": "module",
 
201
      "displayName": "Readline"
 
202
    }
 
203
  ]
 
204
}