UNPKG

7.36 kBTypeScriptView Raw
1declare module 'node:readline' {
2 export * from 'readline';
3}
4
5declare module 'readline' {
6 import EventEmitter = require('node:events');
7
8 interface Key {
9 sequence?: string;
10 name?: string;
11 ctrl?: boolean;
12 meta?: boolean;
13 shift?: boolean;
14 }
15
16 class Interface extends EventEmitter {
17 readonly terminal: boolean;
18
19 // Need direct access to line/cursor data, for use in external processes
20 // see: https://github.com/nodejs/node/issues/30347
21 /** The current input data */
22 readonly line: string;
23 /** The current cursor position in the input line */
24 readonly cursor: number;
25
26 /**
27 * NOTE: According to the documentation:
28 *
29 * > Instances of the `readline.Interface` class are constructed using the
30 * > `readline.createInterface()` method.
31 *
32 * @see https://nodejs.org/dist/latest-v10.x/docs/api/readline.html#readline_class_interface
33 */
34 protected constructor(input: NodeJS.ReadableStream, output?: NodeJS.WritableStream, completer?: Completer | AsyncCompleter, terminal?: boolean);
35 /**
36 * NOTE: According to the documentation:
37 *
38 * > Instances of the `readline.Interface` class are constructed using the
39 * > `readline.createInterface()` method.
40 *
41 * @see https://nodejs.org/dist/latest-v10.x/docs/api/readline.html#readline_class_interface
42 */
43 protected constructor(options: ReadLineOptions);
44
45 setPrompt(prompt: string): void;
46 prompt(preserveCursor?: boolean): void;
47 question(query: string, callback: (answer: string) => void): void;
48 pause(): this;
49 resume(): this;
50 close(): void;
51 write(data: string | Buffer, key?: Key): void;
52
53 /**
54 * Returns the real position of the cursor in relation to the input
55 * prompt + string. Long input (wrapping) strings, as well as multiple
56 * line prompts are included in the calculations.
57 */
58 getCursorPos(): CursorPos;
59
60 /**
61 * events.EventEmitter
62 * 1. close
63 * 2. line
64 * 3. pause
65 * 4. resume
66 * 5. SIGCONT
67 * 6. SIGINT
68 * 7. SIGTSTP
69 */
70
71 addListener(event: string, listener: (...args: any[]) => void): this;
72 addListener(event: "close", listener: () => void): this;
73 addListener(event: "line", listener: (input: string) => void): this;
74 addListener(event: "pause", listener: () => void): this;
75 addListener(event: "resume", listener: () => void): this;
76 addListener(event: "SIGCONT", listener: () => void): this;
77 addListener(event: "SIGINT", listener: () => void): this;
78 addListener(event: "SIGTSTP", listener: () => void): this;
79
80 emit(event: string | symbol, ...args: any[]): boolean;
81 emit(event: "close"): boolean;
82 emit(event: "line", input: string): boolean;
83 emit(event: "pause"): boolean;
84 emit(event: "resume"): boolean;
85 emit(event: "SIGCONT"): boolean;
86 emit(event: "SIGINT"): boolean;
87 emit(event: "SIGTSTP"): boolean;
88
89 on(event: string, listener: (...args: any[]) => void): this;
90 on(event: "close", listener: () => void): this;
91 on(event: "line", listener: (input: string) => void): this;
92 on(event: "pause", listener: () => void): this;
93 on(event: "resume", listener: () => void): this;
94 on(event: "SIGCONT", listener: () => void): this;
95 on(event: "SIGINT", listener: () => void): this;
96 on(event: "SIGTSTP", listener: () => void): this;
97
98 once(event: string, listener: (...args: any[]) => void): this;
99 once(event: "close", listener: () => void): this;
100 once(event: "line", listener: (input: string) => void): this;
101 once(event: "pause", listener: () => void): this;
102 once(event: "resume", listener: () => void): this;
103 once(event: "SIGCONT", listener: () => void): this;
104 once(event: "SIGINT", listener: () => void): this;
105 once(event: "SIGTSTP", listener: () => void): this;
106
107 prependListener(event: string, listener: (...args: any[]) => void): this;
108 prependListener(event: "close", listener: () => void): this;
109 prependListener(event: "line", listener: (input: string) => void): this;
110 prependListener(event: "pause", listener: () => void): this;
111 prependListener(event: "resume", listener: () => void): this;
112 prependListener(event: "SIGCONT", listener: () => void): this;
113 prependListener(event: "SIGINT", listener: () => void): this;
114 prependListener(event: "SIGTSTP", listener: () => void): this;
115
116 prependOnceListener(event: string, listener: (...args: any[]) => void): this;
117 prependOnceListener(event: "close", listener: () => void): this;
118 prependOnceListener(event: "line", listener: (input: string) => void): this;
119 prependOnceListener(event: "pause", listener: () => void): this;
120 prependOnceListener(event: "resume", listener: () => void): this;
121 prependOnceListener(event: "SIGCONT", listener: () => void): this;
122 prependOnceListener(event: "SIGINT", listener: () => void): this;
123 prependOnceListener(event: "SIGTSTP", listener: () => void): this;
124 [Symbol.asyncIterator](): AsyncIterableIterator<string>;
125 }
126
127 type ReadLine = Interface; // type forwarded for backwards compatibility
128
129 type Completer = (line: string) => CompleterResult;
130 type AsyncCompleter = (line: string, callback: (err?: null | Error, result?: CompleterResult) => void) => any;
131
132 type CompleterResult = [string[], string];
133
134 interface ReadLineOptions {
135 input: NodeJS.ReadableStream;
136 output?: NodeJS.WritableStream;
137 completer?: Completer | AsyncCompleter;
138 terminal?: boolean;
139 historySize?: number;
140 prompt?: string;
141 crlfDelay?: number;
142 removeHistoryDuplicates?: boolean;
143 escapeCodeTimeout?: number;
144 tabSize?: number;
145 }
146
147 function createInterface(input: NodeJS.ReadableStream, output?: NodeJS.WritableStream, completer?: Completer | AsyncCompleter, terminal?: boolean): Interface;
148 function createInterface(options: ReadLineOptions): Interface;
149 function emitKeypressEvents(stream: NodeJS.ReadableStream, readlineInterface?: Interface): void;
150
151 type Direction = -1 | 0 | 1;
152
153 interface CursorPos {
154 rows: number;
155 cols: number;
156 }
157
158 /**
159 * Clears the current line of this WriteStream in a direction identified by `dir`.
160 */
161 function clearLine(stream: NodeJS.WritableStream, dir: Direction, callback?: () => void): boolean;
162 /**
163 * Clears this `WriteStream` from the current cursor down.
164 */
165 function clearScreenDown(stream: NodeJS.WritableStream, callback?: () => void): boolean;
166 /**
167 * Moves this WriteStream's cursor to the specified position.
168 */
169 function cursorTo(stream: NodeJS.WritableStream, x: number, y?: number, callback?: () => void): boolean;
170 /**
171 * Moves this WriteStream's cursor relative to its current position.
172 */
173 function moveCursor(stream: NodeJS.WritableStream, dx: number, dy: number, callback?: () => void): boolean;
174}