@@ -23,75 +23,21 @@ program to gracefully exit:
23
23
rl.close();
24
24
});
25
25
26
- ## readline.createInterface(options)
27
-
28
- Creates a readline ` Interface ` instance. Accepts an "options" Object that takes
29
- the following values:
30
-
31
- - ` input ` - the readable stream to listen to (Required).
32
-
33
- - ` output ` - the writable stream to write readline data to (Optional).
34
-
35
- - ` completer ` - an optional function that is used for Tab autocompletion. See
36
- below for an example of using this.
37
-
38
- - ` terminal ` - pass ` true ` if the ` input ` and ` output ` streams should be
39
- treated like a TTY, and have ANSI/VT100 escape codes written to it.
40
- Defaults to checking ` isTTY ` on the ` output ` stream upon instantiation.
41
-
42
- - ` historySize ` - maximum number of history lines retained. Defaults to ` 30 ` .
43
-
44
- The ` completer ` function is given the current line entered by the user, and
45
- is supposed to return an Array with 2 entries:
46
-
47
- 1 . An Array with matching entries for the completion.
48
-
49
- 2 . The substring that was used for the matching.
50
-
51
- Which ends up looking something like:
52
- ` [[substr1, substr2, ...], originalsubstring] ` .
53
-
54
- Example:
55
-
56
- function completer(line) {
57
- var completions = '.help .error .exit .quit .q'.split(' ')
58
- var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })
59
- // show all completions if none found
60
- return [hits.length ? hits : completions, line]
61
- }
62
-
63
- Also ` completer ` can be run in async mode if it accepts two arguments:
64
-
65
- function completer(linePartial, callback) {
66
- callback(null, [['123'], linePartial]);
67
- }
68
-
69
- ` createInterface ` is commonly used with ` process.stdin ` and
70
- ` process.stdout ` in order to accept user input:
71
-
72
- var readline = require('readline');
73
- var rl = readline.createInterface({
74
- input: process.stdin,
75
- output: process.stdout
76
- });
77
-
78
- Once you have a readline instance, you most commonly listen for the
79
- ` "line" ` event.
80
-
81
- If ` terminal ` is ` true ` for this instance then the ` output ` stream will get
82
- the best compatibility if it defines an ` output.columns ` property, and fires
83
- a ` "resize" ` event on the ` output ` if/when the columns ever change
84
- (` process.stdout ` does this automatically when it is a TTY).
85
-
86
26
## Class: Interface
87
27
88
28
The class that represents a readline interface with an input and output
89
29
stream.
90
30
91
- ### rl.setPrompt(prompt )
31
+ ### rl.close( )
92
32
93
- Sets the prompt, for example when you run ` node ` on the command line, you see
94
- ` > ` , which is node.js's prompt.
33
+ Closes the ` Interface ` instance, relinquishing control on the ` input ` and
34
+ ` output ` streams. The "close" event will also be emitted.
35
+
36
+ ### rl.pause()
37
+
38
+ Pauses the readline ` input ` stream, allowing it to be resumed later if needed.
39
+
40
+ Note that this doesn't immediately pause the stream of events. Several events may be emitted after calling ` pause ` , including ` line ` .
95
41
96
42
### rl.prompt([ preserveCursor] )
97
43
@@ -123,20 +69,14 @@ Example usage:
123
69
console.log('Oh, so your favorite food is ' + answer);
124
70
});
125
71
126
- ### rl.pause()
127
-
128
- Pauses the readline ` input ` stream, allowing it to be resumed later if needed.
129
-
130
- Note that this doesn't immediately pause the stream of events. Several events may be emitted after calling ` pause ` , including ` line ` .
131
-
132
72
### rl.resume()
133
73
134
74
Resumes the readline ` input ` stream.
135
75
136
- ### rl.close( )
76
+ ### rl.setPrompt(prompt )
137
77
138
- Closes the ` Interface ` instance, relinquishing control on the ` input ` and
139
- ` output ` streams. The "close" event will also be emitted .
78
+ Sets the prompt, for example when you run ` node ` on the command line, you see
79
+ ` > ` , which is node.js's prompt .
140
80
141
81
### rl.write(data[ , key] )
142
82
@@ -154,6 +94,19 @@ Example:
154
94
155
95
## Events
156
96
97
+ ### Event: 'close'
98
+
99
+ ` function () {} `
100
+
101
+ Emitted when ` close() ` is called.
102
+
103
+ Also emitted when the ` input ` stream receives its "end" event. The ` Interface `
104
+ instance should be considered "finished" once this is emitted. For example, when
105
+ the ` input ` stream receives ` ^D ` , respectively known as ` EOT ` .
106
+
107
+ This event is also called if there is no ` SIGINT ` event listener present when
108
+ the ` input ` stream receives a ` ^C ` , respectively known as ` SIGINT ` .
109
+
157
110
### Event: 'line'
158
111
159
112
` function (line) {} `
@@ -194,18 +147,23 @@ Example of listening for `resume`:
194
147
console.log('Readline resumed.');
195
148
});
196
149
197
- ### Event: 'close '
150
+ ### Event: 'SIGCONT '
198
151
199
152
` function () {} `
200
153
201
- Emitted when ` close() ` is called.
154
+ ** This does not work on Windows. **
202
155
203
- Also emitted when the ` input ` stream receives its "end" event. The ` Interface `
204
- instance should be considered "finished" once this is emitted. For example, when
205
- the ` input ` stream receives ` ^D ` , respectively known as ` EOT ` .
156
+ Emitted whenever the ` input ` stream is sent to the background with ` ^Z ` ,
157
+ respectively known as ` SIGTSTP ` , and then continued with ` fg(1) ` . This event
158
+ only emits if the stream was not paused before sending the program to the
159
+ background.
206
160
207
- This event is also called if there is no ` SIGINT ` event listener present when
208
- the ` input ` stream receives a ` ^C ` , respectively known as ` SIGINT ` .
161
+ Example of listening for ` SIGCONT ` :
162
+
163
+ rl.on('SIGCONT', function() {
164
+ // `prompt` will automatically resume the stream
165
+ rl.prompt();
166
+ });
209
167
210
168
### Event: 'SIGINT'
211
169
@@ -247,25 +205,6 @@ Example of listening for `SIGTSTP`:
247
205
console.log('Caught SIGTSTP.');
248
206
});
249
207
250
- ### Event: 'SIGCONT'
251
-
252
- ` function () {} `
253
-
254
- ** This does not work on Windows.**
255
-
256
- Emitted whenever the ` input ` stream is sent to the background with ` ^Z ` ,
257
- respectively known as ` SIGTSTP ` , and then continued with ` fg(1) ` . This event
258
- only emits if the stream was not paused before sending the program to the
259
- background.
260
-
261
- Example of listening for ` SIGCONT ` :
262
-
263
- rl.on('SIGCONT', function() {
264
- // `prompt` will automatically resume the stream
265
- rl.prompt();
266
- });
267
-
268
-
269
208
## Example: Tiny CLI
270
209
271
210
Here's an example of how to use all these together to craft a tiny command
@@ -292,14 +231,6 @@ line interface:
292
231
process.exit(0);
293
232
});
294
233
295
- ## readline.cursorTo(stream, x, y)
296
-
297
- Move cursor to the specified position in a given TTY stream.
298
-
299
- ## readline.moveCursor(stream, dx, dy)
300
-
301
- Move cursor relative to it's current position in a given TTY stream.
302
-
303
234
## readline.clearLine(stream, dir)
304
235
305
236
Clears current line of given TTY stream in a specified direction.
@@ -312,3 +243,71 @@ Clears current line of given TTY stream in a specified direction.
312
243
## readline.clearScreenDown(stream)
313
244
314
245
Clears the screen from the current position of the cursor down.
246
+
247
+ ## readline.createInterface(options)
248
+
249
+ Creates a readline ` Interface ` instance. Accepts an "options" Object that takes
250
+ the following values:
251
+
252
+ - ` input ` - the readable stream to listen to (Required).
253
+
254
+ - ` output ` - the writable stream to write readline data to (Optional).
255
+
256
+ - ` completer ` - an optional function that is used for Tab autocompletion. See
257
+ below for an example of using this.
258
+
259
+ - ` terminal ` - pass ` true ` if the ` input ` and ` output ` streams should be
260
+ treated like a TTY, and have ANSI/VT100 escape codes written to it.
261
+ Defaults to checking ` isTTY ` on the ` output ` stream upon instantiation.
262
+
263
+ - ` historySize ` - maximum number of history lines retained. Defaults to ` 30 ` .
264
+
265
+ The ` completer ` function is given the current line entered by the user, and
266
+ is supposed to return an Array with 2 entries:
267
+
268
+ 1 . An Array with matching entries for the completion.
269
+
270
+ 2 . The substring that was used for the matching.
271
+
272
+ Which ends up looking something like:
273
+ ` [[substr1, substr2, ...], originalsubstring] ` .
274
+
275
+ Example:
276
+
277
+ function completer(line) {
278
+ var completions = '.help .error .exit .quit .q'.split(' ')
279
+ var hits = completions.filter(function(c) { return c.indexOf(line) == 0 })
280
+ // show all completions if none found
281
+ return [hits.length ? hits : completions, line]
282
+ }
283
+
284
+ Also ` completer ` can be run in async mode if it accepts two arguments:
285
+
286
+ function completer(linePartial, callback) {
287
+ callback(null, [['123'], linePartial]);
288
+ }
289
+
290
+ ` createInterface ` is commonly used with ` process.stdin ` and
291
+ ` process.stdout ` in order to accept user input:
292
+
293
+ var readline = require('readline');
294
+ var rl = readline.createInterface({
295
+ input: process.stdin,
296
+ output: process.stdout
297
+ });
298
+
299
+ Once you have a readline instance, you most commonly listen for the
300
+ ` "line" ` event.
301
+
302
+ If ` terminal ` is ` true ` for this instance then the ` output ` stream will get
303
+ the best compatibility if it defines an ` output.columns ` property, and fires
304
+ a ` "resize" ` event on the ` output ` if/when the columns ever change
305
+ (` process.stdout ` does this automatically when it is a TTY).
306
+
307
+ ## readline.cursorTo(stream, x, y)
308
+
309
+ Move cursor to the specified position in a given TTY stream.
310
+
311
+ ## readline.moveCursor(stream, dx, dy)
312
+
313
+ Move cursor relative to it's current position in a given TTY stream.
0 commit comments