-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
278 lines (235 loc) · 6.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/* eslint-disable no-control-regex */
import type readline from 'node:readline';
import { stdin, stdout } from 'node:process';
import { emitKeypressEvents } from '~/emit-keypress';
import { mousepress } from '~/mousepress';
import { keycodes } from '~/keycodes';
import {
createShortcut,
isMousepress,
isPrintableCharacter,
parsePosition,
prioritizeKeymap,
sortShortcutModifier
} from '~/utils';
export * from '~/utils';
const ESC = '\x1b';
const ENABLE_PASTE_BRACKET_MODE = `${ESC}[?2004h`;
const DISABLE_PASTE_BRACKET_MODE = `${ESC}[?2004l`;
const isWindows = globalThis.process.platform === 'win32';
const MAX_PASTE_BUFFER = 1024 * 1024; // 1MB limit for paste buffer
export const enablePaste = (stdout: NodeJS.WriteStream) => {
stdout.write(ENABLE_PASTE_BRACKET_MODE);
};
export const disablePaste = (stdout: NodeJS.WriteStream) => {
stdout.write(DISABLE_PASTE_BRACKET_MODE);
};
export const enableMouse = (stdout: NodeJS.WriteStream) => {
stdout.write(`${ESC}[?1003h`);
};
export const disableMouse = (stdout: NodeJS.WriteStream) => {
stdout.write(`${ESC}[?1003l`);
};
export const cursor = {
hide: (stdout: NodeJS.WriteStream) => {
stdout.write(`${ESC}[?25l`);
},
show: (stdout: NodeJS.WriteStream) => {
stdout.write(`${ESC}[?25h`);
},
position: (stdout: NodeJS.WriteStream) => {
stdout.write(`${ESC}[6n`);
}
};
const hasMatchingModifiers = (a: readline.Key, b) => {
return a.ctrl === b.ctrl && a.shift === b.shift && a.meta === b.meta && a.fn === b.fn;
};
const hasModifier = (key: readline.Key) => {
return key.ctrl || key.shift || key.meta || key.fn;
};
const onExitHandlers = globalThis.onExitHandlers ||= [];
const onExitHandler = () => {
for (const fn of onExitHandlers) {
if (!fn.called) {
fn.called = true;
fn();
}
}
};
if (!globalThis.exitHandlers) {
globalThis.exitHandlers = onExitHandlers;
process.once('uncaughtException', onExitHandler);
process.once('SIGINT', onExitHandler);
process.once('exit', onExitHandler);
}
export const emitKeypress = ({
input = stdin,
output = stdout,
keymap = [],
onKeypress,
onMousepress,
onExit,
handleClose = true,
hideCursor = false,
initialPosition = false,
enablePasteMode = false,
pasteModeTimeout = 100
}: {
// eslint-disable-next-line no-undef
input?: NodeJS.ReadStream;
keymap?: Array<{ sequence: string; shortcut: string }>;
// eslint-disable-next-line no-unused-vars
onKeypress: (input: string, key: readline.Key, close: () => void) => void;
// eslint-disable-next-line no-unused-vars
onMousepress?: (input: string, key: any, close: () => void) => void;
onExit?: () => void;
}) => {
if (!input || (input !== process.stdin && !input.isTTY)) {
throw new Error('Invalid stream passed');
}
const isRaw = input.isRaw;
const sortedShorcuts = new Set();
let closed = false;
let pasting = false;
let initial = true;
let sorted = false;
let buffer = '';
let pasteTimeout: NodeJS.Timeout | null = null;
const clearPasteState = () => {
pasting = false;
buffer = '';
if (pasteTimeout) {
clearTimeout(pasteTimeout);
pasteTimeout = null;
}
};
// eslint-disable-next-line complexity
async function handleKeypress(input: string, key: readline.Key) {
if (initialPosition && initial) {
const parsed = parsePosition(key.sequence);
if (parsed) {
initial = false;
onKeypress('', parsed, close);
return;
}
}
if (key.name === 'paste-start' || /\x1B\[200~/.test(key.sequence)) {
clearPasteState();
pasting = true;
pasteTimeout = setTimeout(clearPasteState, pasteModeTimeout);
return;
}
if (key.name === 'paste-end' || /\x1B\[201~/.test(key.sequence)) {
clearTimeout(pasteTimeout!);
pasteTimeout = null;
if (pasting) {
key.name = 'paste';
key.sequence = buffer.replace(/\x1B\[201~/g, '');
key.ctrl = false;
key.shift = false;
key.meta = false;
key.fn = false;
key.printable = true;
onKeypress(buffer, key, close);
clearPasteState();
}
return;
}
if (pasting) {
if (buffer.length < MAX_PASTE_BUFFER) {
buffer += key.sequence?.replace(/\r/g, '\n') || '';
} else {
clearPasteState();
}
return;
}
if (!buffer && isMousepress(key.sequence, key)) {
const k = mousepress(key.sequence, Buffer.from(key.sequence));
k.mouse = true;
onMousepress?.(k, close);
} else {
let addShortcut = false;
if (typeof keymap === 'function') {
keymap = keymap();
}
if (!sorted) {
keymap = prioritizeKeymap(keymap);
sorted = true;
}
const shortcut = key.shortcut ? sortShortcutModifier(key.shortcut) : createShortcut(key);
if (!key.shortcut && hasModifier(key)) {
key.shortcut = shortcut;
}
for (const mapping of keymap) {
if (mapping.sequence) {
if (key.sequence === mapping.sequence && hasMatchingModifiers(key, mapping)) {
key = { ...key, ...mapping };
addShortcut = false;
break;
}
continue;
}
if (mapping.shortcut && !sortedShorcuts.has(mapping.shortcut)) {
sortedShorcuts.add(mapping.shortcut);
mapping.shortcut = sortShortcutModifier(mapping.shortcut);
}
// Only continue comparison if the custom key mapping does not have a sequence
if ((shortcut === mapping.shortcut) || (key.name && key.name === mapping.shortcut && hasMatchingModifiers(key, mapping))) {
key = { ...key, ...mapping };
addShortcut = false;
break;
}
}
if (/^f[0-9]+$/.test(key.name)) {
addShortcut = true;
}
if (addShortcut) {
key.shortcut ||= shortcut;
}
key.printable ||= isPrintableCharacter(key.sequence);
onKeypress(key.sequence, key, close);
}
}
function close() {
if (closed) return;
if (!isWindows && input.isTTY) input.setRawMode(isRaw);
if (hideCursor) cursor.show(output);
if (onMousepress) disableMouse(output);
if (enablePasteMode) disablePaste(output);
if (onKeypress) input.off('keypress', handleKeypress);
if (pasteTimeout) clearTimeout(pasteTimeout);
closed = true;
input.pause();
onExit?.();
}
emitKeypressEvents(input);
if (onMousepress) {
enableMouse(output);
}
if (enablePasteMode === true) {
enablePaste(output);
}
// Disable automatic character echoing
if (!isWindows && input.isTTY) input.setRawMode(true);
if (hideCursor) cursor.hide(output);
if (onKeypress) input.on('keypress', handleKeypress);
input.setEncoding('utf8');
input.once('pause', close);
input.resume();
if (initialPosition) {
cursor.position(output);
}
if (handleClose !== false) {
onExitHandlers.push(close);
}
return close;
};
export {
createShortcut,
emitKeypressEvents,
isMousepress,
isPrintableCharacter,
keycodes,
mousepress
};
export default emitKeypress;