@lenylvt/pi-tui 0.64.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.
Files changed (127) hide show
  1. package/README.md +767 -0
  2. package/dist/autocomplete.d.ts +50 -0
  3. package/dist/autocomplete.d.ts.map +1 -0
  4. package/dist/autocomplete.js +623 -0
  5. package/dist/autocomplete.js.map +1 -0
  6. package/dist/components/box.d.ts +22 -0
  7. package/dist/components/box.d.ts.map +1 -0
  8. package/dist/components/box.js +104 -0
  9. package/dist/components/box.js.map +1 -0
  10. package/dist/components/cancellable-loader.d.ts +22 -0
  11. package/dist/components/cancellable-loader.d.ts.map +1 -0
  12. package/dist/components/cancellable-loader.js +35 -0
  13. package/dist/components/cancellable-loader.js.map +1 -0
  14. package/dist/components/editor.d.ts +244 -0
  15. package/dist/components/editor.d.ts.map +1 -0
  16. package/dist/components/editor.js +1861 -0
  17. package/dist/components/editor.js.map +1 -0
  18. package/dist/components/image.d.ts +28 -0
  19. package/dist/components/image.d.ts.map +1 -0
  20. package/dist/components/image.js +69 -0
  21. package/dist/components/image.js.map +1 -0
  22. package/dist/components/input.d.ts +37 -0
  23. package/dist/components/input.d.ts.map +1 -0
  24. package/dist/components/input.js +426 -0
  25. package/dist/components/input.js.map +1 -0
  26. package/dist/components/loader.d.ts +21 -0
  27. package/dist/components/loader.d.ts.map +1 -0
  28. package/dist/components/loader.js +49 -0
  29. package/dist/components/loader.js.map +1 -0
  30. package/dist/components/markdown.d.ts +95 -0
  31. package/dist/components/markdown.d.ts.map +1 -0
  32. package/dist/components/markdown.js +660 -0
  33. package/dist/components/markdown.js.map +1 -0
  34. package/dist/components/select-list.d.ts +50 -0
  35. package/dist/components/select-list.d.ts.map +1 -0
  36. package/dist/components/select-list.js +159 -0
  37. package/dist/components/select-list.js.map +1 -0
  38. package/dist/components/settings-list.d.ts +50 -0
  39. package/dist/components/settings-list.d.ts.map +1 -0
  40. package/dist/components/settings-list.js +185 -0
  41. package/dist/components/settings-list.js.map +1 -0
  42. package/dist/components/spacer.d.ts +12 -0
  43. package/dist/components/spacer.d.ts.map +1 -0
  44. package/dist/components/spacer.js +23 -0
  45. package/dist/components/spacer.js.map +1 -0
  46. package/dist/components/text.d.ts +19 -0
  47. package/dist/components/text.d.ts.map +1 -0
  48. package/dist/components/text.js +89 -0
  49. package/dist/components/text.js.map +1 -0
  50. package/dist/components/truncated-text.d.ts +13 -0
  51. package/dist/components/truncated-text.d.ts.map +1 -0
  52. package/dist/components/truncated-text.js +51 -0
  53. package/dist/components/truncated-text.js.map +1 -0
  54. package/dist/editor-component.d.ts +39 -0
  55. package/dist/editor-component.d.ts.map +1 -0
  56. package/dist/editor-component.js +2 -0
  57. package/dist/editor-component.js.map +1 -0
  58. package/dist/fuzzy.d.ts +16 -0
  59. package/dist/fuzzy.d.ts.map +1 -0
  60. package/dist/fuzzy.js +107 -0
  61. package/dist/fuzzy.js.map +1 -0
  62. package/dist/index.d.ts +23 -0
  63. package/dist/index.d.ts.map +1 -0
  64. package/dist/index.js +32 -0
  65. package/dist/index.js.map +1 -0
  66. package/dist/keybindings.d.ts +193 -0
  67. package/dist/keybindings.d.ts.map +1 -0
  68. package/dist/keybindings.js +174 -0
  69. package/dist/keybindings.js.map +1 -0
  70. package/dist/keys.d.ts +170 -0
  71. package/dist/keys.d.ts.map +1 -0
  72. package/dist/keys.js +1124 -0
  73. package/dist/keys.js.map +1 -0
  74. package/dist/kill-ring.d.ts +28 -0
  75. package/dist/kill-ring.d.ts.map +1 -0
  76. package/dist/kill-ring.js +44 -0
  77. package/dist/kill-ring.js.map +1 -0
  78. package/dist/stdin-buffer.d.ts +48 -0
  79. package/dist/stdin-buffer.d.ts.map +1 -0
  80. package/dist/stdin-buffer.js +317 -0
  81. package/dist/stdin-buffer.js.map +1 -0
  82. package/dist/terminal-image.d.ts +68 -0
  83. package/dist/terminal-image.d.ts.map +1 -0
  84. package/dist/terminal-image.js +288 -0
  85. package/dist/terminal-image.js.map +1 -0
  86. package/dist/terminal.d.ts +84 -0
  87. package/dist/terminal.d.ts.map +1 -0
  88. package/dist/terminal.js +285 -0
  89. package/dist/terminal.js.map +1 -0
  90. package/dist/tui.d.ts +218 -0
  91. package/dist/tui.d.ts.map +1 -0
  92. package/dist/tui.js +966 -0
  93. package/dist/tui.js.map +1 -0
  94. package/dist/undo-stack.d.ts +17 -0
  95. package/dist/undo-stack.d.ts.map +1 -0
  96. package/dist/undo-stack.js +25 -0
  97. package/dist/undo-stack.js.map +1 -0
  98. package/dist/utils.d.ts +78 -0
  99. package/dist/utils.d.ts.map +1 -0
  100. package/dist/utils.js +960 -0
  101. package/dist/utils.js.map +1 -0
  102. package/package.json +55 -0
  103. package/src/autocomplete.ts +771 -0
  104. package/src/components/box.ts +137 -0
  105. package/src/components/cancellable-loader.ts +40 -0
  106. package/src/components/editor.ts +2230 -0
  107. package/src/components/image.ts +104 -0
  108. package/src/components/input.ts +503 -0
  109. package/src/components/loader.ts +55 -0
  110. package/src/components/markdown.ts +820 -0
  111. package/src/components/select-list.ts +229 -0
  112. package/src/components/settings-list.ts +250 -0
  113. package/src/components/spacer.ts +28 -0
  114. package/src/components/text.ts +106 -0
  115. package/src/components/truncated-text.ts +65 -0
  116. package/src/editor-component.ts +74 -0
  117. package/src/fuzzy.ts +133 -0
  118. package/src/index.ts +104 -0
  119. package/src/keybindings.ts +244 -0
  120. package/src/keys.ts +1356 -0
  121. package/src/kill-ring.ts +46 -0
  122. package/src/stdin-buffer.ts +386 -0
  123. package/src/terminal-image.ts +381 -0
  124. package/src/terminal.ts +360 -0
  125. package/src/tui.ts +1200 -0
  126. package/src/undo-stack.ts +28 -0
  127. package/src/utils.ts +1068 -0
@@ -0,0 +1,46 @@
1
+ /**
2
+ * Ring buffer for Emacs-style kill/yank operations.
3
+ *
4
+ * Tracks killed (deleted) text entries. Consecutive kills can accumulate
5
+ * into a single entry. Supports yank (paste most recent) and yank-pop
6
+ * (cycle through older entries).
7
+ */
8
+ export class KillRing {
9
+ private ring: string[] = [];
10
+
11
+ /**
12
+ * Add text to the kill ring.
13
+ *
14
+ * @param text - The killed text to add
15
+ * @param opts - Push options
16
+ * @param opts.prepend - If accumulating, prepend (backward deletion) or append (forward deletion)
17
+ * @param opts.accumulate - Merge with the most recent entry instead of creating a new one
18
+ */
19
+ push(text: string, opts: { prepend: boolean; accumulate?: boolean }): void {
20
+ if (!text) return;
21
+
22
+ if (opts.accumulate && this.ring.length > 0) {
23
+ const last = this.ring.pop()!;
24
+ this.ring.push(opts.prepend ? text + last : last + text);
25
+ } else {
26
+ this.ring.push(text);
27
+ }
28
+ }
29
+
30
+ /** Get most recent entry without modifying the ring. */
31
+ peek(): string | undefined {
32
+ return this.ring.length > 0 ? this.ring[this.ring.length - 1] : undefined;
33
+ }
34
+
35
+ /** Move last entry to front (for yank-pop cycling). */
36
+ rotate(): void {
37
+ if (this.ring.length > 1) {
38
+ const last = this.ring.pop()!;
39
+ this.ring.unshift(last);
40
+ }
41
+ }
42
+
43
+ get length(): number {
44
+ return this.ring.length;
45
+ }
46
+ }
@@ -0,0 +1,386 @@
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
+
20
+ import { EventEmitter } from "events";
21
+
22
+ const ESC = "\x1b";
23
+ const BRACKETED_PASTE_START = "\x1b[200~";
24
+ const BRACKETED_PASTE_END = "\x1b[201~";
25
+
26
+ /**
27
+ * Check if a string is a complete escape sequence or needs more data
28
+ */
29
+ function isCompleteSequence(data: string): "complete" | "incomplete" | "not-escape" {
30
+ if (!data.startsWith(ESC)) {
31
+ return "not-escape";
32
+ }
33
+
34
+ if (data.length === 1) {
35
+ return "incomplete";
36
+ }
37
+
38
+ const afterEsc = data.slice(1);
39
+
40
+ // CSI sequences: ESC [
41
+ if (afterEsc.startsWith("[")) {
42
+ // Check for old-style mouse sequence: ESC[M + 3 bytes
43
+ if (afterEsc.startsWith("[M")) {
44
+ // Old-style mouse needs ESC[M + 3 bytes = 6 total
45
+ return data.length >= 6 ? "complete" : "incomplete";
46
+ }
47
+ return isCompleteCsiSequence(data);
48
+ }
49
+
50
+ // OSC sequences: ESC ]
51
+ if (afterEsc.startsWith("]")) {
52
+ return isCompleteOscSequence(data);
53
+ }
54
+
55
+ // DCS sequences: ESC P ... ESC \ (includes XTVersion responses)
56
+ if (afterEsc.startsWith("P")) {
57
+ return isCompleteDcsSequence(data);
58
+ }
59
+
60
+ // APC sequences: ESC _ ... ESC \ (includes Kitty graphics responses)
61
+ if (afterEsc.startsWith("_")) {
62
+ return isCompleteApcSequence(data);
63
+ }
64
+
65
+ // SS3 sequences: ESC O
66
+ if (afterEsc.startsWith("O")) {
67
+ // ESC O followed by a single character
68
+ return afterEsc.length >= 2 ? "complete" : "incomplete";
69
+ }
70
+
71
+ // Meta key sequences: ESC followed by a single character
72
+ if (afterEsc.length === 1) {
73
+ return "complete";
74
+ }
75
+
76
+ // Unknown escape sequence - treat as complete
77
+ return "complete";
78
+ }
79
+
80
+ /**
81
+ * Check if CSI sequence is complete
82
+ * CSI sequences: ESC [ ... followed by a final byte (0x40-0x7E)
83
+ */
84
+ function isCompleteCsiSequence(data: string): "complete" | "incomplete" {
85
+ if (!data.startsWith(`${ESC}[`)) {
86
+ return "complete";
87
+ }
88
+
89
+ // Need at least ESC [ and one more character
90
+ if (data.length < 3) {
91
+ return "incomplete";
92
+ }
93
+
94
+ const payload = data.slice(2);
95
+
96
+ // CSI sequences end with a byte in the range 0x40-0x7E (@-~)
97
+ // This includes all letters and several special characters
98
+ const lastChar = payload[payload.length - 1];
99
+ const lastCharCode = lastChar.charCodeAt(0);
100
+
101
+ if (lastCharCode >= 0x40 && lastCharCode <= 0x7e) {
102
+ // Special handling for SGR mouse sequences
103
+ // Format: ESC[<B;X;Ym or ESC[<B;X;YM
104
+ if (payload.startsWith("<")) {
105
+ // Must have format: <digits;digits;digits[Mm]
106
+ const mouseMatch = /^<\d+;\d+;\d+[Mm]$/.test(payload);
107
+ if (mouseMatch) {
108
+ return "complete";
109
+ }
110
+ // If it ends with M or m but doesn't match the pattern, still incomplete
111
+ if (lastChar === "M" || lastChar === "m") {
112
+ // Check if we have the right structure
113
+ const parts = payload.slice(1, -1).split(";");
114
+ if (parts.length === 3 && parts.every((p) => /^\d+$/.test(p))) {
115
+ return "complete";
116
+ }
117
+ }
118
+
119
+ return "incomplete";
120
+ }
121
+
122
+ return "complete";
123
+ }
124
+
125
+ return "incomplete";
126
+ }
127
+
128
+ /**
129
+ * Check if OSC sequence is complete
130
+ * OSC sequences: ESC ] ... ST (where ST is ESC \ or BEL)
131
+ */
132
+ function isCompleteOscSequence(data: string): "complete" | "incomplete" {
133
+ if (!data.startsWith(`${ESC}]`)) {
134
+ return "complete";
135
+ }
136
+
137
+ // OSC sequences end with ST (ESC \) or BEL (\x07)
138
+ if (data.endsWith(`${ESC}\\`) || data.endsWith("\x07")) {
139
+ return "complete";
140
+ }
141
+
142
+ return "incomplete";
143
+ }
144
+
145
+ /**
146
+ * Check if DCS (Device Control String) sequence is complete
147
+ * DCS sequences: ESC P ... ST (where ST is ESC \)
148
+ * Used for XTVersion responses like ESC P >| ... ESC \
149
+ */
150
+ function isCompleteDcsSequence(data: string): "complete" | "incomplete" {
151
+ if (!data.startsWith(`${ESC}P`)) {
152
+ return "complete";
153
+ }
154
+
155
+ // DCS sequences end with ST (ESC \)
156
+ if (data.endsWith(`${ESC}\\`)) {
157
+ return "complete";
158
+ }
159
+
160
+ return "incomplete";
161
+ }
162
+
163
+ /**
164
+ * Check if APC (Application Program Command) sequence is complete
165
+ * APC sequences: ESC _ ... ST (where ST is ESC \)
166
+ * Used for Kitty graphics responses like ESC _ G ... ESC \
167
+ */
168
+ function isCompleteApcSequence(data: string): "complete" | "incomplete" {
169
+ if (!data.startsWith(`${ESC}_`)) {
170
+ return "complete";
171
+ }
172
+
173
+ // APC sequences end with ST (ESC \)
174
+ if (data.endsWith(`${ESC}\\`)) {
175
+ return "complete";
176
+ }
177
+
178
+ return "incomplete";
179
+ }
180
+
181
+ /**
182
+ * Split accumulated buffer into complete sequences
183
+ */
184
+ function extractCompleteSequences(buffer: string): { sequences: string[]; remainder: string } {
185
+ const sequences: string[] = [];
186
+ let pos = 0;
187
+
188
+ while (pos < buffer.length) {
189
+ const remaining = buffer.slice(pos);
190
+
191
+ // Try to extract a sequence starting at this position
192
+ if (remaining.startsWith(ESC)) {
193
+ // Find the end of this escape sequence
194
+ let seqEnd = 1;
195
+ while (seqEnd <= remaining.length) {
196
+ const candidate = remaining.slice(0, seqEnd);
197
+ const status = isCompleteSequence(candidate);
198
+
199
+ if (status === "complete") {
200
+ sequences.push(candidate);
201
+ pos += seqEnd;
202
+ break;
203
+ } else if (status === "incomplete") {
204
+ seqEnd++;
205
+ } else {
206
+ // Should not happen when starting with ESC
207
+ sequences.push(candidate);
208
+ pos += seqEnd;
209
+ break;
210
+ }
211
+ }
212
+
213
+ if (seqEnd > remaining.length) {
214
+ return { sequences, remainder: remaining };
215
+ }
216
+ } else {
217
+ // Not an escape sequence - take a single character
218
+ sequences.push(remaining[0]!);
219
+ pos++;
220
+ }
221
+ }
222
+
223
+ return { sequences, remainder: "" };
224
+ }
225
+
226
+ export type StdinBufferOptions = {
227
+ /**
228
+ * Maximum time to wait for sequence completion (default: 10ms)
229
+ * After this time, the buffer is flushed even if incomplete
230
+ */
231
+ timeout?: number;
232
+ };
233
+
234
+ export type StdinBufferEventMap = {
235
+ data: [string];
236
+ paste: [string];
237
+ };
238
+
239
+ /**
240
+ * Buffers stdin input and emits complete sequences via the 'data' event.
241
+ * Handles partial escape sequences that arrive across multiple chunks.
242
+ */
243
+ export class StdinBuffer extends EventEmitter<StdinBufferEventMap> {
244
+ private buffer: string = "";
245
+ private timeout: ReturnType<typeof setTimeout> | null = null;
246
+ private readonly timeoutMs: number;
247
+ private pasteMode: boolean = false;
248
+ private pasteBuffer: string = "";
249
+
250
+ constructor(options: StdinBufferOptions = {}) {
251
+ super();
252
+ this.timeoutMs = options.timeout ?? 10;
253
+ }
254
+
255
+ public process(data: string | Buffer): void {
256
+ // Clear any pending timeout
257
+ if (this.timeout) {
258
+ clearTimeout(this.timeout);
259
+ this.timeout = null;
260
+ }
261
+
262
+ // Handle high-byte conversion (for compatibility with parseKeypress)
263
+ // If buffer has single byte > 127, convert to ESC + (byte - 128)
264
+ let str: string;
265
+ if (Buffer.isBuffer(data)) {
266
+ if (data.length === 1 && data[0]! > 127) {
267
+ const byte = data[0]! - 128;
268
+ str = `\x1b${String.fromCharCode(byte)}`;
269
+ } else {
270
+ str = data.toString();
271
+ }
272
+ } else {
273
+ str = data;
274
+ }
275
+
276
+ if (str.length === 0 && this.buffer.length === 0) {
277
+ this.emit("data", "");
278
+ return;
279
+ }
280
+
281
+ this.buffer += str;
282
+
283
+ if (this.pasteMode) {
284
+ this.pasteBuffer += this.buffer;
285
+ this.buffer = "";
286
+
287
+ const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END);
288
+ if (endIndex !== -1) {
289
+ const pastedContent = this.pasteBuffer.slice(0, endIndex);
290
+ const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
291
+
292
+ this.pasteMode = false;
293
+ this.pasteBuffer = "";
294
+
295
+ this.emit("paste", pastedContent);
296
+
297
+ if (remaining.length > 0) {
298
+ this.process(remaining);
299
+ }
300
+ }
301
+ return;
302
+ }
303
+
304
+ const startIndex = this.buffer.indexOf(BRACKETED_PASTE_START);
305
+ if (startIndex !== -1) {
306
+ if (startIndex > 0) {
307
+ const beforePaste = this.buffer.slice(0, startIndex);
308
+ const result = extractCompleteSequences(beforePaste);
309
+ for (const sequence of result.sequences) {
310
+ this.emit("data", sequence);
311
+ }
312
+ }
313
+
314
+ this.buffer = this.buffer.slice(startIndex + BRACKETED_PASTE_START.length);
315
+ this.pasteMode = true;
316
+ this.pasteBuffer = this.buffer;
317
+ this.buffer = "";
318
+
319
+ const endIndex = this.pasteBuffer.indexOf(BRACKETED_PASTE_END);
320
+ if (endIndex !== -1) {
321
+ const pastedContent = this.pasteBuffer.slice(0, endIndex);
322
+ const remaining = this.pasteBuffer.slice(endIndex + BRACKETED_PASTE_END.length);
323
+
324
+ this.pasteMode = false;
325
+ this.pasteBuffer = "";
326
+
327
+ this.emit("paste", pastedContent);
328
+
329
+ if (remaining.length > 0) {
330
+ this.process(remaining);
331
+ }
332
+ }
333
+ return;
334
+ }
335
+
336
+ const result = extractCompleteSequences(this.buffer);
337
+ this.buffer = result.remainder;
338
+
339
+ for (const sequence of result.sequences) {
340
+ this.emit("data", sequence);
341
+ }
342
+
343
+ if (this.buffer.length > 0) {
344
+ this.timeout = setTimeout(() => {
345
+ const flushed = this.flush();
346
+
347
+ for (const sequence of flushed) {
348
+ this.emit("data", sequence);
349
+ }
350
+ }, this.timeoutMs);
351
+ }
352
+ }
353
+
354
+ flush(): string[] {
355
+ if (this.timeout) {
356
+ clearTimeout(this.timeout);
357
+ this.timeout = null;
358
+ }
359
+
360
+ if (this.buffer.length === 0) {
361
+ return [];
362
+ }
363
+
364
+ const sequences = [this.buffer];
365
+ this.buffer = "";
366
+ return sequences;
367
+ }
368
+
369
+ clear(): void {
370
+ if (this.timeout) {
371
+ clearTimeout(this.timeout);
372
+ this.timeout = null;
373
+ }
374
+ this.buffer = "";
375
+ this.pasteMode = false;
376
+ this.pasteBuffer = "";
377
+ }
378
+
379
+ getBuffer(): string {
380
+ return this.buffer;
381
+ }
382
+
383
+ destroy(): void {
384
+ this.clear();
385
+ }
386
+ }