UNPKG

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