@nghyane/arcane-tui 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3 -0
- package/README.md +704 -0
- package/package.json +72 -0
- package/src/autocomplete.ts +772 -0
- package/src/buffer/ansi-parser.ts +349 -0
- package/src/buffer/buffer.ts +120 -0
- package/src/buffer/cell.ts +103 -0
- package/src/buffer/index.ts +16 -0
- package/src/buffer/render.ts +149 -0
- package/src/components/box.ts +144 -0
- package/src/components/cancellable-loader.ts +39 -0
- package/src/components/editor.ts +2289 -0
- package/src/components/image.ts +86 -0
- package/src/components/input.ts +531 -0
- package/src/components/loader.ts +59 -0
- package/src/components/markdown.ts +858 -0
- package/src/components/select-list.ts +198 -0
- package/src/components/settings-list.ts +194 -0
- package/src/components/spacer.ts +28 -0
- package/src/components/tab-bar.ts +142 -0
- package/src/components/text.ts +110 -0
- package/src/components/truncated-text.ts +61 -0
- package/src/editor-component.ts +71 -0
- package/src/fuzzy.ts +143 -0
- package/src/index.ts +69 -0
- package/src/keybindings.ts +197 -0
- package/src/keys.ts +270 -0
- package/src/kill-ring.ts +46 -0
- package/src/mermaid.ts +140 -0
- package/src/stdin-buffer.ts +385 -0
- package/src/symbols.ts +24 -0
- package/src/terminal-capabilities.ts +393 -0
- package/src/terminal.ts +467 -0
- package/src/ttyid.ts +66 -0
- package/src/tui.ts +1134 -0
- package/src/utils.ts +149 -0
|
@@ -0,0 +1,385 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* StdinBuffer buffers input and emits complete sequences.
|
|
3
|
+
*
|
|
4
|
+
* This is necessary because stdin data events can arrive in partial chunks,
|
|
5
|
+
* especially for escape sequences like mouse events. Without buffering,
|
|
6
|
+
* partial sequences can be misinterpreted as regular keypresses.
|
|
7
|
+
*
|
|
8
|
+
* For example, the mouse SGR sequence `\x1b[<35;20;5m` might arrive as:
|
|
9
|
+
* - Event 1: `\x1b`
|
|
10
|
+
* - Event 2: `[<35`
|
|
11
|
+
* - Event 3: `;20;5m`
|
|
12
|
+
*
|
|
13
|
+
* The buffer accumulates these until a complete sequence is detected.
|
|
14
|
+
* Call the `process()` method to feed input data.
|
|
15
|
+
*
|
|
16
|
+
* Based on code from OpenTUI (https://github.com/anomalyco/opentui)
|
|
17
|
+
* MIT License - Copyright (c) 2025 opentui
|
|
18
|
+
*/
|
|
19
|
+
import { EventEmitter } from "events";
|
|
20
|
+
|
|
21
|
+
const ESC = "\x1b";
|
|
22
|
+
const BRACKETED_PASTE_START = "\x1b[200~";
|
|
23
|
+
const BRACKETED_PASTE_END = "\x1b[201~";
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Check if a string is a complete escape sequence or needs more data
|
|
27
|
+
*/
|
|
28
|
+
function isCompleteSequence(data: string): "complete" | "incomplete" | "not-escape" {
|
|
29
|
+
if (!data.startsWith(ESC)) {
|
|
30
|
+
return "not-escape";
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (data.length === 1) {
|
|
34
|
+
return "incomplete";
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const afterEsc = data.slice(1);
|
|
38
|
+
|
|
39
|
+
// CSI sequences: ESC [
|
|
40
|
+
if (afterEsc.startsWith("[")) {
|
|
41
|
+
// Check for old-style mouse sequence: ESC[M + 3 bytes
|
|
42
|
+
if (afterEsc.startsWith("[M")) {
|
|
43
|
+
// Old-style mouse needs ESC[M + 3 bytes = 6 total
|
|
44
|
+
return data.length >= 6 ? "complete" : "incomplete";
|
|
45
|
+
}
|
|
46
|
+
return isCompleteCsiSequence(data);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// OSC sequences: ESC ]
|
|
50
|
+
if (afterEsc.startsWith("]")) {
|
|
51
|
+
return isCompleteOscSequence(data);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// DCS sequences: ESC P ... ESC \ (includes XTVersion responses)
|
|
55
|
+
if (afterEsc.startsWith("P")) {
|
|
56
|
+
return isCompleteDcsSequence(data);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// APC sequences: ESC _ ... ESC \ (includes Kitty graphics responses)
|
|
60
|
+
if (afterEsc.startsWith("_")) {
|
|
61
|
+
return isCompleteApcSequence(data);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// SS3 sequences: ESC O
|
|
65
|
+
if (afterEsc.startsWith("O")) {
|
|
66
|
+
// ESC O followed by a single character
|
|
67
|
+
return afterEsc.length >= 2 ? "complete" : "incomplete";
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Meta key sequences: ESC followed by a single character
|
|
71
|
+
if (afterEsc.length === 1) {
|
|
72
|
+
return "complete";
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// Unknown escape sequence - treat as complete
|
|
76
|
+
return "complete";
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Check if CSI sequence is complete
|
|
81
|
+
* CSI sequences: ESC [ ... followed by a final byte (0x40-0x7E)
|
|
82
|
+
*/
|
|
83
|
+
function isCompleteCsiSequence(data: string): "complete" | "incomplete" {
|
|
84
|
+
if (!data.startsWith(`${ESC}[`)) {
|
|
85
|
+
return "complete";
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Need at least ESC [ and one more character
|
|
89
|
+
if (data.length < 3) {
|
|
90
|
+
return "incomplete";
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const payload = data.slice(2);
|
|
94
|
+
|
|
95
|
+
// CSI sequences end with a byte in the range 0x40-0x7E (@-~)
|
|
96
|
+
// This includes all letters and several special characters
|
|
97
|
+
const lastChar = payload[payload.length - 1];
|
|
98
|
+
const lastCharCode = lastChar.charCodeAt(0);
|
|
99
|
+
|
|
100
|
+
if (lastCharCode >= 0x40 && lastCharCode <= 0x7e) {
|
|
101
|
+
// Special handling for SGR mouse sequences
|
|
102
|
+
// Format: ESC[<B;X;Ym or ESC[<B;X;YM
|
|
103
|
+
if (payload.startsWith("<")) {
|
|
104
|
+
// Must have format: <digits;digits;digits[Mm]
|
|
105
|
+
const mouseMatch = /^<\d+;\d+;\d+[Mm]$/.test(payload);
|
|
106
|
+
if (mouseMatch) {
|
|
107
|
+
return "complete";
|
|
108
|
+
}
|
|
109
|
+
// If it ends with M or m but doesn't match the pattern, still incomplete
|
|
110
|
+
if (lastChar === "M" || lastChar === "m") {
|
|
111
|
+
// Check if we have the right structure
|
|
112
|
+
const parts = payload.slice(1, -1).split(";");
|
|
113
|
+
if (parts.length === 3 && parts.every(p => /^\d+$/.test(p))) {
|
|
114
|
+
return "complete";
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
return "incomplete";
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return "complete";
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return "incomplete";
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Check if OSC sequence is complete
|
|
129
|
+
* OSC sequences: ESC ] ... ST (where ST is ESC \ or BEL)
|
|
130
|
+
*/
|
|
131
|
+
function isCompleteOscSequence(data: string): "complete" | "incomplete" {
|
|
132
|
+
if (!data.startsWith(`${ESC}]`)) {
|
|
133
|
+
return "complete";
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// OSC sequences end with ST (ESC \) or BEL (\x07)
|
|
137
|
+
if (data.endsWith(`${ESC}\\`) || data.endsWith("\x07")) {
|
|
138
|
+
return "complete";
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return "incomplete";
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Check if DCS (Device Control String) sequence is complete
|
|
146
|
+
* DCS sequences: ESC P ... ST (where ST is ESC \)
|
|
147
|
+
* Used for XTVersion responses like ESC P >| ... ESC \
|
|
148
|
+
*/
|
|
149
|
+
function isCompleteDcsSequence(data: string): "complete" | "incomplete" {
|
|
150
|
+
if (!data.startsWith(`${ESC}P`)) {
|
|
151
|
+
return "complete";
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// DCS sequences end with ST (ESC \)
|
|
155
|
+
if (data.endsWith(`${ESC}\\`)) {
|
|
156
|
+
return "complete";
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
return "incomplete";
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Check if APC (Application Program Command) sequence is complete
|
|
164
|
+
* APC sequences: ESC _ ... ST (where ST is ESC \)
|
|
165
|
+
* Used for Kitty graphics responses like ESC _ G ... ESC \
|
|
166
|
+
*/
|
|
167
|
+
function isCompleteApcSequence(data: string): "complete" | "incomplete" {
|
|
168
|
+
if (!data.startsWith(`${ESC}_`)) {
|
|
169
|
+
return "complete";
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// APC sequences end with ST (ESC \)
|
|
173
|
+
if (data.endsWith(`${ESC}\\`)) {
|
|
174
|
+
return "complete";
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
return "incomplete";
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Split accumulated buffer into complete sequences
|
|
182
|
+
*/
|
|
183
|
+
function extractCompleteSequences(buffer: string): { sequences: string[]; remainder: string } {
|
|
184
|
+
const sequences: string[] = [];
|
|
185
|
+
let pos = 0;
|
|
186
|
+
|
|
187
|
+
while (pos < buffer.length) {
|
|
188
|
+
const remaining = buffer.slice(pos);
|
|
189
|
+
|
|
190
|
+
// Try to extract a sequence starting at this position
|
|
191
|
+
if (remaining.startsWith(ESC)) {
|
|
192
|
+
// Find the end of this escape sequence
|
|
193
|
+
let seqEnd = 1;
|
|
194
|
+
while (seqEnd <= remaining.length) {
|
|
195
|
+
const candidate = remaining.slice(0, seqEnd);
|
|
196
|
+
const status = isCompleteSequence(candidate);
|
|
197
|
+
|
|
198
|
+
if (status === "complete") {
|
|
199
|
+
sequences.push(candidate);
|
|
200
|
+
pos += seqEnd;
|
|
201
|
+
break;
|
|
202
|
+
} else if (status === "incomplete") {
|
|
203
|
+
seqEnd++;
|
|
204
|
+
} else {
|
|
205
|
+
// Should not happen when starting with ESC
|
|
206
|
+
sequences.push(candidate);
|
|
207
|
+
pos += seqEnd;
|
|
208
|
+
break;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
if (seqEnd > remaining.length) {
|
|
213
|
+
return { sequences, remainder: remaining };
|
|
214
|
+
}
|
|
215
|
+
} else {
|
|
216
|
+
// Not an escape sequence - take a single character
|
|
217
|
+
sequences.push(remaining[0]!);
|
|
218
|
+
pos++;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return { sequences, remainder: "" };
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export type StdinBufferOptions = {
|
|
226
|
+
/**
|
|
227
|
+
* Maximum time to wait for sequence completion (default: 10ms)
|
|
228
|
+
* After this time, the buffer is flushed even if incomplete
|
|
229
|
+
*/
|
|
230
|
+
timeout?: number;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
export type StdinBufferEventMap = {
|
|
234
|
+
data: [string];
|
|
235
|
+
paste: [string];
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Buffers stdin input and emits complete sequences via the 'data' event.
|
|
240
|
+
* Handles partial escape sequences that arrive across multiple chunks.
|
|
241
|
+
*/
|
|
242
|
+
export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
|
|
243
|
+
#buffer: string = "";
|
|
244
|
+
#timeout?: NodeJS.Timeout;
|
|
245
|
+
readonly #timeoutMs: number;
|
|
246
|
+
#pasteMode: boolean = false;
|
|
247
|
+
#pasteBuffer: string = "";
|
|
248
|
+
|
|
249
|
+
constructor(options: StdinBufferOptions = {}) {
|
|
250
|
+
super();
|
|
251
|
+
this.#timeoutMs = options.timeout ?? 10;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
process(data: string | Buffer): void {
|
|
255
|
+
// Clear any pending timeout
|
|
256
|
+
if (this.#timeout) {
|
|
257
|
+
clearTimeout(this.#timeout);
|
|
258
|
+
this.#timeout = undefined;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
// Handle high-byte conversion (for compatibility with parseKeypress)
|
|
262
|
+
// If buffer has single byte > 127, convert to ESC + (byte - 128)
|
|
263
|
+
let str: string;
|
|
264
|
+
if (Buffer.isBuffer(data)) {
|
|
265
|
+
if (data.length === 1 && data[0]! > 127) {
|
|
266
|
+
const byte = data[0]! - 128;
|
|
267
|
+
str = `\x1b${String.fromCharCode(byte)}`;
|
|
268
|
+
} else {
|
|
269
|
+
str = data.toString();
|
|
270
|
+
}
|
|
271
|
+
} else {
|
|
272
|
+
str = data;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (str.length === 0 && this.#buffer.length === 0) {
|
|
276
|
+
this.emit("data", "");
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
this.#buffer += str;
|
|
281
|
+
|
|
282
|
+
if (this.#pasteMode) {
|
|
283
|
+
this.#pasteBuffer += this.#buffer;
|
|
284
|
+
this.#buffer = "";
|
|
285
|
+
|
|
286
|
+
const endIndex = this.#pasteBuffer.indexOf(BRACKETED_PASTE_END);
|
|
287
|
+
if (endIndex !== -1) {
|
|
288
|
+
const pastedContent = this.#pasteBuffer.slice(0, endIndex);
|
|
289
|
+
const remaining = this.#pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
|
|
290
|
+
|
|
291
|
+
this.#pasteMode = false;
|
|
292
|
+
this.#pasteBuffer = "";
|
|
293
|
+
|
|
294
|
+
this.emit("paste", pastedContent);
|
|
295
|
+
|
|
296
|
+
if (remaining.length > 0) {
|
|
297
|
+
this.process(remaining);
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const startIndex = this.#buffer.indexOf(BRACKETED_PASTE_START);
|
|
304
|
+
if (startIndex !== -1) {
|
|
305
|
+
if (startIndex > 0) {
|
|
306
|
+
const beforePaste = this.#buffer.slice(0, startIndex);
|
|
307
|
+
const result = extractCompleteSequences(beforePaste);
|
|
308
|
+
for (const sequence of result.sequences) {
|
|
309
|
+
this.emit("data", sequence);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
this.#buffer = this.#buffer.slice(startIndex + BRACKETED_PASTE_START.length);
|
|
314
|
+
this.#pasteMode = true;
|
|
315
|
+
this.#pasteBuffer = this.#buffer;
|
|
316
|
+
this.#buffer = "";
|
|
317
|
+
|
|
318
|
+
const endIndex = this.#pasteBuffer.indexOf(BRACKETED_PASTE_END);
|
|
319
|
+
if (endIndex !== -1) {
|
|
320
|
+
const pastedContent = this.#pasteBuffer.slice(0, endIndex);
|
|
321
|
+
const remaining = this.#pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
|
|
322
|
+
|
|
323
|
+
this.#pasteMode = false;
|
|
324
|
+
this.#pasteBuffer = "";
|
|
325
|
+
|
|
326
|
+
this.emit("paste", pastedContent);
|
|
327
|
+
|
|
328
|
+
if (remaining.length > 0) {
|
|
329
|
+
this.process(remaining);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
const result = extractCompleteSequences(this.#buffer);
|
|
336
|
+
this.#buffer = result.remainder;
|
|
337
|
+
|
|
338
|
+
for (const sequence of result.sequences) {
|
|
339
|
+
this.emit("data", sequence);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (this.#buffer.length > 0) {
|
|
343
|
+
this.#timeout = setTimeout(() => {
|
|
344
|
+
const flushed = this.flush();
|
|
345
|
+
|
|
346
|
+
for (const sequence of flushed) {
|
|
347
|
+
this.emit("data", sequence);
|
|
348
|
+
}
|
|
349
|
+
}, this.#timeoutMs);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
flush(): string[] {
|
|
354
|
+
if (this.#timeout) {
|
|
355
|
+
clearTimeout(this.#timeout);
|
|
356
|
+
this.#timeout = undefined;
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
if (this.#buffer.length === 0) {
|
|
360
|
+
return [];
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
const sequences = [this.#buffer];
|
|
364
|
+
this.#buffer = "";
|
|
365
|
+
return sequences;
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
clear(): void {
|
|
369
|
+
if (this.#timeout) {
|
|
370
|
+
clearTimeout(this.#timeout);
|
|
371
|
+
this.#timeout = undefined;
|
|
372
|
+
}
|
|
373
|
+
this.#buffer = "";
|
|
374
|
+
this.#pasteMode = false;
|
|
375
|
+
this.#pasteBuffer = "";
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
getBuffer(): string {
|
|
379
|
+
return this.#buffer;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
destroy(): void {
|
|
383
|
+
this.clear();
|
|
384
|
+
}
|
|
385
|
+
}
|
package/src/symbols.ts
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface BoxSymbols {
|
|
2
|
+
topLeft: string;
|
|
3
|
+
topRight: string;
|
|
4
|
+
bottomLeft: string;
|
|
5
|
+
bottomRight: string;
|
|
6
|
+
horizontal: string;
|
|
7
|
+
vertical: string;
|
|
8
|
+
teeDown: string;
|
|
9
|
+
teeUp: string;
|
|
10
|
+
teeLeft: string;
|
|
11
|
+
teeRight: string;
|
|
12
|
+
cross: string;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface SymbolTheme {
|
|
16
|
+
cursor: string;
|
|
17
|
+
inputCursor: string;
|
|
18
|
+
boxRound: Omit<BoxSymbols, "teeDown" | "teeUp" | "teeLeft" | "teeRight" | "cross">;
|
|
19
|
+
boxSharp: BoxSymbols;
|
|
20
|
+
table: BoxSymbols;
|
|
21
|
+
quoteBorder: string;
|
|
22
|
+
hrChar: string;
|
|
23
|
+
spinnerFrames: string[];
|
|
24
|
+
}
|