@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
package/dist/tui.js ADDED
@@ -0,0 +1,966 @@
1
+ /**
2
+ * Minimal TUI implementation with differential rendering
3
+ */
4
+ import * as fs from "node:fs";
5
+ import * as os from "node:os";
6
+ import * as path from "node:path";
7
+ import { isKeyRelease, matchesKey } from "./keys.js";
8
+ import { getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.js";
9
+ import { extractSegments, sliceByColumn, sliceWithWidth, visibleWidth } from "./utils.js";
10
+ /** Type guard to check if a component implements Focusable */
11
+ export function isFocusable(component) {
12
+ return component !== null && "focused" in component;
13
+ }
14
+ /**
15
+ * Cursor position marker - APC (Application Program Command) sequence.
16
+ * This is a zero-width escape sequence that terminals ignore.
17
+ * Components emit this at the cursor position when focused.
18
+ * TUI finds and strips this marker, then positions the hardware cursor there.
19
+ */
20
+ export const CURSOR_MARKER = "\x1b_pi:c\x07";
21
+ export { visibleWidth };
22
+ /** Parse a SizeValue into absolute value given a reference size */
23
+ function parseSizeValue(value, referenceSize) {
24
+ if (value === undefined)
25
+ return undefined;
26
+ if (typeof value === "number")
27
+ return value;
28
+ // Parse percentage string like "50%"
29
+ const match = value.match(/^(\d+(?:\.\d+)?)%$/);
30
+ if (match) {
31
+ return Math.floor((referenceSize * parseFloat(match[1])) / 100);
32
+ }
33
+ return undefined;
34
+ }
35
+ function isTermuxSession() {
36
+ return Boolean(process.env.TERMUX_VERSION);
37
+ }
38
+ /**
39
+ * Container - a component that contains other components
40
+ */
41
+ export class Container {
42
+ children = [];
43
+ addChild(component) {
44
+ this.children.push(component);
45
+ }
46
+ removeChild(component) {
47
+ const index = this.children.indexOf(component);
48
+ if (index !== -1) {
49
+ this.children.splice(index, 1);
50
+ }
51
+ }
52
+ clear() {
53
+ this.children = [];
54
+ }
55
+ invalidate() {
56
+ for (const child of this.children) {
57
+ child.invalidate?.();
58
+ }
59
+ }
60
+ render(width) {
61
+ const lines = [];
62
+ for (const child of this.children) {
63
+ lines.push(...child.render(width));
64
+ }
65
+ return lines;
66
+ }
67
+ }
68
+ /**
69
+ * TUI - Main class for managing terminal UI with differential rendering
70
+ */
71
+ export class TUI extends Container {
72
+ terminal;
73
+ previousLines = [];
74
+ previousWidth = 0;
75
+ previousHeight = 0;
76
+ focusedComponent = null;
77
+ inputListeners = new Set();
78
+ /** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
79
+ onDebug;
80
+ renderRequested = false;
81
+ cursorRow = 0; // Logical cursor row (end of rendered content)
82
+ hardwareCursorRow = 0; // Actual terminal cursor row (may differ due to IME positioning)
83
+ showHardwareCursor = process.env.PI_HARDWARE_CURSOR === "1";
84
+ clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1"; // Clear empty rows when content shrinks (default: off)
85
+ maxLinesRendered = 0; // Track terminal's working area (max lines ever rendered)
86
+ previousViewportTop = 0; // Track previous viewport top for resize-aware cursor moves
87
+ fullRedrawCount = 0;
88
+ stopped = false;
89
+ // Overlay stack for modal components rendered on top of base content
90
+ focusOrderCounter = 0;
91
+ overlayStack = [];
92
+ constructor(terminal, showHardwareCursor) {
93
+ super();
94
+ this.terminal = terminal;
95
+ if (showHardwareCursor !== undefined) {
96
+ this.showHardwareCursor = showHardwareCursor;
97
+ }
98
+ }
99
+ get fullRedraws() {
100
+ return this.fullRedrawCount;
101
+ }
102
+ getShowHardwareCursor() {
103
+ return this.showHardwareCursor;
104
+ }
105
+ setShowHardwareCursor(enabled) {
106
+ if (this.showHardwareCursor === enabled)
107
+ return;
108
+ this.showHardwareCursor = enabled;
109
+ if (!enabled) {
110
+ this.terminal.hideCursor();
111
+ }
112
+ this.requestRender();
113
+ }
114
+ getClearOnShrink() {
115
+ return this.clearOnShrink;
116
+ }
117
+ /**
118
+ * Set whether to trigger full re-render when content shrinks.
119
+ * When true (default), empty rows are cleared when content shrinks.
120
+ * When false, empty rows remain (reduces redraws on slower terminals).
121
+ */
122
+ setClearOnShrink(enabled) {
123
+ this.clearOnShrink = enabled;
124
+ }
125
+ setFocus(component) {
126
+ // Clear focused flag on old component
127
+ if (isFocusable(this.focusedComponent)) {
128
+ this.focusedComponent.focused = false;
129
+ }
130
+ this.focusedComponent = component;
131
+ // Set focused flag on new component
132
+ if (isFocusable(component)) {
133
+ component.focused = true;
134
+ }
135
+ }
136
+ /**
137
+ * Show an overlay component with configurable positioning and sizing.
138
+ * Returns a handle to control the overlay's visibility.
139
+ */
140
+ showOverlay(component, options) {
141
+ const entry = {
142
+ component,
143
+ options,
144
+ preFocus: this.focusedComponent,
145
+ hidden: false,
146
+ focusOrder: ++this.focusOrderCounter,
147
+ };
148
+ this.overlayStack.push(entry);
149
+ // Only focus if overlay is actually visible
150
+ if (!options?.nonCapturing && this.isOverlayVisible(entry)) {
151
+ this.setFocus(component);
152
+ }
153
+ this.terminal.hideCursor();
154
+ this.requestRender();
155
+ // Return handle for controlling this overlay
156
+ return {
157
+ hide: () => {
158
+ const index = this.overlayStack.indexOf(entry);
159
+ if (index !== -1) {
160
+ this.overlayStack.splice(index, 1);
161
+ // Restore focus if this overlay had focus
162
+ if (this.focusedComponent === component) {
163
+ const topVisible = this.getTopmostVisibleOverlay();
164
+ this.setFocus(topVisible?.component ?? entry.preFocus);
165
+ }
166
+ if (this.overlayStack.length === 0)
167
+ this.terminal.hideCursor();
168
+ this.requestRender();
169
+ }
170
+ },
171
+ setHidden: (hidden) => {
172
+ if (entry.hidden === hidden)
173
+ return;
174
+ entry.hidden = hidden;
175
+ // Update focus when hiding/showing
176
+ if (hidden) {
177
+ // If this overlay had focus, move focus to next visible or preFocus
178
+ if (this.focusedComponent === component) {
179
+ const topVisible = this.getTopmostVisibleOverlay();
180
+ this.setFocus(topVisible?.component ?? entry.preFocus);
181
+ }
182
+ }
183
+ else {
184
+ // Restore focus to this overlay when showing (if it's actually visible)
185
+ if (!options?.nonCapturing && this.isOverlayVisible(entry)) {
186
+ entry.focusOrder = ++this.focusOrderCounter;
187
+ this.setFocus(component);
188
+ }
189
+ }
190
+ this.requestRender();
191
+ },
192
+ isHidden: () => entry.hidden,
193
+ focus: () => {
194
+ if (!this.overlayStack.includes(entry) || !this.isOverlayVisible(entry))
195
+ return;
196
+ if (this.focusedComponent !== component) {
197
+ this.setFocus(component);
198
+ }
199
+ entry.focusOrder = ++this.focusOrderCounter;
200
+ this.requestRender();
201
+ },
202
+ unfocus: () => {
203
+ if (this.focusedComponent !== component)
204
+ return;
205
+ const topVisible = this.getTopmostVisibleOverlay();
206
+ this.setFocus(topVisible && topVisible !== entry ? topVisible.component : entry.preFocus);
207
+ this.requestRender();
208
+ },
209
+ isFocused: () => this.focusedComponent === component,
210
+ };
211
+ }
212
+ /** Hide the topmost overlay and restore previous focus. */
213
+ hideOverlay() {
214
+ const overlay = this.overlayStack.pop();
215
+ if (!overlay)
216
+ return;
217
+ if (this.focusedComponent === overlay.component) {
218
+ // Find topmost visible overlay, or fall back to preFocus
219
+ const topVisible = this.getTopmostVisibleOverlay();
220
+ this.setFocus(topVisible?.component ?? overlay.preFocus);
221
+ }
222
+ if (this.overlayStack.length === 0)
223
+ this.terminal.hideCursor();
224
+ this.requestRender();
225
+ }
226
+ /** Check if there are any visible overlays */
227
+ hasOverlay() {
228
+ return this.overlayStack.some((o) => this.isOverlayVisible(o));
229
+ }
230
+ /** Check if an overlay entry is currently visible */
231
+ isOverlayVisible(entry) {
232
+ if (entry.hidden)
233
+ return false;
234
+ if (entry.options?.visible) {
235
+ return entry.options.visible(this.terminal.columns, this.terminal.rows);
236
+ }
237
+ return true;
238
+ }
239
+ /** Find the topmost visible capturing overlay, if any */
240
+ getTopmostVisibleOverlay() {
241
+ for (let i = this.overlayStack.length - 1; i >= 0; i--) {
242
+ if (this.overlayStack[i].options?.nonCapturing)
243
+ continue;
244
+ if (this.isOverlayVisible(this.overlayStack[i])) {
245
+ return this.overlayStack[i];
246
+ }
247
+ }
248
+ return undefined;
249
+ }
250
+ invalidate() {
251
+ super.invalidate();
252
+ for (const overlay of this.overlayStack)
253
+ overlay.component.invalidate?.();
254
+ }
255
+ start() {
256
+ this.stopped = false;
257
+ this.terminal.start((data) => this.handleInput(data), () => this.requestRender());
258
+ this.terminal.hideCursor();
259
+ this.queryCellSize();
260
+ this.requestRender();
261
+ }
262
+ addInputListener(listener) {
263
+ this.inputListeners.add(listener);
264
+ return () => {
265
+ this.inputListeners.delete(listener);
266
+ };
267
+ }
268
+ removeInputListener(listener) {
269
+ this.inputListeners.delete(listener);
270
+ }
271
+ queryCellSize() {
272
+ // Only query if terminal supports images (cell size is only used for image rendering)
273
+ if (!getCapabilities().images) {
274
+ return;
275
+ }
276
+ // Query terminal for cell size in pixels: CSI 16 t
277
+ // Response format: CSI 6 ; height ; width t
278
+ this.terminal.write("\x1b[16t");
279
+ }
280
+ stop() {
281
+ this.stopped = true;
282
+ // Move cursor to the end of the content to prevent overwriting/artifacts on exit
283
+ if (this.previousLines.length > 0) {
284
+ const targetRow = this.previousLines.length; // Line after the last content
285
+ const lineDiff = targetRow - this.hardwareCursorRow;
286
+ if (lineDiff > 0) {
287
+ this.terminal.write(`\x1b[${lineDiff}B`);
288
+ }
289
+ else if (lineDiff < 0) {
290
+ this.terminal.write(`\x1b[${-lineDiff}A`);
291
+ }
292
+ this.terminal.write("\r\n");
293
+ }
294
+ this.terminal.showCursor();
295
+ this.terminal.stop();
296
+ }
297
+ requestRender(force = false) {
298
+ if (force) {
299
+ this.previousLines = [];
300
+ this.previousWidth = -1; // -1 triggers widthChanged, forcing a full clear
301
+ this.previousHeight = -1; // -1 triggers heightChanged, forcing a full clear
302
+ this.cursorRow = 0;
303
+ this.hardwareCursorRow = 0;
304
+ this.maxLinesRendered = 0;
305
+ this.previousViewportTop = 0;
306
+ }
307
+ if (this.renderRequested)
308
+ return;
309
+ this.renderRequested = true;
310
+ process.nextTick(() => {
311
+ this.renderRequested = false;
312
+ this.doRender();
313
+ });
314
+ }
315
+ handleInput(data) {
316
+ if (this.inputListeners.size > 0) {
317
+ let current = data;
318
+ for (const listener of this.inputListeners) {
319
+ const result = listener(current);
320
+ if (result?.consume) {
321
+ return;
322
+ }
323
+ if (result?.data !== undefined) {
324
+ current = result.data;
325
+ }
326
+ }
327
+ if (current.length === 0) {
328
+ return;
329
+ }
330
+ data = current;
331
+ }
332
+ // Consume terminal cell size responses without blocking unrelated input.
333
+ if (this.consumeCellSizeResponse(data)) {
334
+ return;
335
+ }
336
+ // Global debug key handler (Shift+Ctrl+D)
337
+ if (matchesKey(data, "shift+ctrl+d") && this.onDebug) {
338
+ this.onDebug();
339
+ return;
340
+ }
341
+ // If focused component is an overlay, verify it's still visible
342
+ // (visibility can change due to terminal resize or visible() callback)
343
+ const focusedOverlay = this.overlayStack.find((o) => o.component === this.focusedComponent);
344
+ if (focusedOverlay && !this.isOverlayVisible(focusedOverlay)) {
345
+ // Focused overlay is no longer visible, redirect to topmost visible overlay
346
+ const topVisible = this.getTopmostVisibleOverlay();
347
+ if (topVisible) {
348
+ this.setFocus(topVisible.component);
349
+ }
350
+ else {
351
+ // No visible overlays, restore to preFocus
352
+ this.setFocus(focusedOverlay.preFocus);
353
+ }
354
+ }
355
+ // Pass input to focused component (including Ctrl+C)
356
+ // The focused component can decide how to handle Ctrl+C
357
+ if (this.focusedComponent?.handleInput) {
358
+ // Filter out key release events unless component opts in
359
+ if (isKeyRelease(data) && !this.focusedComponent.wantsKeyRelease) {
360
+ return;
361
+ }
362
+ this.focusedComponent.handleInput(data);
363
+ this.requestRender();
364
+ }
365
+ }
366
+ consumeCellSizeResponse(data) {
367
+ // Response format: ESC [ 6 ; height ; width t
368
+ const match = data.match(/^\x1b\[6;(\d+);(\d+)t$/);
369
+ if (!match) {
370
+ return false;
371
+ }
372
+ const heightPx = parseInt(match[1], 10);
373
+ const widthPx = parseInt(match[2], 10);
374
+ if (heightPx <= 0 || widthPx <= 0) {
375
+ return true;
376
+ }
377
+ setCellDimensions({ widthPx, heightPx });
378
+ // Invalidate all components so images re-render with correct dimensions.
379
+ this.invalidate();
380
+ this.requestRender();
381
+ return true;
382
+ }
383
+ /**
384
+ * Resolve overlay layout from options.
385
+ * Returns { width, row, col, maxHeight } for rendering.
386
+ */
387
+ resolveOverlayLayout(options, overlayHeight, termWidth, termHeight) {
388
+ const opt = options ?? {};
389
+ // Parse margin (clamp to non-negative)
390
+ const margin = typeof opt.margin === "number"
391
+ ? { top: opt.margin, right: opt.margin, bottom: opt.margin, left: opt.margin }
392
+ : (opt.margin ?? {});
393
+ const marginTop = Math.max(0, margin.top ?? 0);
394
+ const marginRight = Math.max(0, margin.right ?? 0);
395
+ const marginBottom = Math.max(0, margin.bottom ?? 0);
396
+ const marginLeft = Math.max(0, margin.left ?? 0);
397
+ // Available space after margins
398
+ const availWidth = Math.max(1, termWidth - marginLeft - marginRight);
399
+ const availHeight = Math.max(1, termHeight - marginTop - marginBottom);
400
+ // === Resolve width ===
401
+ let width = parseSizeValue(opt.width, termWidth) ?? Math.min(80, availWidth);
402
+ // Apply minWidth
403
+ if (opt.minWidth !== undefined) {
404
+ width = Math.max(width, opt.minWidth);
405
+ }
406
+ // Clamp to available space
407
+ width = Math.max(1, Math.min(width, availWidth));
408
+ // === Resolve maxHeight ===
409
+ let maxHeight = parseSizeValue(opt.maxHeight, termHeight);
410
+ // Clamp to available space
411
+ if (maxHeight !== undefined) {
412
+ maxHeight = Math.max(1, Math.min(maxHeight, availHeight));
413
+ }
414
+ // Effective overlay height (may be clamped by maxHeight)
415
+ const effectiveHeight = maxHeight !== undefined ? Math.min(overlayHeight, maxHeight) : overlayHeight;
416
+ // === Resolve position ===
417
+ let row;
418
+ let col;
419
+ if (opt.row !== undefined) {
420
+ if (typeof opt.row === "string") {
421
+ // Percentage: 0% = top, 100% = bottom (overlay stays within bounds)
422
+ const match = opt.row.match(/^(\d+(?:\.\d+)?)%$/);
423
+ if (match) {
424
+ const maxRow = Math.max(0, availHeight - effectiveHeight);
425
+ const percent = parseFloat(match[1]) / 100;
426
+ row = marginTop + Math.floor(maxRow * percent);
427
+ }
428
+ else {
429
+ // Invalid format, fall back to center
430
+ row = this.resolveAnchorRow("center", effectiveHeight, availHeight, marginTop);
431
+ }
432
+ }
433
+ else {
434
+ // Absolute row position
435
+ row = opt.row;
436
+ }
437
+ }
438
+ else {
439
+ // Anchor-based (default: center)
440
+ const anchor = opt.anchor ?? "center";
441
+ row = this.resolveAnchorRow(anchor, effectiveHeight, availHeight, marginTop);
442
+ }
443
+ if (opt.col !== undefined) {
444
+ if (typeof opt.col === "string") {
445
+ // Percentage: 0% = left, 100% = right (overlay stays within bounds)
446
+ const match = opt.col.match(/^(\d+(?:\.\d+)?)%$/);
447
+ if (match) {
448
+ const maxCol = Math.max(0, availWidth - width);
449
+ const percent = parseFloat(match[1]) / 100;
450
+ col = marginLeft + Math.floor(maxCol * percent);
451
+ }
452
+ else {
453
+ // Invalid format, fall back to center
454
+ col = this.resolveAnchorCol("center", width, availWidth, marginLeft);
455
+ }
456
+ }
457
+ else {
458
+ // Absolute column position
459
+ col = opt.col;
460
+ }
461
+ }
462
+ else {
463
+ // Anchor-based (default: center)
464
+ const anchor = opt.anchor ?? "center";
465
+ col = this.resolveAnchorCol(anchor, width, availWidth, marginLeft);
466
+ }
467
+ // Apply offsets
468
+ if (opt.offsetY !== undefined)
469
+ row += opt.offsetY;
470
+ if (opt.offsetX !== undefined)
471
+ col += opt.offsetX;
472
+ // Clamp to terminal bounds (respecting margins)
473
+ row = Math.max(marginTop, Math.min(row, termHeight - marginBottom - effectiveHeight));
474
+ col = Math.max(marginLeft, Math.min(col, termWidth - marginRight - width));
475
+ return { width, row, col, maxHeight };
476
+ }
477
+ resolveAnchorRow(anchor, height, availHeight, marginTop) {
478
+ switch (anchor) {
479
+ case "top-left":
480
+ case "top-center":
481
+ case "top-right":
482
+ return marginTop;
483
+ case "bottom-left":
484
+ case "bottom-center":
485
+ case "bottom-right":
486
+ return marginTop + availHeight - height;
487
+ case "left-center":
488
+ case "center":
489
+ case "right-center":
490
+ return marginTop + Math.floor((availHeight - height) / 2);
491
+ }
492
+ }
493
+ resolveAnchorCol(anchor, width, availWidth, marginLeft) {
494
+ switch (anchor) {
495
+ case "top-left":
496
+ case "left-center":
497
+ case "bottom-left":
498
+ return marginLeft;
499
+ case "top-right":
500
+ case "right-center":
501
+ case "bottom-right":
502
+ return marginLeft + availWidth - width;
503
+ case "top-center":
504
+ case "center":
505
+ case "bottom-center":
506
+ return marginLeft + Math.floor((availWidth - width) / 2);
507
+ }
508
+ }
509
+ /** Composite all overlays into content lines (sorted by focusOrder, higher = on top). */
510
+ compositeOverlays(lines, termWidth, termHeight) {
511
+ if (this.overlayStack.length === 0)
512
+ return lines;
513
+ const result = [...lines];
514
+ // Pre-render all visible overlays and calculate positions
515
+ const rendered = [];
516
+ let minLinesNeeded = result.length;
517
+ const visibleEntries = this.overlayStack.filter((e) => this.isOverlayVisible(e));
518
+ visibleEntries.sort((a, b) => a.focusOrder - b.focusOrder);
519
+ for (const entry of visibleEntries) {
520
+ const { component, options } = entry;
521
+ // Get layout with height=0 first to determine width and maxHeight
522
+ // (width and maxHeight don't depend on overlay height)
523
+ const { width, maxHeight } = this.resolveOverlayLayout(options, 0, termWidth, termHeight);
524
+ // Render component at calculated width
525
+ let overlayLines = component.render(width);
526
+ // Apply maxHeight if specified
527
+ if (maxHeight !== undefined && overlayLines.length > maxHeight) {
528
+ overlayLines = overlayLines.slice(0, maxHeight);
529
+ }
530
+ // Get final row/col with actual overlay height
531
+ const { row, col } = this.resolveOverlayLayout(options, overlayLines.length, termWidth, termHeight);
532
+ rendered.push({ overlayLines, row, col, w: width });
533
+ minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length);
534
+ }
535
+ // Ensure result covers the terminal working area to keep overlay positioning stable across resizes.
536
+ // maxLinesRendered can exceed current content length after a shrink; pad to keep viewportStart consistent.
537
+ const workingHeight = Math.max(this.maxLinesRendered, minLinesNeeded);
538
+ // Extend result with empty lines if content is too short for overlay placement or working area
539
+ while (result.length < workingHeight) {
540
+ result.push("");
541
+ }
542
+ const viewportStart = Math.max(0, workingHeight - termHeight);
543
+ // Composite each overlay
544
+ for (const { overlayLines, row, col, w } of rendered) {
545
+ for (let i = 0; i < overlayLines.length; i++) {
546
+ const idx = viewportStart + row + i;
547
+ if (idx >= 0 && idx < result.length) {
548
+ // Defensive: truncate overlay line to declared width before compositing
549
+ // (components should already respect width, but this ensures it)
550
+ const truncatedOverlayLine = visibleWidth(overlayLines[i]) > w ? sliceByColumn(overlayLines[i], 0, w, true) : overlayLines[i];
551
+ result[idx] = this.compositeLineAt(result[idx], truncatedOverlayLine, col, w, termWidth);
552
+ }
553
+ }
554
+ }
555
+ return result;
556
+ }
557
+ static SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07";
558
+ applyLineResets(lines) {
559
+ const reset = TUI.SEGMENT_RESET;
560
+ for (let i = 0; i < lines.length; i++) {
561
+ const line = lines[i];
562
+ if (!isImageLine(line)) {
563
+ lines[i] = line + reset;
564
+ }
565
+ }
566
+ return lines;
567
+ }
568
+ /** Splice overlay content into a base line at a specific column. Single-pass optimized. */
569
+ compositeLineAt(baseLine, overlayLine, startCol, overlayWidth, totalWidth) {
570
+ if (isImageLine(baseLine))
571
+ return baseLine;
572
+ // Single pass through baseLine extracts both before and after segments
573
+ const afterStart = startCol + overlayWidth;
574
+ const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
575
+ // Extract overlay with width tracking (strict=true to exclude wide chars at boundary)
576
+ const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
577
+ // Pad segments to target widths
578
+ const beforePad = Math.max(0, startCol - base.beforeWidth);
579
+ const overlayPad = Math.max(0, overlayWidth - overlay.width);
580
+ const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
581
+ const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
582
+ const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
583
+ const afterPad = Math.max(0, afterTarget - base.afterWidth);
584
+ // Compose result
585
+ const r = TUI.SEGMENT_RESET;
586
+ const result = base.before +
587
+ " ".repeat(beforePad) +
588
+ r +
589
+ overlay.text +
590
+ " ".repeat(overlayPad) +
591
+ r +
592
+ base.after +
593
+ " ".repeat(afterPad);
594
+ // CRITICAL: Always verify and truncate to terminal width.
595
+ // This is the final safeguard against width overflow which would crash the TUI.
596
+ // Width tracking can drift from actual visible width due to:
597
+ // - Complex ANSI/OSC sequences (hyperlinks, colors)
598
+ // - Wide characters at segment boundaries
599
+ // - Edge cases in segment extraction
600
+ const resultWidth = visibleWidth(result);
601
+ if (resultWidth <= totalWidth) {
602
+ return result;
603
+ }
604
+ // Truncate with strict=true to ensure we don't exceed totalWidth
605
+ return sliceByColumn(result, 0, totalWidth, true);
606
+ }
607
+ /**
608
+ * Find and extract cursor position from rendered lines.
609
+ * Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
610
+ * Only scans the bottom terminal height lines (visible viewport).
611
+ * @param lines - Rendered lines to search
612
+ * @param height - Terminal height (visible viewport size)
613
+ * @returns Cursor position { row, col } or null if no marker found
614
+ */
615
+ extractCursorPosition(lines, height) {
616
+ // Only scan the bottom `height` lines (visible viewport)
617
+ const viewportTop = Math.max(0, lines.length - height);
618
+ for (let row = lines.length - 1; row >= viewportTop; row--) {
619
+ const line = lines[row];
620
+ const markerIndex = line.indexOf(CURSOR_MARKER);
621
+ if (markerIndex !== -1) {
622
+ // Calculate visual column (width of text before marker)
623
+ const beforeMarker = line.slice(0, markerIndex);
624
+ const col = visibleWidth(beforeMarker);
625
+ // Strip marker from the line
626
+ lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);
627
+ return { row, col };
628
+ }
629
+ }
630
+ return null;
631
+ }
632
+ doRender() {
633
+ if (this.stopped)
634
+ return;
635
+ const width = this.terminal.columns;
636
+ const height = this.terminal.rows;
637
+ const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;
638
+ const heightChanged = this.previousHeight !== 0 && this.previousHeight !== height;
639
+ const previousBufferLength = this.previousHeight > 0 ? this.previousViewportTop + this.previousHeight : height;
640
+ let prevViewportTop = heightChanged ? Math.max(0, previousBufferLength - height) : this.previousViewportTop;
641
+ let viewportTop = prevViewportTop;
642
+ let hardwareCursorRow = this.hardwareCursorRow;
643
+ const computeLineDiff = (targetRow) => {
644
+ const currentScreenRow = hardwareCursorRow - prevViewportTop;
645
+ const targetScreenRow = targetRow - viewportTop;
646
+ return targetScreenRow - currentScreenRow;
647
+ };
648
+ // Render all components to get new lines
649
+ let newLines = this.render(width);
650
+ // Composite overlays into the rendered lines (before differential compare)
651
+ if (this.overlayStack.length > 0) {
652
+ newLines = this.compositeOverlays(newLines, width, height);
653
+ }
654
+ // Extract cursor position before applying line resets (marker must be found first)
655
+ const cursorPos = this.extractCursorPosition(newLines, height);
656
+ newLines = this.applyLineResets(newLines);
657
+ // Helper to clear scrollback and viewport and render all new lines
658
+ const fullRender = (clear) => {
659
+ this.fullRedrawCount += 1;
660
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
661
+ if (clear)
662
+ buffer += "\x1b[2J\x1b[H\x1b[3J"; // Clear screen, home, then clear scrollback
663
+ for (let i = 0; i < newLines.length; i++) {
664
+ if (i > 0)
665
+ buffer += "\r\n";
666
+ buffer += newLines[i];
667
+ }
668
+ buffer += "\x1b[?2026l"; // End synchronized output
669
+ this.terminal.write(buffer);
670
+ this.cursorRow = Math.max(0, newLines.length - 1);
671
+ this.hardwareCursorRow = this.cursorRow;
672
+ // Reset max lines when clearing, otherwise track growth
673
+ if (clear) {
674
+ this.maxLinesRendered = newLines.length;
675
+ }
676
+ else {
677
+ this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
678
+ }
679
+ const bufferLength = Math.max(height, newLines.length);
680
+ this.previousViewportTop = Math.max(0, bufferLength - height);
681
+ this.positionHardwareCursor(cursorPos, newLines.length);
682
+ this.previousLines = newLines;
683
+ this.previousWidth = width;
684
+ this.previousHeight = height;
685
+ };
686
+ const debugRedraw = process.env.PI_DEBUG_REDRAW === "1";
687
+ const logRedraw = (reason) => {
688
+ if (!debugRedraw)
689
+ return;
690
+ const logPath = path.join(os.homedir(), ".pi", "agent", "pi-debug.log");
691
+ const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`;
692
+ fs.appendFileSync(logPath, msg);
693
+ };
694
+ // First render - just output everything without clearing (assumes clean screen)
695
+ if (this.previousLines.length === 0 && !widthChanged && !heightChanged) {
696
+ logRedraw("first render");
697
+ fullRender(false);
698
+ return;
699
+ }
700
+ // Width changes always need a full re-render because wrapping changes.
701
+ if (widthChanged) {
702
+ logRedraw(`terminal width changed (${this.previousWidth} -> ${width})`);
703
+ fullRender(true);
704
+ return;
705
+ }
706
+ // Height changes normally need a full re-render to keep the visible viewport aligned,
707
+ // but Termux changes height when the software keyboard shows or hides.
708
+ // In that environment, a full redraw causes the entire history to replay on every toggle.
709
+ if (heightChanged && !isTermuxSession()) {
710
+ logRedraw(`terminal height changed (${this.previousHeight} -> ${height})`);
711
+ fullRender(true);
712
+ return;
713
+ }
714
+ // Content shrunk below the working area and no overlays - re-render to clear empty rows
715
+ // (overlays need the padding, so only do this when no overlays are active)
716
+ // Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
717
+ if (this.clearOnShrink && newLines.length < this.maxLinesRendered && this.overlayStack.length === 0) {
718
+ logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);
719
+ fullRender(true);
720
+ return;
721
+ }
722
+ // Find first and last changed lines
723
+ let firstChanged = -1;
724
+ let lastChanged = -1;
725
+ const maxLines = Math.max(newLines.length, this.previousLines.length);
726
+ for (let i = 0; i < maxLines; i++) {
727
+ const oldLine = i < this.previousLines.length ? this.previousLines[i] : "";
728
+ const newLine = i < newLines.length ? newLines[i] : "";
729
+ if (oldLine !== newLine) {
730
+ if (firstChanged === -1) {
731
+ firstChanged = i;
732
+ }
733
+ lastChanged = i;
734
+ }
735
+ }
736
+ const appendedLines = newLines.length > this.previousLines.length;
737
+ if (appendedLines) {
738
+ if (firstChanged === -1) {
739
+ firstChanged = this.previousLines.length;
740
+ }
741
+ lastChanged = newLines.length - 1;
742
+ }
743
+ const appendStart = appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;
744
+ // No changes - but still need to update hardware cursor position if it moved
745
+ if (firstChanged === -1) {
746
+ this.positionHardwareCursor(cursorPos, newLines.length);
747
+ this.previousViewportTop = prevViewportTop;
748
+ this.previousHeight = height;
749
+ return;
750
+ }
751
+ // All changes are in deleted lines (nothing to render, just clear)
752
+ if (firstChanged >= newLines.length) {
753
+ if (this.previousLines.length > newLines.length) {
754
+ let buffer = "\x1b[?2026h";
755
+ // Move to end of new content (clamp to 0 for empty content)
756
+ const targetRow = Math.max(0, newLines.length - 1);
757
+ if (targetRow < prevViewportTop) {
758
+ logRedraw(`deleted lines moved viewport up (${targetRow} < ${prevViewportTop})`);
759
+ fullRender(true);
760
+ return;
761
+ }
762
+ const lineDiff = computeLineDiff(targetRow);
763
+ if (lineDiff > 0)
764
+ buffer += `\x1b[${lineDiff}B`;
765
+ else if (lineDiff < 0)
766
+ buffer += `\x1b[${-lineDiff}A`;
767
+ buffer += "\r";
768
+ // Clear extra lines without scrolling
769
+ const extraLines = this.previousLines.length - newLines.length;
770
+ if (extraLines > height) {
771
+ logRedraw(`extraLines > height (${extraLines} > ${height})`);
772
+ fullRender(true);
773
+ return;
774
+ }
775
+ if (extraLines > 0) {
776
+ buffer += "\x1b[1B";
777
+ }
778
+ for (let i = 0; i < extraLines; i++) {
779
+ buffer += "\r\x1b[2K";
780
+ if (i < extraLines - 1)
781
+ buffer += "\x1b[1B";
782
+ }
783
+ if (extraLines > 0) {
784
+ buffer += `\x1b[${extraLines}A`;
785
+ }
786
+ buffer += "\x1b[?2026l";
787
+ this.terminal.write(buffer);
788
+ this.cursorRow = targetRow;
789
+ this.hardwareCursorRow = targetRow;
790
+ }
791
+ this.positionHardwareCursor(cursorPos, newLines.length);
792
+ this.previousLines = newLines;
793
+ this.previousWidth = width;
794
+ this.previousHeight = height;
795
+ this.previousViewportTop = prevViewportTop;
796
+ return;
797
+ }
798
+ // Differential rendering can only touch what was actually visible.
799
+ // If the first changed line is above the previous viewport, we need a full redraw.
800
+ if (firstChanged < prevViewportTop) {
801
+ logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
802
+ fullRender(true);
803
+ return;
804
+ }
805
+ // Render from first changed line to end
806
+ // Build buffer with all updates wrapped in synchronized output
807
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
808
+ const prevViewportBottom = prevViewportTop + height - 1;
809
+ const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
810
+ if (moveTargetRow > prevViewportBottom) {
811
+ const currentScreenRow = Math.max(0, Math.min(height - 1, hardwareCursorRow - prevViewportTop));
812
+ const moveToBottom = height - 1 - currentScreenRow;
813
+ if (moveToBottom > 0) {
814
+ buffer += `\x1b[${moveToBottom}B`;
815
+ }
816
+ const scroll = moveTargetRow - prevViewportBottom;
817
+ buffer += "\r\n".repeat(scroll);
818
+ prevViewportTop += scroll;
819
+ viewportTop += scroll;
820
+ hardwareCursorRow = moveTargetRow;
821
+ }
822
+ // Move cursor to first changed line (use hardwareCursorRow for actual position)
823
+ const lineDiff = computeLineDiff(moveTargetRow);
824
+ if (lineDiff > 0) {
825
+ buffer += `\x1b[${lineDiff}B`; // Move down
826
+ }
827
+ else if (lineDiff < 0) {
828
+ buffer += `\x1b[${-lineDiff}A`; // Move up
829
+ }
830
+ buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
831
+ // Only render changed lines (firstChanged to lastChanged), not all lines to end
832
+ // This reduces flicker when only a single line changes (e.g., spinner animation)
833
+ const renderEnd = Math.min(lastChanged, newLines.length - 1);
834
+ for (let i = firstChanged; i <= renderEnd; i++) {
835
+ if (i > firstChanged)
836
+ buffer += "\r\n";
837
+ buffer += "\x1b[2K"; // Clear current line
838
+ const line = newLines[i];
839
+ const isImage = isImageLine(line);
840
+ if (!isImage && visibleWidth(line) > width) {
841
+ // Log all lines to crash file for debugging
842
+ const crashLogPath = path.join(os.homedir(), ".pi", "agent", "pi-crash.log");
843
+ const crashData = [
844
+ `Crash at ${new Date().toISOString()}`,
845
+ `Terminal width: ${width}`,
846
+ `Line ${i} visible width: ${visibleWidth(line)}`,
847
+ "",
848
+ "=== All rendered lines ===",
849
+ ...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),
850
+ "",
851
+ ].join("\n");
852
+ fs.mkdirSync(path.dirname(crashLogPath), { recursive: true });
853
+ fs.writeFileSync(crashLogPath, crashData);
854
+ // Clean up terminal state before throwing
855
+ this.stop();
856
+ const errorMsg = [
857
+ `Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,
858
+ "",
859
+ "This is likely caused by a custom TUI component not truncating its output.",
860
+ "Use visibleWidth() to measure and truncateToWidth() to truncate lines.",
861
+ "",
862
+ `Debug log written to: ${crashLogPath}`,
863
+ ].join("\n");
864
+ throw new Error(errorMsg);
865
+ }
866
+ buffer += line;
867
+ }
868
+ // Track where cursor ended up after rendering
869
+ let finalCursorRow = renderEnd;
870
+ // If we had more lines before, clear them and move cursor back
871
+ if (this.previousLines.length > newLines.length) {
872
+ // Move to end of new content first if we stopped before it
873
+ if (renderEnd < newLines.length - 1) {
874
+ const moveDown = newLines.length - 1 - renderEnd;
875
+ buffer += `\x1b[${moveDown}B`;
876
+ finalCursorRow = newLines.length - 1;
877
+ }
878
+ const extraLines = this.previousLines.length - newLines.length;
879
+ for (let i = newLines.length; i < this.previousLines.length; i++) {
880
+ buffer += "\r\n\x1b[2K";
881
+ }
882
+ // Move cursor back to end of new content
883
+ buffer += `\x1b[${extraLines}A`;
884
+ }
885
+ buffer += "\x1b[?2026l"; // End synchronized output
886
+ if (process.env.PI_TUI_DEBUG === "1") {
887
+ const debugDir = "/tmp/tui";
888
+ fs.mkdirSync(debugDir, { recursive: true });
889
+ const debugPath = path.join(debugDir, `render-${Date.now()}-${Math.random().toString(36).slice(2)}.log`);
890
+ const debugData = [
891
+ `firstChanged: ${firstChanged}`,
892
+ `viewportTop: ${viewportTop}`,
893
+ `cursorRow: ${this.cursorRow}`,
894
+ `height: ${height}`,
895
+ `lineDiff: ${lineDiff}`,
896
+ `hardwareCursorRow: ${hardwareCursorRow}`,
897
+ `renderEnd: ${renderEnd}`,
898
+ `finalCursorRow: ${finalCursorRow}`,
899
+ `cursorPos: ${JSON.stringify(cursorPos)}`,
900
+ `newLines.length: ${newLines.length}`,
901
+ `previousLines.length: ${this.previousLines.length}`,
902
+ "",
903
+ "=== newLines ===",
904
+ JSON.stringify(newLines, null, 2),
905
+ "",
906
+ "=== previousLines ===",
907
+ JSON.stringify(this.previousLines, null, 2),
908
+ "",
909
+ "=== buffer ===",
910
+ JSON.stringify(buffer),
911
+ ].join("\n");
912
+ fs.writeFileSync(debugPath, debugData);
913
+ }
914
+ // Write entire buffer at once
915
+ this.terminal.write(buffer);
916
+ // Track cursor position for next render
917
+ // cursorRow tracks end of content (for viewport calculation)
918
+ // hardwareCursorRow tracks actual terminal cursor position (for movement)
919
+ this.cursorRow = Math.max(0, newLines.length - 1);
920
+ this.hardwareCursorRow = finalCursorRow;
921
+ // Track terminal's working area (grows but doesn't shrink unless cleared)
922
+ this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
923
+ this.previousViewportTop = Math.max(prevViewportTop, finalCursorRow - height + 1);
924
+ // Position hardware cursor for IME
925
+ this.positionHardwareCursor(cursorPos, newLines.length);
926
+ this.previousLines = newLines;
927
+ this.previousWidth = width;
928
+ this.previousHeight = height;
929
+ }
930
+ /**
931
+ * Position the hardware cursor for IME candidate window.
932
+ * @param cursorPos The cursor position extracted from rendered output, or null
933
+ * @param totalLines Total number of rendered lines
934
+ */
935
+ positionHardwareCursor(cursorPos, totalLines) {
936
+ if (!cursorPos || totalLines <= 0) {
937
+ this.terminal.hideCursor();
938
+ return;
939
+ }
940
+ // Clamp cursor position to valid range
941
+ const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
942
+ const targetCol = Math.max(0, cursorPos.col);
943
+ // Move cursor from current position to target
944
+ const rowDelta = targetRow - this.hardwareCursorRow;
945
+ let buffer = "";
946
+ if (rowDelta > 0) {
947
+ buffer += `\x1b[${rowDelta}B`; // Move down
948
+ }
949
+ else if (rowDelta < 0) {
950
+ buffer += `\x1b[${-rowDelta}A`; // Move up
951
+ }
952
+ // Move to absolute column (1-indexed)
953
+ buffer += `\x1b[${targetCol + 1}G`;
954
+ if (buffer) {
955
+ this.terminal.write(buffer);
956
+ }
957
+ this.hardwareCursorRow = targetRow;
958
+ if (this.showHardwareCursor) {
959
+ this.terminal.showCursor();
960
+ }
961
+ else {
962
+ this.terminal.hideCursor();
963
+ }
964
+ }
965
+ }
966
+ //# sourceMappingURL=tui.js.map