UNPKG

26.5 kBTypeScriptView Raw
1/**
2 * The `readline` module provides an interface for reading data from a `Readable` stream (such as `process.stdin`) one line at a time.
3 *
4 * To use the promise-based APIs:
5 *
6 * ```js
7 * import * as readline from 'node:readline/promises';
8 * ```
9 *
10 * To use the callback and sync APIs:
11 *
12 * ```js
13 * import * as readline from 'node:readline';
14 * ```
15 *
16 * The following simple example illustrates the basic use of the `readline` module.
17 *
18 * ```js
19 * import * as readline from 'node:readline/promises';
20 * import { stdin as input, stdout as output } from 'process';
21 *
22 * const rl = readline.createInterface({ input, output });
23 *
24 * const answer = await rl.question('What do you think of Node.js? ');
25 *
26 * console.log(`Thank you for your valuable feedback: ${answer}`);
27 *
28 * rl.close();
29 * ```
30 *
31 * Once this code is invoked, the Node.js application will not terminate until the`readline.Interface` is closed because the interface waits for data to be
32 * received on the `input` stream.
33 * @see [source](https://github.com/nodejs/node/blob/v17.0.0/lib/readline.js)
34 */
35declare module 'readline' {
36 import { Abortable, EventEmitter } from 'node:events';
37 interface Key {
38 sequence?: string | undefined;
39 name?: string | undefined;
40 ctrl?: boolean | undefined;
41 meta?: boolean | undefined;
42 shift?: boolean | undefined;
43 }
44 /**
45 * Instances of the `readline.Interface` class are constructed using the`readline.createInterface()` method. Every instance is associated with a
46 * single `input` `Readable` stream and a single `output` `Writable` stream.
47 * The `output` stream is used to print prompts for user input that arrives on,
48 * and is read from, the `input` stream.
49 * @since v0.1.104
50 */
51 class Interface extends EventEmitter {
52 readonly terminal: boolean;
53 /**
54 * The current input data being processed by node.
55 *
56 * This can be used when collecting input from a TTY stream to retrieve the
57 * current value that has been processed thus far, prior to the `line` event
58 * being emitted. Once the `line` event has been emitted, this property will
59 * be an empty string.
60 *
61 * Be aware that modifying the value during the instance runtime may have
62 * unintended consequences if `rl.cursor` is not also controlled.
63 *
64 * **If not using a TTY stream for input, use the `'line'` event.**
65 *
66 * One possible use case would be as follows:
67 *
68 * ```js
69 * const values = ['lorem ipsum', 'dolor sit amet'];
70 * const rl = readline.createInterface(process.stdin);
71 * const showResults = debounce(() => {
72 * console.log(
73 * '\n',
74 * values.filter((val) => val.startsWith(rl.line)).join(' ')
75 * );
76 * }, 300);
77 * process.stdin.on('keypress', (c, k) => {
78 * showResults();
79 * });
80 * ```
81 * @since v0.1.98
82 */
83 readonly line: string;
84 /**
85 * The cursor position relative to `rl.line`.
86 *
87 * This will track where the current cursor lands in the input string, when
88 * reading input from a TTY stream. The position of cursor determines the
89 * portion of the input string that will be modified as input is processed,
90 * as well as the column where the terminal caret will be rendered.
91 * @since v0.1.98
92 */
93 readonly cursor: number;
94 /**
95 * NOTE: According to the documentation:
96 *
97 * > Instances of the `readline.Interface` class are constructed using the
98 * > `readline.createInterface()` method.
99 *
100 * @see https://nodejs.org/dist/latest-v10.x/docs/api/readline.html#readline_class_interface
101 */
102 protected constructor(input: NodeJS.ReadableStream, output?: NodeJS.WritableStream, completer?: Completer | AsyncCompleter, terminal?: boolean);
103 /**
104 * NOTE: According to the documentation:
105 *
106 * > Instances of the `readline.Interface` class are constructed using the
107 * > `readline.createInterface()` method.
108 *
109 * @see https://nodejs.org/dist/latest-v10.x/docs/api/readline.html#readline_class_interface
110 */
111 protected constructor(options: ReadLineOptions);
112 /**
113 * The `rl.getPrompt()` method returns the current prompt used by `rl.prompt()`.
114 * @since v15.3.0
115 * @return the current prompt string
116 */
117 getPrompt(): string;
118 /**
119 * The `rl.setPrompt()` method sets the prompt that will be written to `output`whenever `rl.prompt()` is called.
120 * @since v0.1.98
121 */
122 setPrompt(prompt: string): void;
123 /**
124 * The `rl.prompt()` method writes the `readline.Interface` instances configured`prompt` to a new line in `output` in order to provide a user with a new
125 * location at which to provide input.
126 *
127 * When called, `rl.prompt()` will resume the `input` stream if it has been
128 * paused.
129 *
130 * If the `readline.Interface` was created with `output` set to `null` or`undefined` the prompt is not written.
131 * @since v0.1.98
132 * @param preserveCursor If `true`, prevents the cursor placement from being reset to `0`.
133 */
134 prompt(preserveCursor?: boolean): void;
135 /**
136 * The `rl.question()` method displays the `query` by writing it to the `output`,
137 * waits for user input to be provided on `input`, then invokes the `callback`function passing the provided input as the first argument.
138 *
139 * When called, `rl.question()` will resume the `input` stream if it has been
140 * paused.
141 *
142 * If the `readline.Interface` was created with `output` set to `null` or`undefined` the `query` is not written.
143 *
144 * The `callback` function passed to `rl.question()` does not follow the typical
145 * pattern of accepting an `Error` object or `null` as the first argument.
146 * The `callback` is called with the provided answer as the only argument.
147 *
148 * Example usage:
149 *
150 * ```js
151 * rl.question('What is your favorite food? ', (answer) => {
152 * console.log(`Oh, so your favorite food is ${answer}`);
153 * });
154 * ```
155 *
156 * Using an `AbortController` to cancel a question.
157 *
158 * ```js
159 * const ac = new AbortController();
160 * const signal = ac.signal;
161 *
162 * rl.question('What is your favorite food? ', { signal }, (answer) => {
163 * console.log(`Oh, so your favorite food is ${answer}`);
164 * });
165 *
166 * signal.addEventListener('abort', () => {
167 * console.log('The food question timed out');
168 * }, { once: true });
169 *
170 * setTimeout(() => ac.abort(), 10000);
171 * ```
172 *
173 * If this method is invoked as it's util.promisify()ed version, it returns a
174 * Promise that fulfills with the answer. If the question is canceled using
175 * an `AbortController` it will reject with an `AbortError`.
176 *
177 * ```js
178 * const util = require('util');
179 * const question = util.promisify(rl.question).bind(rl);
180 *
181 * async function questionExample() {
182 * try {
183 * const answer = await question('What is you favorite food? ');
184 * console.log(`Oh, so your favorite food is ${answer}`);
185 * } catch (err) {
186 * console.error('Question rejected', err);
187 * }
188 * }
189 * questionExample();
190 * ```
191 * @since v0.3.3
192 * @param query A statement or query to write to `output`, prepended to the prompt.
193 * @param callback A callback function that is invoked with the user's input in response to the `query`.
194 */
195 question(query: string, callback: (answer: string) => void): void;
196 question(query: string, options: Abortable, callback: (answer: string) => void): void;
197 /**
198 * The `rl.pause()` method pauses the `input` stream, allowing it to be resumed
199 * later if necessary.
200 *
201 * Calling `rl.pause()` does not immediately pause other events (including`'line'`) from being emitted by the `readline.Interface` instance.
202 * @since v0.3.4
203 */
204 pause(): this;
205 /**
206 * The `rl.resume()` method resumes the `input` stream if it has been paused.
207 * @since v0.3.4
208 */
209 resume(): this;
210 /**
211 * The `rl.close()` method closes the `readline.Interface` instance and
212 * relinquishes control over the `input` and `output` streams. When called,
213 * the `'close'` event will be emitted.
214 *
215 * Calling `rl.close()` does not immediately stop other events (including `'line'`)
216 * from being emitted by the `readline.Interface` instance.
217 * @since v0.1.98
218 */
219 close(): void;
220 /**
221 * The `rl.write()` method will write either `data` or a key sequence identified
222 * by `key` to the `output`. The `key` argument is supported only if `output` is
223 * a `TTY` text terminal. See `TTY keybindings` for a list of key
224 * combinations.
225 *
226 * If `key` is specified, `data` is ignored.
227 *
228 * When called, `rl.write()` will resume the `input` stream if it has been
229 * paused.
230 *
231 * If the `readline.Interface` was created with `output` set to `null` or`undefined` the `data` and `key` are not written.
232 *
233 * ```js
234 * rl.write('Delete this!');
235 * // Simulate Ctrl+U to delete the line written previously
236 * rl.write(null, { ctrl: true, name: 'u' });
237 * ```
238 *
239 * The `rl.write()` method will write the data to the `readline` `Interface`'s`input`_as if it were provided by the user_.
240 * @since v0.1.98
241 */
242 write(data: string | Buffer, key?: Key): void;
243 write(data: undefined | null | string | Buffer, key: Key): void;
244 /**
245 * Returns the real position of the cursor in relation to the input
246 * prompt + string. Long input (wrapping) strings, as well as multiple
247 * line prompts are included in the calculations.
248 * @since v13.5.0, v12.16.0
249 */
250 getCursorPos(): CursorPos;
251 /**
252 * events.EventEmitter
253 * 1. close
254 * 2. line
255 * 3. pause
256 * 4. resume
257 * 5. SIGCONT
258 * 6. SIGINT
259 * 7. SIGTSTP
260 * 8. history
261 */
262 addListener(event: string, listener: (...args: any[]) => void): this;
263 addListener(event: 'close', listener: () => void): this;
264 addListener(event: 'line', listener: (input: string) => void): this;
265 addListener(event: 'pause', listener: () => void): this;
266 addListener(event: 'resume', listener: () => void): this;
267 addListener(event: 'SIGCONT', listener: () => void): this;
268 addListener(event: 'SIGINT', listener: () => void): this;
269 addListener(event: 'SIGTSTP', listener: () => void): this;
270 addListener(event: 'history', listener: (history: string[]) => void): this;
271 emit(event: string | symbol, ...args: any[]): boolean;
272 emit(event: 'close'): boolean;
273 emit(event: 'line', input: string): boolean;
274 emit(event: 'pause'): boolean;
275 emit(event: 'resume'): boolean;
276 emit(event: 'SIGCONT'): boolean;
277 emit(event: 'SIGINT'): boolean;
278 emit(event: 'SIGTSTP'): boolean;
279 emit(event: 'history', history: string[]): boolean;
280 on(event: string, listener: (...args: any[]) => void): this;
281 on(event: 'close', listener: () => void): this;
282 on(event: 'line', listener: (input: string) => void): this;
283 on(event: 'pause', listener: () => void): this;
284 on(event: 'resume', listener: () => void): this;
285 on(event: 'SIGCONT', listener: () => void): this;
286 on(event: 'SIGINT', listener: () => void): this;
287 on(event: 'SIGTSTP', listener: () => void): this;
288 on(event: 'history', listener: (history: string[]) => void): this;
289 once(event: string, listener: (...args: any[]) => void): this;
290 once(event: 'close', listener: () => void): this;
291 once(event: 'line', listener: (input: string) => void): this;
292 once(event: 'pause', listener: () => void): this;
293 once(event: 'resume', listener: () => void): this;
294 once(event: 'SIGCONT', listener: () => void): this;
295 once(event: 'SIGINT', listener: () => void): this;
296 once(event: 'SIGTSTP', listener: () => void): this;
297 once(event: 'history', listener: (history: string[]) => void): this;
298 prependListener(event: string, listener: (...args: any[]) => void): this;
299 prependListener(event: 'close', listener: () => void): this;
300 prependListener(event: 'line', listener: (input: string) => void): this;
301 prependListener(event: 'pause', listener: () => void): this;
302 prependListener(event: 'resume', listener: () => void): this;
303 prependListener(event: 'SIGCONT', listener: () => void): this;
304 prependListener(event: 'SIGINT', listener: () => void): this;
305 prependListener(event: 'SIGTSTP', listener: () => void): this;
306 prependListener(event: 'history', listener: (history: string[]) => void): this;
307 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
308 prependOnceListener(event: 'close', listener: () => void): this;
309 prependOnceListener(event: 'line', listener: (input: string) => void): this;
310 prependOnceListener(event: 'pause', listener: () => void): this;
311 prependOnceListener(event: 'resume', listener: () => void): this;
312 prependOnceListener(event: 'SIGCONT', listener: () => void): this;
313 prependOnceListener(event: 'SIGINT', listener: () => void): this;
314 prependOnceListener(event: 'SIGTSTP', listener: () => void): this;
315 prependOnceListener(event: 'history', listener: (history: string[]) => void): this;
316 [Symbol.asyncIterator](): AsyncIterableIterator<string>;
317 }
318 type ReadLine = Interface; // type forwarded for backwards compatibility
319 type Completer = (line: string) => CompleterResult;
320 type AsyncCompleter = (line: string, callback: (err?: null | Error, result?: CompleterResult) => void) => void;
321 type CompleterResult = [string[], string];
322 interface ReadLineOptions {
323 input: NodeJS.ReadableStream;
324 output?: NodeJS.WritableStream | undefined;
325 completer?: Completer | AsyncCompleter | undefined;
326 terminal?: boolean | undefined;
327 /**
328 * Initial list of history lines. This option makes sense
329 * only if `terminal` is set to `true` by the user or by an internal `output`
330 * check, otherwise the history caching mechanism is not initialized at all.
331 * @default []
332 */
333 history?: string[] | undefined;
334 historySize?: number | undefined;
335 prompt?: string | undefined;
336 crlfDelay?: number | undefined;
337 /**
338 * If `true`, when a new input line added
339 * to the history list duplicates an older one, this removes the older line
340 * from the list.
341 * @default false
342 */
343 removeHistoryDuplicates?: boolean | undefined;
344 escapeCodeTimeout?: number | undefined;
345 tabSize?: number | undefined;
346 }
347 /**
348 * The `readline.createInterface()` method creates a new `readline.Interface`instance.
349 *
350 * ```js
351 * const readline = require('readline');
352 * const rl = readline.createInterface({
353 * input: process.stdin,
354 * output: process.stdout
355 * });
356 * ```
357 *
358 * Once the `readline.Interface` instance is created, the most common case is to
359 * listen for the `'line'` event:
360 *
361 * ```js
362 * rl.on('line', (line) => {
363 * console.log(`Received: ${line}`);
364 * });
365 * ```
366 *
367 * If `terminal` is `true` for this instance then the `output` stream will get
368 * the best compatibility if it defines an `output.columns` property and emits
369 * a `'resize'` event on the `output` if or when the columns ever change
370 * (`process.stdout` does this automatically when it is a TTY).
371 *
372 * When creating a `readline.Interface` using `stdin` as input, the program
373 * will not terminate until it receives `EOF` (Ctrl+D on
374 * Linux/macOS, Ctrl+Z followed by Return on
375 * Windows).
376 * If you want your application to exit without waiting for user input, you can `unref()` the standard input stream:
377 *
378 * ```js
379 * process.stdin.unref();
380 * ```
381 * @since v0.1.98
382 */
383 function createInterface(input: NodeJS.ReadableStream, output?: NodeJS.WritableStream, completer?: Completer | AsyncCompleter, terminal?: boolean): Interface;
384 function createInterface(options: ReadLineOptions): Interface;
385 /**
386 * The `readline.emitKeypressEvents()` method causes the given `Readable` stream to begin emitting `'keypress'` events corresponding to received input.
387 *
388 * Optionally, `interface` specifies a `readline.Interface` instance for which
389 * autocompletion is disabled when copy-pasted input is detected.
390 *
391 * If the `stream` is a `TTY`, then it must be in raw mode.
392 *
393 * This is automatically called by any readline instance on its `input` if the`input` is a terminal. Closing the `readline` instance does not stop
394 * the `input` from emitting `'keypress'` events.
395 *
396 * ```js
397 * readline.emitKeypressEvents(process.stdin);
398 * if (process.stdin.isTTY)
399 * process.stdin.setRawMode(true);
400 * ```
401 *
402 * ## Example: Tiny CLI
403 *
404 * The following example illustrates the use of `readline.Interface` class to
405 * implement a small command-line interface:
406 *
407 * ```js
408 * const readline = require('readline');
409 * const rl = readline.createInterface({
410 * input: process.stdin,
411 * output: process.stdout,
412 * prompt: 'OHAI> '
413 * });
414 *
415 * rl.prompt();
416 *
417 * rl.on('line', (line) => {
418 * switch (line.trim()) {
419 * case 'hello':
420 * console.log('world!');
421 * break;
422 * default:
423 * console.log(`Say what? I might have heard '${line.trim()}'`);
424 * break;
425 * }
426 * rl.prompt();
427 * }).on('close', () => {
428 * console.log('Have a great day!');
429 * process.exit(0);
430 * });
431 * ```
432 *
433 * ## Example: Read file stream line-by-Line
434 *
435 * A common use case for `readline` is to consume an input file one line at a
436 * time. The easiest way to do so is leveraging the `fs.ReadStream` API as
437 * well as a `for await...of` loop:
438 *
439 * ```js
440 * const fs = require('fs');
441 * const readline = require('readline');
442 *
443 * async function processLineByLine() {
444 * const fileStream = fs.createReadStream('input.txt');
445 *
446 * const rl = readline.createInterface({
447 * input: fileStream,
448 * crlfDelay: Infinity
449 * });
450 * // Note: we use the crlfDelay option to recognize all instances of CR LF
451 * // ('\r\n') in input.txt as a single line break.
452 *
453 * for await (const line of rl) {
454 * // Each line in input.txt will be successively available here as `line`.
455 * console.log(`Line from file: ${line}`);
456 * }
457 * }
458 *
459 * processLineByLine();
460 * ```
461 *
462 * Alternatively, one could use the `'line'` event:
463 *
464 * ```js
465 * const fs = require('fs');
466 * const readline = require('readline');
467 *
468 * const rl = readline.createInterface({
469 * input: fs.createReadStream('sample.txt'),
470 * crlfDelay: Infinity
471 * });
472 *
473 * rl.on('line', (line) => {
474 * console.log(`Line from file: ${line}`);
475 * });
476 * ```
477 *
478 * Currently, `for await...of` loop can be a bit slower. If `async` / `await`flow and speed are both essential, a mixed approach can be applied:
479 *
480 * ```js
481 * const { once } = require('events');
482 * const { createReadStream } = require('fs');
483 * const { createInterface } = require('readline');
484 *
485 * (async function processLineByLine() {
486 * try {
487 * const rl = createInterface({
488 * input: createReadStream('big-file.txt'),
489 * crlfDelay: Infinity
490 * });
491 *
492 * rl.on('line', (line) => {
493 * // Process the line.
494 * });
495 *
496 * await once(rl, 'close');
497 *
498 * console.log('File processed.');
499 * } catch (err) {
500 * console.error(err);
501 * }
502 * })();
503 * ```
504 * @since v0.7.7
505 */
506 function emitKeypressEvents(stream: NodeJS.ReadableStream, readlineInterface?: Interface): void;
507 type Direction = -1 | 0 | 1;
508 interface CursorPos {
509 rows: number;
510 cols: number;
511 }
512 /**
513 * The `readline.clearLine()` method clears current line of given `TTY` stream
514 * in a specified direction identified by `dir`.
515 * @since v0.7.7
516 * @param callback Invoked once the operation completes.
517 * @return `false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
518 */
519 function clearLine(stream: NodeJS.WritableStream, dir: Direction, callback?: () => void): boolean;
520 /**
521 * The `readline.clearScreenDown()` method clears the given `TTY` stream from
522 * the current position of the cursor down.
523 * @since v0.7.7
524 * @param callback Invoked once the operation completes.
525 * @return `false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
526 */
527 function clearScreenDown(stream: NodeJS.WritableStream, callback?: () => void): boolean;
528 /**
529 * The `readline.cursorTo()` method moves cursor to the specified position in a
530 * given `TTY` `stream`.
531 * @since v0.7.7
532 * @param callback Invoked once the operation completes.
533 * @return `false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
534 */
535 function cursorTo(stream: NodeJS.WritableStream, x: number, y?: number, callback?: () => void): boolean;
536 /**
537 * The `readline.moveCursor()` method moves the cursor _relative_ to its current
538 * position in a given `TTY` `stream`.
539 *
540 * ## Example: Tiny CLI
541 *
542 * The following example illustrates the use of `readline.Interface` class to
543 * implement a small command-line interface:
544 *
545 * ```js
546 * const readline = require('readline');
547 * const rl = readline.createInterface({
548 * input: process.stdin,
549 * output: process.stdout,
550 * prompt: 'OHAI> '
551 * });
552 *
553 * rl.prompt();
554 *
555 * rl.on('line', (line) => {
556 * switch (line.trim()) {
557 * case 'hello':
558 * console.log('world!');
559 * break;
560 * default:
561 * console.log(`Say what? I might have heard '${line.trim()}'`);
562 * break;
563 * }
564 * rl.prompt();
565 * }).on('close', () => {
566 * console.log('Have a great day!');
567 * process.exit(0);
568 * });
569 * ```
570 *
571 * ## Example: Read file stream line-by-Line
572 *
573 * A common use case for `readline` is to consume an input file one line at a
574 * time. The easiest way to do so is leveraging the `fs.ReadStream` API as
575 * well as a `for await...of` loop:
576 *
577 * ```js
578 * const fs = require('fs');
579 * const readline = require('readline');
580 *
581 * async function processLineByLine() {
582 * const fileStream = fs.createReadStream('input.txt');
583 *
584 * const rl = readline.createInterface({
585 * input: fileStream,
586 * crlfDelay: Infinity
587 * });
588 * // Note: we use the crlfDelay option to recognize all instances of CR LF
589 * // ('\r\n') in input.txt as a single line break.
590 *
591 * for await (const line of rl) {
592 * // Each line in input.txt will be successively available here as `line`.
593 * console.log(`Line from file: ${line}`);
594 * }
595 * }
596 *
597 * processLineByLine();
598 * ```
599 *
600 * Alternatively, one could use the `'line'` event:
601 *
602 * ```js
603 * const fs = require('fs');
604 * const readline = require('readline');
605 *
606 * const rl = readline.createInterface({
607 * input: fs.createReadStream('sample.txt'),
608 * crlfDelay: Infinity
609 * });
610 *
611 * rl.on('line', (line) => {
612 * console.log(`Line from file: ${line}`);
613 * });
614 * ```
615 *
616 * Currently, `for await...of` loop can be a bit slower. If `async` / `await`flow and speed are both essential, a mixed approach can be applied:
617 *
618 * ```js
619 * const { once } = require('events');
620 * const { createReadStream } = require('fs');
621 * const { createInterface } = require('readline');
622 *
623 * (async function processLineByLine() {
624 * try {
625 * const rl = createInterface({
626 * input: createReadStream('big-file.txt'),
627 * crlfDelay: Infinity
628 * });
629 *
630 * rl.on('line', (line) => {
631 * // Process the line.
632 * });
633 *
634 * await once(rl, 'close');
635 *
636 * console.log('File processed.');
637 * } catch (err) {
638 * console.error(err);
639 * }
640 * })();
641 * ```
642 * @since v0.7.7
643 * @param callback Invoked once the operation completes.
644 * @return `false` if `stream` wishes for the calling code to wait for the `'drain'` event to be emitted before continuing to write additional data; otherwise `true`.
645 */
646 function moveCursor(stream: NodeJS.WritableStream, dx: number, dy: number, callback?: () => void): boolean;
647}
648declare module 'node:readline' {
649 export * from 'readline';
650}
651
\No newline at end of file