@lenylvt/pi-tui 0.62.5

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,360 @@
1
+ import * as fs from "node:fs";
2
+ import { createRequire } from "node:module";
3
+ import * as path from "node:path";
4
+ import { setKittyProtocolActive } from "./keys.js";
5
+ import { StdinBuffer } from "./stdin-buffer.js";
6
+
7
+ const cjsRequire = createRequire(import.meta.url);
8
+
9
+ /**
10
+ * Minimal terminal interface for TUI
11
+ */
12
+ export interface Terminal {
13
+ // Start the terminal with input and resize handlers
14
+ start(onInput: (data: string) => void, onResize: () => void): void;
15
+
16
+ // Stop the terminal and restore state
17
+ stop(): void;
18
+
19
+ /**
20
+ * Drain stdin before exiting to prevent Kitty key release events from
21
+ * leaking to the parent shell over slow SSH connections.
22
+ * @param maxMs - Maximum time to drain (default: 1000ms)
23
+ * @param idleMs - Exit early if no input arrives within this time (default: 50ms)
24
+ */
25
+ drainInput(maxMs?: number, idleMs?: number): Promise<void>;
26
+
27
+ // Write output to terminal
28
+ write(data: string): void;
29
+
30
+ // Get terminal dimensions
31
+ get columns(): number;
32
+ get rows(): number;
33
+
34
+ // Whether Kitty keyboard protocol is active
35
+ get kittyProtocolActive(): boolean;
36
+
37
+ // Cursor positioning (relative to current position)
38
+ moveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines
39
+
40
+ // Cursor visibility
41
+ hideCursor(): void; // Hide the cursor
42
+ showCursor(): void; // Show the cursor
43
+
44
+ // Clear operations
45
+ clearLine(): void; // Clear current line
46
+ clearFromCursor(): void; // Clear from cursor to end of screen
47
+ clearScreen(): void; // Clear entire screen and move cursor to (0,0)
48
+
49
+ // Title operations
50
+ setTitle(title: string): void; // Set terminal window title
51
+ }
52
+
53
+ /**
54
+ * Real terminal using process.stdin/stdout
55
+ */
56
+ export class ProcessTerminal implements Terminal {
57
+ private wasRaw = false;
58
+ private inputHandler?: (data: string) => void;
59
+ private resizeHandler?: () => void;
60
+ private _kittyProtocolActive = false;
61
+ private _modifyOtherKeysActive = false;
62
+ private stdinBuffer?: StdinBuffer;
63
+ private stdinDataHandler?: (data: string) => void;
64
+ private writeLogPath = (() => {
65
+ const env = process.env.PI_TUI_WRITE_LOG || "";
66
+ if (!env) return "";
67
+ try {
68
+ if (fs.statSync(env).isDirectory()) {
69
+ const now = new Date();
70
+ const ts = `${now.getFullYear()}-${String(now.getMonth() + 1).padStart(2, "0")}-${String(now.getDate()).padStart(2, "0")}_${String(now.getHours()).padStart(2, "0")}-${String(now.getMinutes()).padStart(2, "0")}-${String(now.getSeconds()).padStart(2, "0")}`;
71
+ return path.join(env, `tui-${ts}-${process.pid}.log`);
72
+ }
73
+ } catch {
74
+ // Not an existing directory - use as-is (file path)
75
+ }
76
+ return env;
77
+ })();
78
+
79
+ get kittyProtocolActive(): boolean {
80
+ return this._kittyProtocolActive;
81
+ }
82
+
83
+ start(onInput: (data: string) => void, onResize: () => void): void {
84
+ this.inputHandler = onInput;
85
+ this.resizeHandler = onResize;
86
+
87
+ // Save previous state and enable raw mode
88
+ this.wasRaw = process.stdin.isRaw || false;
89
+ if (process.stdin.setRawMode) {
90
+ process.stdin.setRawMode(true);
91
+ }
92
+ process.stdin.setEncoding("utf8");
93
+ process.stdin.resume();
94
+
95
+ // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
96
+ process.stdout.write("\x1b[?2004h");
97
+
98
+ // Set up resize handler immediately
99
+ process.stdout.on("resize", this.resizeHandler);
100
+
101
+ // Refresh terminal dimensions - they may be stale after suspend/resume
102
+ // (SIGWINCH is lost while process is stopped). Unix only.
103
+ if (process.platform !== "win32") {
104
+ process.kill(process.pid, "SIGWINCH");
105
+ }
106
+
107
+ // On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends
108
+ // VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console
109
+ // events that lose modifier information. Must run AFTER setRawMode(true)
110
+ // since that resets console mode flags.
111
+ this.enableWindowsVTInput();
112
+
113
+ // Query and enable Kitty keyboard protocol
114
+ // The query handler intercepts input temporarily, then installs the user's handler
115
+ // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
116
+ this.queryAndEnableKittyProtocol();
117
+ }
118
+
119
+ /**
120
+ * Set up StdinBuffer to split batched input into individual sequences.
121
+ * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
122
+ *
123
+ * Also watches for Kitty protocol response and enables it when detected.
124
+ * This is done here (after stdinBuffer parsing) rather than on raw stdin
125
+ * to handle the case where the response arrives split across multiple events.
126
+ */
127
+ private setupStdinBuffer(): void {
128
+ this.stdinBuffer = new StdinBuffer({ timeout: 10 });
129
+
130
+ // Kitty protocol response pattern: \x1b[?<flags>u
131
+ const kittyResponsePattern = /^\x1b\[\?(\d+)u$/;
132
+
133
+ // Forward individual sequences to the input handler
134
+ this.stdinBuffer.on("data", (sequence) => {
135
+ // Check for Kitty protocol response (only if not already enabled)
136
+ if (!this._kittyProtocolActive) {
137
+ const match = sequence.match(kittyResponsePattern);
138
+ if (match) {
139
+ this._kittyProtocolActive = true;
140
+ setKittyProtocolActive(true);
141
+
142
+ // Enable Kitty keyboard protocol (push flags)
143
+ // Flag 1 = disambiguate escape codes
144
+ // Flag 2 = report event types (press/repeat/release)
145
+ // Flag 4 = report alternate keys (shifted key, base layout key)
146
+ // Base layout key enables shortcuts to work with non-Latin keyboard layouts
147
+ process.stdout.write("\x1b[>7u");
148
+ return; // Don't forward protocol response to TUI
149
+ }
150
+ }
151
+
152
+ if (this.inputHandler) {
153
+ this.inputHandler(sequence);
154
+ }
155
+ });
156
+
157
+ // Re-wrap paste content with bracketed paste markers for existing editor handling
158
+ this.stdinBuffer.on("paste", (content) => {
159
+ if (this.inputHandler) {
160
+ this.inputHandler(`\x1b[200~${content}\x1b[201~`);
161
+ }
162
+ });
163
+
164
+ // Handler that pipes stdin data through the buffer
165
+ this.stdinDataHandler = (data: string) => {
166
+ this.stdinBuffer!.process(data);
167
+ };
168
+ }
169
+
170
+ /**
171
+ * Query terminal for Kitty keyboard protocol support and enable if available.
172
+ *
173
+ * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,
174
+ * it supports the protocol and we enable it with CSI > 1 u.
175
+ *
176
+ * If no Kitty response arrives shortly after startup, fall back to enabling
177
+ * xterm modifyOtherKeys mode 2. This is needed for tmux, which can forward
178
+ * modified enter keys as CSI-u when extended-keys is enabled, but may not
179
+ * answer the Kitty protocol query.
180
+ *
181
+ * The response is detected in setupStdinBuffer's data handler, which properly
182
+ * handles the case where the response arrives split across multiple stdin events.
183
+ */
184
+ private queryAndEnableKittyProtocol(): void {
185
+ this.setupStdinBuffer();
186
+ process.stdin.on("data", this.stdinDataHandler!);
187
+ process.stdout.write("\x1b[?u");
188
+ setTimeout(() => {
189
+ if (!this._kittyProtocolActive && !this._modifyOtherKeysActive) {
190
+ process.stdout.write("\x1b[>4;2m");
191
+ this._modifyOtherKeysActive = true;
192
+ }
193
+ }, 150);
194
+ }
195
+
196
+ /**
197
+ * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin
198
+ * console handle so the terminal sends VT sequences for modified keys
199
+ * (e.g. \x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW
200
+ * discards modifier state and Shift+Tab arrives as plain \t.
201
+ */
202
+ private enableWindowsVTInput(): void {
203
+ if (process.platform !== "win32") return;
204
+ try {
205
+ // Dynamic require to avoid bundling koffi's 74MB of cross-platform
206
+ // native binaries into every compiled binary. Koffi is only needed
207
+ // on Windows for VT input support.
208
+ const koffi = cjsRequire("koffi");
209
+ const k32 = koffi.load("kernel32.dll");
210
+ const GetStdHandle = k32.func("void* __stdcall GetStdHandle(int)");
211
+ const GetConsoleMode = k32.func("bool __stdcall GetConsoleMode(void*, _Out_ uint32_t*)");
212
+ const SetConsoleMode = k32.func("bool __stdcall SetConsoleMode(void*, uint32_t)");
213
+
214
+ const STD_INPUT_HANDLE = -10;
215
+ const ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
216
+ const handle = GetStdHandle(STD_INPUT_HANDLE);
217
+ const mode = new Uint32Array(1);
218
+ GetConsoleMode(handle, mode);
219
+ SetConsoleMode(handle, mode[0]! | ENABLE_VIRTUAL_TERMINAL_INPUT);
220
+ } catch {
221
+ // koffi not available — Shift+Tab won't be distinguishable from Tab
222
+ }
223
+ }
224
+
225
+ async drainInput(maxMs = 1000, idleMs = 50): Promise<void> {
226
+ if (this._kittyProtocolActive) {
227
+ // Disable Kitty keyboard protocol first so any late key releases
228
+ // do not generate new Kitty escape sequences.
229
+ process.stdout.write("\x1b[<u");
230
+ this._kittyProtocolActive = false;
231
+ setKittyProtocolActive(false);
232
+ }
233
+ if (this._modifyOtherKeysActive) {
234
+ process.stdout.write("\x1b[>4;0m");
235
+ this._modifyOtherKeysActive = false;
236
+ }
237
+
238
+ const previousHandler = this.inputHandler;
239
+ this.inputHandler = undefined;
240
+
241
+ let lastDataTime = Date.now();
242
+ const onData = () => {
243
+ lastDataTime = Date.now();
244
+ };
245
+
246
+ process.stdin.on("data", onData);
247
+ const endTime = Date.now() + maxMs;
248
+
249
+ try {
250
+ while (true) {
251
+ const now = Date.now();
252
+ const timeLeft = endTime - now;
253
+ if (timeLeft <= 0) break;
254
+ if (now - lastDataTime >= idleMs) break;
255
+ await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
256
+ }
257
+ } finally {
258
+ process.stdin.removeListener("data", onData);
259
+ this.inputHandler = previousHandler;
260
+ }
261
+ }
262
+
263
+ stop(): void {
264
+ // Disable bracketed paste mode
265
+ process.stdout.write("\x1b[?2004l");
266
+
267
+ // Disable Kitty keyboard protocol if not already done by drainInput()
268
+ if (this._kittyProtocolActive) {
269
+ process.stdout.write("\x1b[<u");
270
+ this._kittyProtocolActive = false;
271
+ setKittyProtocolActive(false);
272
+ }
273
+ if (this._modifyOtherKeysActive) {
274
+ process.stdout.write("\x1b[>4;0m");
275
+ this._modifyOtherKeysActive = false;
276
+ }
277
+
278
+ // Clean up StdinBuffer
279
+ if (this.stdinBuffer) {
280
+ this.stdinBuffer.destroy();
281
+ this.stdinBuffer = undefined;
282
+ }
283
+
284
+ // Remove event handlers
285
+ if (this.stdinDataHandler) {
286
+ process.stdin.removeListener("data", this.stdinDataHandler);
287
+ this.stdinDataHandler = undefined;
288
+ }
289
+ this.inputHandler = undefined;
290
+ if (this.resizeHandler) {
291
+ process.stdout.removeListener("resize", this.resizeHandler);
292
+ this.resizeHandler = undefined;
293
+ }
294
+
295
+ // Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
296
+ // re-interpreted after raw mode is disabled. This fixes a race condition
297
+ // where Ctrl+D could close the parent shell over SSH.
298
+ process.stdin.pause();
299
+
300
+ // Restore raw mode state
301
+ if (process.stdin.setRawMode) {
302
+ process.stdin.setRawMode(this.wasRaw);
303
+ }
304
+ }
305
+
306
+ write(data: string): void {
307
+ process.stdout.write(data);
308
+ if (this.writeLogPath) {
309
+ try {
310
+ fs.appendFileSync(this.writeLogPath, data, { encoding: "utf8" });
311
+ } catch {
312
+ // Ignore logging errors
313
+ }
314
+ }
315
+ }
316
+
317
+ get columns(): number {
318
+ return process.stdout.columns || 80;
319
+ }
320
+
321
+ get rows(): number {
322
+ return process.stdout.rows || 24;
323
+ }
324
+
325
+ moveBy(lines: number): void {
326
+ if (lines > 0) {
327
+ // Move down
328
+ process.stdout.write(`\x1b[${lines}B`);
329
+ } else if (lines < 0) {
330
+ // Move up
331
+ process.stdout.write(`\x1b[${-lines}A`);
332
+ }
333
+ // lines === 0: no movement
334
+ }
335
+
336
+ hideCursor(): void {
337
+ process.stdout.write("\x1b[?25l");
338
+ }
339
+
340
+ showCursor(): void {
341
+ process.stdout.write("\x1b[?25h");
342
+ }
343
+
344
+ clearLine(): void {
345
+ process.stdout.write("\x1b[K");
346
+ }
347
+
348
+ clearFromCursor(): void {
349
+ process.stdout.write("\x1b[J");
350
+ }
351
+
352
+ clearScreen(): void {
353
+ process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
354
+ }
355
+
356
+ setTitle(title: string): void {
357
+ // OSC 0;title BEL - set terminal window title
358
+ process.stdout.write(`\x1b]0;${title}\x07`);
359
+ }
360
+ }