@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,285 @@
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
+ const cjsRequire = createRequire(import.meta.url);
7
+ /**
8
+ * Real terminal using process.stdin/stdout
9
+ */
10
+ export class ProcessTerminal {
11
+ wasRaw = false;
12
+ inputHandler;
13
+ resizeHandler;
14
+ _kittyProtocolActive = false;
15
+ _modifyOtherKeysActive = false;
16
+ stdinBuffer;
17
+ stdinDataHandler;
18
+ writeLogPath = (() => {
19
+ const env = process.env.PI_TUI_WRITE_LOG || "";
20
+ if (!env)
21
+ return "";
22
+ try {
23
+ if (fs.statSync(env).isDirectory()) {
24
+ const now = new Date();
25
+ 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")}`;
26
+ return path.join(env, `tui-${ts}-${process.pid}.log`);
27
+ }
28
+ }
29
+ catch {
30
+ // Not an existing directory - use as-is (file path)
31
+ }
32
+ return env;
33
+ })();
34
+ get kittyProtocolActive() {
35
+ return this._kittyProtocolActive;
36
+ }
37
+ start(onInput, onResize) {
38
+ this.inputHandler = onInput;
39
+ this.resizeHandler = onResize;
40
+ // Save previous state and enable raw mode
41
+ this.wasRaw = process.stdin.isRaw || false;
42
+ if (process.stdin.setRawMode) {
43
+ process.stdin.setRawMode(true);
44
+ }
45
+ process.stdin.setEncoding("utf8");
46
+ process.stdin.resume();
47
+ // Enable bracketed paste mode - terminal will wrap pastes in \x1b[200~ ... \x1b[201~
48
+ process.stdout.write("\x1b[?2004h");
49
+ // Set up resize handler immediately
50
+ process.stdout.on("resize", this.resizeHandler);
51
+ // Refresh terminal dimensions - they may be stale after suspend/resume
52
+ // (SIGWINCH is lost while process is stopped). Unix only.
53
+ if (process.platform !== "win32") {
54
+ process.kill(process.pid, "SIGWINCH");
55
+ }
56
+ // On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends
57
+ // VT escape sequences (e.g. \x1b[Z for Shift+Tab) instead of raw console
58
+ // events that lose modifier information. Must run AFTER setRawMode(true)
59
+ // since that resets console mode flags.
60
+ this.enableWindowsVTInput();
61
+ // Query and enable Kitty keyboard protocol
62
+ // The query handler intercepts input temporarily, then installs the user's handler
63
+ // See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/
64
+ this.queryAndEnableKittyProtocol();
65
+ }
66
+ /**
67
+ * Set up StdinBuffer to split batched input into individual sequences.
68
+ * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.
69
+ *
70
+ * Also watches for Kitty protocol response and enables it when detected.
71
+ * This is done here (after stdinBuffer parsing) rather than on raw stdin
72
+ * to handle the case where the response arrives split across multiple events.
73
+ */
74
+ setupStdinBuffer() {
75
+ this.stdinBuffer = new StdinBuffer({ timeout: 10 });
76
+ // Kitty protocol response pattern: \x1b[?<flags>u
77
+ const kittyResponsePattern = /^\x1b\[\?(\d+)u$/;
78
+ // Forward individual sequences to the input handler
79
+ this.stdinBuffer.on("data", (sequence) => {
80
+ // Check for Kitty protocol response (only if not already enabled)
81
+ if (!this._kittyProtocolActive) {
82
+ const match = sequence.match(kittyResponsePattern);
83
+ if (match) {
84
+ this._kittyProtocolActive = true;
85
+ setKittyProtocolActive(true);
86
+ // Enable Kitty keyboard protocol (push flags)
87
+ // Flag 1 = disambiguate escape codes
88
+ // Flag 2 = report event types (press/repeat/release)
89
+ // Flag 4 = report alternate keys (shifted key, base layout key)
90
+ // Base layout key enables shortcuts to work with non-Latin keyboard layouts
91
+ process.stdout.write("\x1b[>7u");
92
+ return; // Don't forward protocol response to TUI
93
+ }
94
+ }
95
+ if (this.inputHandler) {
96
+ this.inputHandler(sequence);
97
+ }
98
+ });
99
+ // Re-wrap paste content with bracketed paste markers for existing editor handling
100
+ this.stdinBuffer.on("paste", (content) => {
101
+ if (this.inputHandler) {
102
+ this.inputHandler(`\x1b[200~${content}\x1b[201~`);
103
+ }
104
+ });
105
+ // Handler that pipes stdin data through the buffer
106
+ this.stdinDataHandler = (data) => {
107
+ this.stdinBuffer.process(data);
108
+ };
109
+ }
110
+ /**
111
+ * Query terminal for Kitty keyboard protocol support and enable if available.
112
+ *
113
+ * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,
114
+ * it supports the protocol and we enable it with CSI > 1 u.
115
+ *
116
+ * If no Kitty response arrives shortly after startup, fall back to enabling
117
+ * xterm modifyOtherKeys mode 2. This is needed for tmux, which can forward
118
+ * modified enter keys as CSI-u when extended-keys is enabled, but may not
119
+ * answer the Kitty protocol query.
120
+ *
121
+ * The response is detected in setupStdinBuffer's data handler, which properly
122
+ * handles the case where the response arrives split across multiple stdin events.
123
+ */
124
+ queryAndEnableKittyProtocol() {
125
+ this.setupStdinBuffer();
126
+ process.stdin.on("data", this.stdinDataHandler);
127
+ process.stdout.write("\x1b[?u");
128
+ setTimeout(() => {
129
+ if (!this._kittyProtocolActive && !this._modifyOtherKeysActive) {
130
+ process.stdout.write("\x1b[>4;2m");
131
+ this._modifyOtherKeysActive = true;
132
+ }
133
+ }, 150);
134
+ }
135
+ /**
136
+ * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin
137
+ * console handle so the terminal sends VT sequences for modified keys
138
+ * (e.g. \x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW
139
+ * discards modifier state and Shift+Tab arrives as plain \t.
140
+ */
141
+ enableWindowsVTInput() {
142
+ if (process.platform !== "win32")
143
+ return;
144
+ try {
145
+ // Dynamic require to avoid bundling koffi's 74MB of cross-platform
146
+ // native binaries into every compiled binary. Koffi is only needed
147
+ // on Windows for VT input support.
148
+ const koffi = cjsRequire("koffi");
149
+ const k32 = koffi.load("kernel32.dll");
150
+ const GetStdHandle = k32.func("void* __stdcall GetStdHandle(int)");
151
+ const GetConsoleMode = k32.func("bool __stdcall GetConsoleMode(void*, _Out_ uint32_t*)");
152
+ const SetConsoleMode = k32.func("bool __stdcall SetConsoleMode(void*, uint32_t)");
153
+ const STD_INPUT_HANDLE = -10;
154
+ const ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;
155
+ const handle = GetStdHandle(STD_INPUT_HANDLE);
156
+ const mode = new Uint32Array(1);
157
+ GetConsoleMode(handle, mode);
158
+ SetConsoleMode(handle, mode[0] | ENABLE_VIRTUAL_TERMINAL_INPUT);
159
+ }
160
+ catch {
161
+ // koffi not available — Shift+Tab won't be distinguishable from Tab
162
+ }
163
+ }
164
+ async drainInput(maxMs = 1000, idleMs = 50) {
165
+ if (this._kittyProtocolActive) {
166
+ // Disable Kitty keyboard protocol first so any late key releases
167
+ // do not generate new Kitty escape sequences.
168
+ process.stdout.write("\x1b[<u");
169
+ this._kittyProtocolActive = false;
170
+ setKittyProtocolActive(false);
171
+ }
172
+ if (this._modifyOtherKeysActive) {
173
+ process.stdout.write("\x1b[>4;0m");
174
+ this._modifyOtherKeysActive = false;
175
+ }
176
+ const previousHandler = this.inputHandler;
177
+ this.inputHandler = undefined;
178
+ let lastDataTime = Date.now();
179
+ const onData = () => {
180
+ lastDataTime = Date.now();
181
+ };
182
+ process.stdin.on("data", onData);
183
+ const endTime = Date.now() + maxMs;
184
+ try {
185
+ while (true) {
186
+ const now = Date.now();
187
+ const timeLeft = endTime - now;
188
+ if (timeLeft <= 0)
189
+ break;
190
+ if (now - lastDataTime >= idleMs)
191
+ break;
192
+ await new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));
193
+ }
194
+ }
195
+ finally {
196
+ process.stdin.removeListener("data", onData);
197
+ this.inputHandler = previousHandler;
198
+ }
199
+ }
200
+ stop() {
201
+ // Disable bracketed paste mode
202
+ process.stdout.write("\x1b[?2004l");
203
+ // Disable Kitty keyboard protocol if not already done by drainInput()
204
+ if (this._kittyProtocolActive) {
205
+ process.stdout.write("\x1b[<u");
206
+ this._kittyProtocolActive = false;
207
+ setKittyProtocolActive(false);
208
+ }
209
+ if (this._modifyOtherKeysActive) {
210
+ process.stdout.write("\x1b[>4;0m");
211
+ this._modifyOtherKeysActive = false;
212
+ }
213
+ // Clean up StdinBuffer
214
+ if (this.stdinBuffer) {
215
+ this.stdinBuffer.destroy();
216
+ this.stdinBuffer = undefined;
217
+ }
218
+ // Remove event handlers
219
+ if (this.stdinDataHandler) {
220
+ process.stdin.removeListener("data", this.stdinDataHandler);
221
+ this.stdinDataHandler = undefined;
222
+ }
223
+ this.inputHandler = undefined;
224
+ if (this.resizeHandler) {
225
+ process.stdout.removeListener("resize", this.resizeHandler);
226
+ this.resizeHandler = undefined;
227
+ }
228
+ // Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being
229
+ // re-interpreted after raw mode is disabled. This fixes a race condition
230
+ // where Ctrl+D could close the parent shell over SSH.
231
+ process.stdin.pause();
232
+ // Restore raw mode state
233
+ if (process.stdin.setRawMode) {
234
+ process.stdin.setRawMode(this.wasRaw);
235
+ }
236
+ }
237
+ write(data) {
238
+ process.stdout.write(data);
239
+ if (this.writeLogPath) {
240
+ try {
241
+ fs.appendFileSync(this.writeLogPath, data, { encoding: "utf8" });
242
+ }
243
+ catch {
244
+ // Ignore logging errors
245
+ }
246
+ }
247
+ }
248
+ get columns() {
249
+ return process.stdout.columns || 80;
250
+ }
251
+ get rows() {
252
+ return process.stdout.rows || 24;
253
+ }
254
+ moveBy(lines) {
255
+ if (lines > 0) {
256
+ // Move down
257
+ process.stdout.write(`\x1b[${lines}B`);
258
+ }
259
+ else if (lines < 0) {
260
+ // Move up
261
+ process.stdout.write(`\x1b[${-lines}A`);
262
+ }
263
+ // lines === 0: no movement
264
+ }
265
+ hideCursor() {
266
+ process.stdout.write("\x1b[?25l");
267
+ }
268
+ showCursor() {
269
+ process.stdout.write("\x1b[?25h");
270
+ }
271
+ clearLine() {
272
+ process.stdout.write("\x1b[K");
273
+ }
274
+ clearFromCursor() {
275
+ process.stdout.write("\x1b[J");
276
+ }
277
+ clearScreen() {
278
+ process.stdout.write("\x1b[2J\x1b[H"); // Clear screen and move to home (1,1)
279
+ }
280
+ setTitle(title) {
281
+ // OSC 0;title BEL - set terminal window title
282
+ process.stdout.write(`\x1b]0;${title}\x07`);
283
+ }
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AI,EAAE,CAAC;IAAA,CACpC;IAED,IAAI,IAAI,GAAW;QAClB,OAAO,OAAO,CAAC,MAAM,CAAC,IAAI,IAAI,EAAE,CAAC;IAAA,CACjC;IAED,MAAM,CAAC,KAAa,EAAQ;QAC3B,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACf,YAAY;YACZ,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,KAAK,GAAG,CAAC,CAAC;QACxC,CAAC;aAAM,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;YACtB,UAAU;YACV,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,KAAK,GAAG,CAAC,CAAC;QACzC,CAAC;QACD,2BAA2B;IAD1B,CAED;IAED,UAAU,GAAS;QAClB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAAA,CAClC;IAED,UAAU,GAAS;QAClB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,WAAW,CAAC,CAAC;IAAA,CAClC;IAED,SAAS,GAAS;QACjB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAAA,CAC/B;IAED,eAAe,GAAS;QACvB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,QAAQ,CAAC,CAAC;IAAA,CAC/B;IAED,WAAW,GAAS;QACnB,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,eAAe,CAAC,CAAC,CAAC,sCAAsC;IAAvC,CACtC;IAED,QAAQ,CAAC,KAAa,EAAQ;QAC7B,8CAA8C;QAC9C,OAAO,CAAC,MAAM,CAAC,KAAK,CAAC,UAAU,KAAK,MAAM,CAAC,CAAC;IAAA,CAC5C;CACD","sourcesContent":["import * as fs from \"node:fs\";\nimport { createRequire } from \"node:module\";\nimport * as path from \"node:path\";\nimport { setKittyProtocolActive } from \"./keys.js\";\nimport { StdinBuffer } from \"./stdin-buffer.js\";\n\nconst cjsRequire = createRequire(import.meta.url);\n\n/**\n * Minimal terminal interface for TUI\n */\nexport interface Terminal {\n\t// Start the terminal with input and resize handlers\n\tstart(onInput: (data: string) => void, onResize: () => void): void;\n\n\t// Stop the terminal and restore state\n\tstop(): void;\n\n\t/**\n\t * Drain stdin before exiting to prevent Kitty key release events from\n\t * leaking to the parent shell over slow SSH connections.\n\t * @param maxMs - Maximum time to drain (default: 1000ms)\n\t * @param idleMs - Exit early if no input arrives within this time (default: 50ms)\n\t */\n\tdrainInput(maxMs?: number, idleMs?: number): Promise<void>;\n\n\t// Write output to terminal\n\twrite(data: string): void;\n\n\t// Get terminal dimensions\n\tget columns(): number;\n\tget rows(): number;\n\n\t// Whether Kitty keyboard protocol is active\n\tget kittyProtocolActive(): boolean;\n\n\t// Cursor positioning (relative to current position)\n\tmoveBy(lines: number): void; // Move cursor up (negative) or down (positive) by N lines\n\n\t// Cursor visibility\n\thideCursor(): void; // Hide the cursor\n\tshowCursor(): void; // Show the cursor\n\n\t// Clear operations\n\tclearLine(): void; // Clear current line\n\tclearFromCursor(): void; // Clear from cursor to end of screen\n\tclearScreen(): void; // Clear entire screen and move cursor to (0,0)\n\n\t// Title operations\n\tsetTitle(title: string): void; // Set terminal window title\n}\n\n/**\n * Real terminal using process.stdin/stdout\n */\nexport class ProcessTerminal implements Terminal {\n\tprivate wasRaw = false;\n\tprivate inputHandler?: (data: string) => void;\n\tprivate resizeHandler?: () => void;\n\tprivate _kittyProtocolActive = false;\n\tprivate _modifyOtherKeysActive = false;\n\tprivate stdinBuffer?: StdinBuffer;\n\tprivate stdinDataHandler?: (data: string) => void;\n\tprivate writeLogPath = (() => {\n\t\tconst env = process.env.PI_TUI_WRITE_LOG || \"\";\n\t\tif (!env) return \"\";\n\t\ttry {\n\t\t\tif (fs.statSync(env).isDirectory()) {\n\t\t\t\tconst now = new Date();\n\t\t\t\tconst 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\")}`;\n\t\t\t\treturn path.join(env, `tui-${ts}-${process.pid}.log`);\n\t\t\t}\n\t\t} catch {\n\t\t\t// Not an existing directory - use as-is (file path)\n\t\t}\n\t\treturn env;\n\t})();\n\n\tget kittyProtocolActive(): boolean {\n\t\treturn this._kittyProtocolActive;\n\t}\n\n\tstart(onInput: (data: string) => void, onResize: () => void): void {\n\t\tthis.inputHandler = onInput;\n\t\tthis.resizeHandler = onResize;\n\n\t\t// Save previous state and enable raw mode\n\t\tthis.wasRaw = process.stdin.isRaw || false;\n\t\tif (process.stdin.setRawMode) {\n\t\t\tprocess.stdin.setRawMode(true);\n\t\t}\n\t\tprocess.stdin.setEncoding(\"utf8\");\n\t\tprocess.stdin.resume();\n\n\t\t// Enable bracketed paste mode - terminal will wrap pastes in \\x1b[200~ ... \\x1b[201~\n\t\tprocess.stdout.write(\"\\x1b[?2004h\");\n\n\t\t// Set up resize handler immediately\n\t\tprocess.stdout.on(\"resize\", this.resizeHandler);\n\n\t\t// Refresh terminal dimensions - they may be stale after suspend/resume\n\t\t// (SIGWINCH is lost while process is stopped). Unix only.\n\t\tif (process.platform !== \"win32\") {\n\t\t\tprocess.kill(process.pid, \"SIGWINCH\");\n\t\t}\n\n\t\t// On Windows, enable ENABLE_VIRTUAL_TERMINAL_INPUT so the console sends\n\t\t// VT escape sequences (e.g. \\x1b[Z for Shift+Tab) instead of raw console\n\t\t// events that lose modifier information. Must run AFTER setRawMode(true)\n\t\t// since that resets console mode flags.\n\t\tthis.enableWindowsVTInput();\n\n\t\t// Query and enable Kitty keyboard protocol\n\t\t// The query handler intercepts input temporarily, then installs the user's handler\n\t\t// See: https://sw.kovidgoyal.net/kitty/keyboard-protocol/\n\t\tthis.queryAndEnableKittyProtocol();\n\t}\n\n\t/**\n\t * Set up StdinBuffer to split batched input into individual sequences.\n\t * This ensures components receive single events, making matchesKey/isKeyRelease work correctly.\n\t *\n\t * Also watches for Kitty protocol response and enables it when detected.\n\t * This is done here (after stdinBuffer parsing) rather than on raw stdin\n\t * to handle the case where the response arrives split across multiple events.\n\t */\n\tprivate setupStdinBuffer(): void {\n\t\tthis.stdinBuffer = new StdinBuffer({ timeout: 10 });\n\n\t\t// Kitty protocol response pattern: \\x1b[?<flags>u\n\t\tconst kittyResponsePattern = /^\\x1b\\[\\?(\\d+)u$/;\n\n\t\t// Forward individual sequences to the input handler\n\t\tthis.stdinBuffer.on(\"data\", (sequence) => {\n\t\t\t// Check for Kitty protocol response (only if not already enabled)\n\t\t\tif (!this._kittyProtocolActive) {\n\t\t\t\tconst match = sequence.match(kittyResponsePattern);\n\t\t\t\tif (match) {\n\t\t\t\t\tthis._kittyProtocolActive = true;\n\t\t\t\t\tsetKittyProtocolActive(true);\n\n\t\t\t\t\t// Enable Kitty keyboard protocol (push flags)\n\t\t\t\t\t// Flag 1 = disambiguate escape codes\n\t\t\t\t\t// Flag 2 = report event types (press/repeat/release)\n\t\t\t\t\t// Flag 4 = report alternate keys (shifted key, base layout key)\n\t\t\t\t\t// Base layout key enables shortcuts to work with non-Latin keyboard layouts\n\t\t\t\t\tprocess.stdout.write(\"\\x1b[>7u\");\n\t\t\t\t\treturn; // Don't forward protocol response to TUI\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (this.inputHandler) {\n\t\t\t\tthis.inputHandler(sequence);\n\t\t\t}\n\t\t});\n\n\t\t// Re-wrap paste content with bracketed paste markers for existing editor handling\n\t\tthis.stdinBuffer.on(\"paste\", (content) => {\n\t\t\tif (this.inputHandler) {\n\t\t\t\tthis.inputHandler(`\\x1b[200~${content}\\x1b[201~`);\n\t\t\t}\n\t\t});\n\n\t\t// Handler that pipes stdin data through the buffer\n\t\tthis.stdinDataHandler = (data: string) => {\n\t\t\tthis.stdinBuffer!.process(data);\n\t\t};\n\t}\n\n\t/**\n\t * Query terminal for Kitty keyboard protocol support and enable if available.\n\t *\n\t * Sends CSI ? u to query current flags. If terminal responds with CSI ? <flags> u,\n\t * it supports the protocol and we enable it with CSI > 1 u.\n\t *\n\t * If no Kitty response arrives shortly after startup, fall back to enabling\n\t * xterm modifyOtherKeys mode 2. This is needed for tmux, which can forward\n\t * modified enter keys as CSI-u when extended-keys is enabled, but may not\n\t * answer the Kitty protocol query.\n\t *\n\t * The response is detected in setupStdinBuffer's data handler, which properly\n\t * handles the case where the response arrives split across multiple stdin events.\n\t */\n\tprivate queryAndEnableKittyProtocol(): void {\n\t\tthis.setupStdinBuffer();\n\t\tprocess.stdin.on(\"data\", this.stdinDataHandler!);\n\t\tprocess.stdout.write(\"\\x1b[?u\");\n\t\tsetTimeout(() => {\n\t\t\tif (!this._kittyProtocolActive && !this._modifyOtherKeysActive) {\n\t\t\t\tprocess.stdout.write(\"\\x1b[>4;2m\");\n\t\t\t\tthis._modifyOtherKeysActive = true;\n\t\t\t}\n\t\t}, 150);\n\t}\n\n\t/**\n\t * On Windows, add ENABLE_VIRTUAL_TERMINAL_INPUT (0x0200) to the stdin\n\t * console handle so the terminal sends VT sequences for modified keys\n\t * (e.g. \\x1b[Z for Shift+Tab). Without this, libuv's ReadConsoleInputW\n\t * discards modifier state and Shift+Tab arrives as plain \\t.\n\t */\n\tprivate enableWindowsVTInput(): void {\n\t\tif (process.platform !== \"win32\") return;\n\t\ttry {\n\t\t\t// Dynamic require to avoid bundling koffi's 74MB of cross-platform\n\t\t\t// native binaries into every compiled binary. Koffi is only needed\n\t\t\t// on Windows for VT input support.\n\t\t\tconst koffi = cjsRequire(\"koffi\");\n\t\t\tconst k32 = koffi.load(\"kernel32.dll\");\n\t\t\tconst GetStdHandle = k32.func(\"void* __stdcall GetStdHandle(int)\");\n\t\t\tconst GetConsoleMode = k32.func(\"bool __stdcall GetConsoleMode(void*, _Out_ uint32_t*)\");\n\t\t\tconst SetConsoleMode = k32.func(\"bool __stdcall SetConsoleMode(void*, uint32_t)\");\n\n\t\t\tconst STD_INPUT_HANDLE = -10;\n\t\t\tconst ENABLE_VIRTUAL_TERMINAL_INPUT = 0x0200;\n\t\t\tconst handle = GetStdHandle(STD_INPUT_HANDLE);\n\t\t\tconst mode = new Uint32Array(1);\n\t\t\tGetConsoleMode(handle, mode);\n\t\t\tSetConsoleMode(handle, mode[0]! | ENABLE_VIRTUAL_TERMINAL_INPUT);\n\t\t} catch {\n\t\t\t// koffi not available — Shift+Tab won't be distinguishable from Tab\n\t\t}\n\t}\n\n\tasync drainInput(maxMs = 1000, idleMs = 50): Promise<void> {\n\t\tif (this._kittyProtocolActive) {\n\t\t\t// Disable Kitty keyboard protocol first so any late key releases\n\t\t\t// do not generate new Kitty escape sequences.\n\t\t\tprocess.stdout.write(\"\\x1b[<u\");\n\t\t\tthis._kittyProtocolActive = false;\n\t\t\tsetKittyProtocolActive(false);\n\t\t}\n\t\tif (this._modifyOtherKeysActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[>4;0m\");\n\t\t\tthis._modifyOtherKeysActive = false;\n\t\t}\n\n\t\tconst previousHandler = this.inputHandler;\n\t\tthis.inputHandler = undefined;\n\n\t\tlet lastDataTime = Date.now();\n\t\tconst onData = () => {\n\t\t\tlastDataTime = Date.now();\n\t\t};\n\n\t\tprocess.stdin.on(\"data\", onData);\n\t\tconst endTime = Date.now() + maxMs;\n\n\t\ttry {\n\t\t\twhile (true) {\n\t\t\t\tconst now = Date.now();\n\t\t\t\tconst timeLeft = endTime - now;\n\t\t\t\tif (timeLeft <= 0) break;\n\t\t\t\tif (now - lastDataTime >= idleMs) break;\n\t\t\t\tawait new Promise((resolve) => setTimeout(resolve, Math.min(idleMs, timeLeft)));\n\t\t\t}\n\t\t} finally {\n\t\t\tprocess.stdin.removeListener(\"data\", onData);\n\t\t\tthis.inputHandler = previousHandler;\n\t\t}\n\t}\n\n\tstop(): void {\n\t\t// Disable bracketed paste mode\n\t\tprocess.stdout.write(\"\\x1b[?2004l\");\n\n\t\t// Disable Kitty keyboard protocol if not already done by drainInput()\n\t\tif (this._kittyProtocolActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[<u\");\n\t\t\tthis._kittyProtocolActive = false;\n\t\t\tsetKittyProtocolActive(false);\n\t\t}\n\t\tif (this._modifyOtherKeysActive) {\n\t\t\tprocess.stdout.write(\"\\x1b[>4;0m\");\n\t\t\tthis._modifyOtherKeysActive = false;\n\t\t}\n\n\t\t// Clean up StdinBuffer\n\t\tif (this.stdinBuffer) {\n\t\t\tthis.stdinBuffer.destroy();\n\t\t\tthis.stdinBuffer = undefined;\n\t\t}\n\n\t\t// Remove event handlers\n\t\tif (this.stdinDataHandler) {\n\t\t\tprocess.stdin.removeListener(\"data\", this.stdinDataHandler);\n\t\t\tthis.stdinDataHandler = undefined;\n\t\t}\n\t\tthis.inputHandler = undefined;\n\t\tif (this.resizeHandler) {\n\t\t\tprocess.stdout.removeListener(\"resize\", this.resizeHandler);\n\t\t\tthis.resizeHandler = undefined;\n\t\t}\n\n\t\t// Pause stdin to prevent any buffered input (e.g., Ctrl+D) from being\n\t\t// re-interpreted after raw mode is disabled. This fixes a race condition\n\t\t// where Ctrl+D could close the parent shell over SSH.\n\t\tprocess.stdin.pause();\n\n\t\t// Restore raw mode state\n\t\tif (process.stdin.setRawMode) {\n\t\t\tprocess.stdin.setRawMode(this.wasRaw);\n\t\t}\n\t}\n\n\twrite(data: string): void {\n\t\tprocess.stdout.write(data);\n\t\tif (this.writeLogPath) {\n\t\t\ttry {\n\t\t\t\tfs.appendFileSync(this.writeLogPath, data, { encoding: \"utf8\" });\n\t\t\t} catch {\n\t\t\t\t// Ignore logging errors\n\t\t\t}\n\t\t}\n\t}\n\n\tget columns(): number {\n\t\treturn process.stdout.columns || 80;\n\t}\n\n\tget rows(): number {\n\t\treturn process.stdout.rows || 24;\n\t}\n\n\tmoveBy(lines: number): void {\n\t\tif (lines > 0) {\n\t\t\t// Move down\n\t\t\tprocess.stdout.write(`\\x1b[${lines}B`);\n\t\t} else if (lines < 0) {\n\t\t\t// Move up\n\t\t\tprocess.stdout.write(`\\x1b[${-lines}A`);\n\t\t}\n\t\t// lines === 0: no movement\n\t}\n\n\thideCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[?25l\");\n\t}\n\n\tshowCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[?25h\");\n\t}\n\n\tclearLine(): void {\n\t\tprocess.stdout.write(\"\\x1b[K\");\n\t}\n\n\tclearFromCursor(): void {\n\t\tprocess.stdout.write(\"\\x1b[J\");\n\t}\n\n\tclearScreen(): void {\n\t\tprocess.stdout.write(\"\\x1b[2J\\x1b[H\"); // Clear screen and move to home (1,1)\n\t}\n\n\tsetTitle(title: string): void {\n\t\t// OSC 0;title BEL - set terminal window title\n\t\tprocess.stdout.write(`\\x1b]0;${title}\\x07`);\n\t}\n}\n"]}
package/dist/tui.d.ts ADDED
@@ -0,0 +1,218 @@
1
+ /**
2
+ * Minimal TUI implementation with differential rendering
3
+ */
4
+ import type { Terminal } from "./terminal.js";
5
+ import { visibleWidth } from "./utils.js";
6
+ /**
7
+ * Component interface - all components must implement this
8
+ */
9
+ export interface Component {
10
+ /**
11
+ * Render the component to lines for the given viewport width
12
+ * @param width - Current viewport width
13
+ * @returns Array of strings, each representing a line
14
+ */
15
+ render(width: number): string[];
16
+ /**
17
+ * Optional handler for keyboard input when component has focus
18
+ */
19
+ handleInput?(data: string): void;
20
+ /**
21
+ * If true, component receives key release events (Kitty protocol).
22
+ * Default is false - release events are filtered out.
23
+ */
24
+ wantsKeyRelease?: boolean;
25
+ /**
26
+ * Invalidate any cached rendering state.
27
+ * Called when theme changes or when component needs to re-render from scratch.
28
+ */
29
+ invalidate(): void;
30
+ }
31
+ type InputListenerResult = {
32
+ consume?: boolean;
33
+ data?: string;
34
+ } | undefined;
35
+ type InputListener = (data: string) => InputListenerResult;
36
+ /**
37
+ * Interface for components that can receive focus and display a hardware cursor.
38
+ * When focused, the component should emit CURSOR_MARKER at the cursor position
39
+ * in its render output. TUI will find this marker and position the hardware
40
+ * cursor there for proper IME candidate window positioning.
41
+ */
42
+ export interface Focusable {
43
+ /** Set by TUI when focus changes. Component should emit CURSOR_MARKER when true. */
44
+ focused: boolean;
45
+ }
46
+ /** Type guard to check if a component implements Focusable */
47
+ export declare function isFocusable(component: Component | null): component is Component & Focusable;
48
+ /**
49
+ * Cursor position marker - APC (Application Program Command) sequence.
50
+ * This is a zero-width escape sequence that terminals ignore.
51
+ * Components emit this at the cursor position when focused.
52
+ * TUI finds and strips this marker, then positions the hardware cursor there.
53
+ */
54
+ export declare const CURSOR_MARKER = "\u001B_pi:c\u0007";
55
+ export { visibleWidth };
56
+ /**
57
+ * Anchor position for overlays
58
+ */
59
+ export type OverlayAnchor = "center" | "top-left" | "top-right" | "bottom-left" | "bottom-right" | "top-center" | "bottom-center" | "left-center" | "right-center";
60
+ /**
61
+ * Margin configuration for overlays
62
+ */
63
+ export interface OverlayMargin {
64
+ top?: number;
65
+ right?: number;
66
+ bottom?: number;
67
+ left?: number;
68
+ }
69
+ /** Value that can be absolute (number) or percentage (string like "50%") */
70
+ export type SizeValue = number | `${number}%`;
71
+ /**
72
+ * Options for overlay positioning and sizing.
73
+ * Values can be absolute numbers or percentage strings (e.g., "50%").
74
+ */
75
+ export interface OverlayOptions {
76
+ /** Width in columns, or percentage of terminal width (e.g., "50%") */
77
+ width?: SizeValue;
78
+ /** Minimum width in columns */
79
+ minWidth?: number;
80
+ /** Maximum height in rows, or percentage of terminal height (e.g., "50%") */
81
+ maxHeight?: SizeValue;
82
+ /** Anchor point for positioning (default: 'center') */
83
+ anchor?: OverlayAnchor;
84
+ /** Horizontal offset from anchor position (positive = right) */
85
+ offsetX?: number;
86
+ /** Vertical offset from anchor position (positive = down) */
87
+ offsetY?: number;
88
+ /** Row position: absolute number, or percentage (e.g., "25%" = 25% from top) */
89
+ row?: SizeValue;
90
+ /** Column position: absolute number, or percentage (e.g., "50%" = centered horizontally) */
91
+ col?: SizeValue;
92
+ /** Margin from terminal edges. Number applies to all sides. */
93
+ margin?: OverlayMargin | number;
94
+ /**
95
+ * Control overlay visibility based on terminal dimensions.
96
+ * If provided, overlay is only rendered when this returns true.
97
+ * Called each render cycle with current terminal dimensions.
98
+ */
99
+ visible?: (termWidth: number, termHeight: number) => boolean;
100
+ /** If true, don't capture keyboard focus when shown */
101
+ nonCapturing?: boolean;
102
+ }
103
+ /**
104
+ * Handle returned by showOverlay for controlling the overlay
105
+ */
106
+ export interface OverlayHandle {
107
+ /** Permanently remove the overlay (cannot be shown again) */
108
+ hide(): void;
109
+ /** Temporarily hide or show the overlay */
110
+ setHidden(hidden: boolean): void;
111
+ /** Check if overlay is temporarily hidden */
112
+ isHidden(): boolean;
113
+ /** Focus this overlay and bring it to the visual front */
114
+ focus(): void;
115
+ /** Release focus to the previous target */
116
+ unfocus(): void;
117
+ /** Check if this overlay currently has focus */
118
+ isFocused(): boolean;
119
+ }
120
+ /**
121
+ * Container - a component that contains other components
122
+ */
123
+ export declare class Container implements Component {
124
+ children: Component[];
125
+ addChild(component: Component): void;
126
+ removeChild(component: Component): void;
127
+ clear(): void;
128
+ invalidate(): void;
129
+ render(width: number): string[];
130
+ }
131
+ /**
132
+ * TUI - Main class for managing terminal UI with differential rendering
133
+ */
134
+ export declare class TUI extends Container {
135
+ terminal: Terminal;
136
+ private previousLines;
137
+ private previousWidth;
138
+ private previousHeight;
139
+ private focusedComponent;
140
+ private inputListeners;
141
+ /** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
142
+ onDebug?: () => void;
143
+ private renderRequested;
144
+ private cursorRow;
145
+ private hardwareCursorRow;
146
+ private showHardwareCursor;
147
+ private clearOnShrink;
148
+ private maxLinesRendered;
149
+ private previousViewportTop;
150
+ private fullRedrawCount;
151
+ private stopped;
152
+ private focusOrderCounter;
153
+ private overlayStack;
154
+ constructor(terminal: Terminal, showHardwareCursor?: boolean);
155
+ get fullRedraws(): number;
156
+ getShowHardwareCursor(): boolean;
157
+ setShowHardwareCursor(enabled: boolean): void;
158
+ getClearOnShrink(): boolean;
159
+ /**
160
+ * Set whether to trigger full re-render when content shrinks.
161
+ * When true (default), empty rows are cleared when content shrinks.
162
+ * When false, empty rows remain (reduces redraws on slower terminals).
163
+ */
164
+ setClearOnShrink(enabled: boolean): void;
165
+ setFocus(component: Component | null): void;
166
+ /**
167
+ * Show an overlay component with configurable positioning and sizing.
168
+ * Returns a handle to control the overlay's visibility.
169
+ */
170
+ showOverlay(component: Component, options?: OverlayOptions): OverlayHandle;
171
+ /** Hide the topmost overlay and restore previous focus. */
172
+ hideOverlay(): void;
173
+ /** Check if there are any visible overlays */
174
+ hasOverlay(): boolean;
175
+ /** Check if an overlay entry is currently visible */
176
+ private isOverlayVisible;
177
+ /** Find the topmost visible capturing overlay, if any */
178
+ private getTopmostVisibleOverlay;
179
+ invalidate(): void;
180
+ start(): void;
181
+ addInputListener(listener: InputListener): () => void;
182
+ removeInputListener(listener: InputListener): void;
183
+ private queryCellSize;
184
+ stop(): void;
185
+ requestRender(force?: boolean): void;
186
+ private handleInput;
187
+ private consumeCellSizeResponse;
188
+ /**
189
+ * Resolve overlay layout from options.
190
+ * Returns { width, row, col, maxHeight } for rendering.
191
+ */
192
+ private resolveOverlayLayout;
193
+ private resolveAnchorRow;
194
+ private resolveAnchorCol;
195
+ /** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */
196
+ private compositeOverlays;
197
+ private static readonly SEGMENT_RESET;
198
+ private applyLineResets;
199
+ /** Splice overlay content into a base line at a specific column. Single-pass optimized. */
200
+ private compositeLineAt;
201
+ /**
202
+ * Find and extract cursor position from rendered lines.
203
+ * Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
204
+ * Only scans the bottom terminal height lines (visible viewport).
205
+ * @param lines - Rendered lines to search
206
+ * @param height - Terminal height (visible viewport size)
207
+ * @returns Cursor position { row, col } or null if no marker found
208
+ */
209
+ private extractCursorPosition;
210
+ private doRender;
211
+ /**
212
+ * Position the hardware cursor for IME candidate window.
213
+ * @param cursorPos The cursor position extracted from rendered output, or null
214
+ * @param totalLines Total number of rendered lines
215
+ */
216
+ private positionHardwareCursor;
217
+ }
218
+ //# sourceMappingURL=tui.d.ts.map