@dungle-scrubs/tallow 0.8.21 → 0.8.23

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 (217) hide show
  1. package/dist/cli.js +35 -4
  2. package/dist/cli.js.map +1 -1
  3. package/dist/config.d.ts +1 -1
  4. package/dist/config.js +1 -1
  5. package/dist/interactive-mode-patch.d.ts +2 -0
  6. package/dist/interactive-mode-patch.d.ts.map +1 -1
  7. package/dist/interactive-mode-patch.js +82 -0
  8. package/dist/interactive-mode-patch.js.map +1 -1
  9. package/dist/sdk.d.ts +17 -0
  10. package/dist/sdk.d.ts.map +1 -1
  11. package/dist/sdk.js +68 -1
  12. package/dist/sdk.js.map +1 -1
  13. package/dist/workspace-transition-relay.d.ts +40 -7
  14. package/dist/workspace-transition-relay.d.ts.map +1 -1
  15. package/dist/workspace-transition-relay.js +81 -16
  16. package/dist/workspace-transition-relay.js.map +1 -1
  17. package/extensions/__integration__/background-task-widget-ownership.test.ts +216 -0
  18. package/extensions/__integration__/claude-hooks-compat.test.ts +156 -0
  19. package/extensions/__integration__/slash-command-bridge.test.ts +169 -23
  20. package/extensions/_shared/atomic-write.ts +1 -1
  21. package/extensions/_shared/bordered-box.ts +102 -0
  22. package/extensions/_shared/interop-events.ts +5 -0
  23. package/extensions/_shared/pid-registry.ts +1 -1
  24. package/extensions/agent-commands-tool/index.ts +4 -1
  25. package/extensions/background-task-tool/__tests__/lifecycle.test.ts +50 -25
  26. package/extensions/background-task-tool/index.ts +139 -221
  27. package/extensions/bash-tool-enhanced/index.ts +1 -75
  28. package/extensions/cd-tool/index.ts +2 -2
  29. package/extensions/context-fork/spawn.ts +4 -1
  30. package/extensions/health/index.ts +6 -6
  31. package/extensions/hooks/__tests__/claude-compat.test.ts +35 -0
  32. package/extensions/hooks/__tests__/subprocess-hardening.test.ts +73 -0
  33. package/extensions/hooks/index.ts +27 -4
  34. package/extensions/loop/__tests__/loop.test.ts +168 -4
  35. package/extensions/loop/extension.json +6 -5
  36. package/extensions/loop/index.ts +242 -31
  37. package/extensions/plan-mode-tool/__tests__/agent-end-execution.test.ts +373 -0
  38. package/extensions/plan-mode-tool/index.ts +103 -41
  39. package/extensions/prompt-suggestions/__tests__/editor-compatibility.test.ts +42 -0
  40. package/extensions/prompt-suggestions/index.ts +41 -6
  41. package/extensions/slash-command-bridge/__tests__/slash-command-bridge.test.ts +267 -671
  42. package/extensions/slash-command-bridge/extension.json +1 -1
  43. package/extensions/slash-command-bridge/index.ts +230 -116
  44. package/extensions/subagent-tool/index.ts +2 -2
  45. package/extensions/subagent-tool/process.ts +4 -5
  46. package/extensions/tasks/commands/register-tasks-extension.ts +41 -0
  47. package/extensions/teams-tool/__tests__/peer-messaging.test.ts +29 -24
  48. package/extensions/teams-tool/dashboard.ts +3 -5
  49. package/extensions/teams-tool/dispatch/auto-dispatch.ts +18 -1
  50. package/extensions/teams-tool/tools/teammate-tools.ts +9 -6
  51. package/extensions/wezterm-pane-control/__tests__/index.test.ts +88 -4
  52. package/extensions/wezterm-pane-control/index.ts +113 -8
  53. package/package.json +6 -4
  54. package/packages/tallow-tui/README.md +51 -0
  55. package/packages/tallow-tui/dist/autocomplete.d.ts +48 -0
  56. package/packages/tallow-tui/dist/autocomplete.d.ts.map +1 -0
  57. package/packages/tallow-tui/dist/autocomplete.js +564 -0
  58. package/packages/tallow-tui/dist/autocomplete.js.map +1 -0
  59. package/packages/tallow-tui/dist/border-styles.d.ts +32 -0
  60. package/packages/tallow-tui/dist/border-styles.d.ts.map +1 -0
  61. package/packages/tallow-tui/dist/border-styles.js +46 -0
  62. package/packages/tallow-tui/dist/border-styles.js.map +1 -0
  63. package/packages/tallow-tui/dist/components/bordered-box.d.ts +52 -0
  64. package/packages/tallow-tui/dist/components/bordered-box.d.ts.map +1 -0
  65. package/packages/tallow-tui/dist/components/bordered-box.js +89 -0
  66. package/packages/tallow-tui/dist/components/bordered-box.js.map +1 -0
  67. package/packages/tallow-tui/dist/components/box.d.ts +22 -0
  68. package/packages/tallow-tui/dist/components/box.d.ts.map +1 -0
  69. package/packages/tallow-tui/dist/components/box.js +104 -0
  70. package/packages/tallow-tui/dist/components/box.js.map +1 -0
  71. package/packages/tallow-tui/dist/components/cancellable-loader.d.ts +22 -0
  72. package/packages/tallow-tui/dist/components/cancellable-loader.d.ts.map +1 -0
  73. package/packages/tallow-tui/dist/components/cancellable-loader.js +35 -0
  74. package/packages/tallow-tui/dist/components/cancellable-loader.js.map +1 -0
  75. package/packages/tallow-tui/dist/components/editor.d.ts +240 -0
  76. package/packages/tallow-tui/dist/components/editor.d.ts.map +1 -0
  77. package/packages/tallow-tui/dist/components/editor.js +1766 -0
  78. package/packages/tallow-tui/dist/components/editor.js.map +1 -0
  79. package/packages/tallow-tui/dist/components/image.d.ts +126 -0
  80. package/packages/tallow-tui/dist/components/image.d.ts.map +1 -0
  81. package/packages/tallow-tui/dist/components/image.js +245 -0
  82. package/packages/tallow-tui/dist/components/image.js.map +1 -0
  83. package/packages/tallow-tui/dist/components/input.d.ts +37 -0
  84. package/packages/tallow-tui/dist/components/input.d.ts.map +1 -0
  85. package/packages/tallow-tui/dist/components/input.js +439 -0
  86. package/packages/tallow-tui/dist/components/input.js.map +1 -0
  87. package/packages/tallow-tui/dist/components/loader.d.ts +88 -0
  88. package/packages/tallow-tui/dist/components/loader.d.ts.map +1 -0
  89. package/packages/tallow-tui/dist/components/loader.js +146 -0
  90. package/packages/tallow-tui/dist/components/loader.js.map +1 -0
  91. package/packages/tallow-tui/dist/components/markdown.d.ts +95 -0
  92. package/packages/tallow-tui/dist/components/markdown.d.ts.map +1 -0
  93. package/packages/tallow-tui/dist/components/markdown.js +633 -0
  94. package/packages/tallow-tui/dist/components/markdown.js.map +1 -0
  95. package/packages/tallow-tui/dist/components/select-list.d.ts +32 -0
  96. package/packages/tallow-tui/dist/components/select-list.d.ts.map +1 -0
  97. package/packages/tallow-tui/dist/components/select-list.js +156 -0
  98. package/packages/tallow-tui/dist/components/select-list.js.map +1 -0
  99. package/packages/tallow-tui/dist/components/settings-list.d.ts +50 -0
  100. package/packages/tallow-tui/dist/components/settings-list.d.ts.map +1 -0
  101. package/packages/tallow-tui/dist/components/settings-list.js +189 -0
  102. package/packages/tallow-tui/dist/components/settings-list.js.map +1 -0
  103. package/packages/tallow-tui/dist/components/spacer.d.ts +12 -0
  104. package/packages/tallow-tui/dist/components/spacer.d.ts.map +1 -0
  105. package/packages/tallow-tui/dist/components/spacer.js +23 -0
  106. package/packages/tallow-tui/dist/components/spacer.js.map +1 -0
  107. package/packages/tallow-tui/dist/components/text.d.ts +19 -0
  108. package/packages/tallow-tui/dist/components/text.d.ts.map +1 -0
  109. package/packages/tallow-tui/dist/components/text.js +91 -0
  110. package/packages/tallow-tui/dist/components/text.js.map +1 -0
  111. package/packages/tallow-tui/dist/components/truncated-text.d.ts +13 -0
  112. package/packages/tallow-tui/dist/components/truncated-text.d.ts.map +1 -0
  113. package/packages/tallow-tui/dist/components/truncated-text.js +51 -0
  114. package/packages/tallow-tui/dist/components/truncated-text.js.map +1 -0
  115. package/packages/tallow-tui/dist/editor-component.d.ts +50 -0
  116. package/packages/tallow-tui/dist/editor-component.d.ts.map +1 -0
  117. package/packages/tallow-tui/dist/editor-component.js +2 -0
  118. package/packages/tallow-tui/dist/editor-component.js.map +1 -0
  119. package/packages/tallow-tui/dist/fuzzy.d.ts +16 -0
  120. package/packages/tallow-tui/dist/fuzzy.d.ts.map +1 -0
  121. package/packages/tallow-tui/dist/fuzzy.js +107 -0
  122. package/packages/tallow-tui/dist/fuzzy.js.map +1 -0
  123. package/packages/tallow-tui/dist/index.d.ts +25 -0
  124. package/packages/tallow-tui/dist/index.d.ts.map +1 -0
  125. package/packages/tallow-tui/dist/index.js +35 -0
  126. package/packages/tallow-tui/dist/index.js.map +1 -0
  127. package/packages/tallow-tui/dist/keybindings.d.ts +39 -0
  128. package/packages/tallow-tui/dist/keybindings.d.ts.map +1 -0
  129. package/packages/tallow-tui/dist/keybindings.js +114 -0
  130. package/packages/tallow-tui/dist/keybindings.js.map +1 -0
  131. package/packages/tallow-tui/dist/keys.d.ts +168 -0
  132. package/packages/tallow-tui/dist/keys.d.ts.map +1 -0
  133. package/packages/tallow-tui/dist/keys.js +971 -0
  134. package/packages/tallow-tui/dist/keys.js.map +1 -0
  135. package/packages/tallow-tui/dist/kill-ring.d.ts +28 -0
  136. package/packages/tallow-tui/dist/kill-ring.d.ts.map +1 -0
  137. package/packages/tallow-tui/dist/kill-ring.js +44 -0
  138. package/packages/tallow-tui/dist/kill-ring.js.map +1 -0
  139. package/packages/tallow-tui/dist/stdin-buffer.d.ts +48 -0
  140. package/packages/tallow-tui/dist/stdin-buffer.d.ts.map +1 -0
  141. package/packages/tallow-tui/dist/stdin-buffer.js +317 -0
  142. package/packages/tallow-tui/dist/stdin-buffer.js.map +1 -0
  143. package/packages/tallow-tui/dist/terminal-image.d.ts +161 -0
  144. package/packages/tallow-tui/dist/terminal-image.d.ts.map +1 -0
  145. package/packages/tallow-tui/dist/terminal-image.js +460 -0
  146. package/packages/tallow-tui/dist/terminal-image.js.map +1 -0
  147. package/packages/tallow-tui/dist/terminal.d.ts +102 -0
  148. package/packages/tallow-tui/dist/terminal.d.ts.map +1 -0
  149. package/packages/tallow-tui/dist/terminal.js +263 -0
  150. package/packages/tallow-tui/dist/terminal.js.map +1 -0
  151. package/packages/tallow-tui/dist/test-utils/capability-env.d.ts +14 -0
  152. package/packages/tallow-tui/dist/test-utils/capability-env.d.ts.map +1 -0
  153. package/packages/tallow-tui/dist/test-utils/capability-env.js +55 -0
  154. package/packages/tallow-tui/dist/test-utils/capability-env.js.map +1 -0
  155. package/packages/tallow-tui/dist/tui.d.ts +239 -0
  156. package/packages/tallow-tui/dist/tui.d.ts.map +1 -0
  157. package/packages/tallow-tui/dist/tui.js +1058 -0
  158. package/packages/tallow-tui/dist/tui.js.map +1 -0
  159. package/packages/tallow-tui/dist/undo-stack.d.ts +17 -0
  160. package/packages/tallow-tui/dist/undo-stack.d.ts.map +1 -0
  161. package/packages/tallow-tui/dist/undo-stack.js +25 -0
  162. package/packages/tallow-tui/dist/undo-stack.js.map +1 -0
  163. package/packages/tallow-tui/dist/utils.d.ts +96 -0
  164. package/packages/tallow-tui/dist/utils.d.ts.map +1 -0
  165. package/packages/tallow-tui/dist/utils.js +843 -0
  166. package/packages/tallow-tui/dist/utils.js.map +1 -0
  167. package/packages/tallow-tui/package.json +24 -0
  168. package/packages/tallow-tui/src/__tests__/__snapshots__/render.test.ts.snap +121 -0
  169. package/packages/tallow-tui/src/__tests__/editor-border.test.ts +72 -0
  170. package/packages/tallow-tui/src/__tests__/editor-change-listener.test.ts +121 -0
  171. package/packages/tallow-tui/src/__tests__/editor-ghost-text.test.ts +112 -0
  172. package/packages/tallow-tui/src/__tests__/fuzzy.test.ts +91 -0
  173. package/packages/tallow-tui/src/__tests__/image-component.test.ts +113 -0
  174. package/packages/tallow-tui/src/__tests__/keys.test.ts +141 -0
  175. package/packages/tallow-tui/src/__tests__/render.test.ts +179 -0
  176. package/packages/tallow-tui/src/__tests__/stdin-buffer.test.ts +82 -0
  177. package/packages/tallow-tui/src/__tests__/terminal-image.test.ts +363 -0
  178. package/packages/tallow-tui/src/__tests__/tui-diff-regression.test.ts +454 -0
  179. package/packages/tallow-tui/src/__tests__/tui-render-scheduling.test.ts +256 -0
  180. package/packages/tallow-tui/src/__tests__/utils.test.ts +259 -0
  181. package/packages/tallow-tui/src/autocomplete.ts +716 -0
  182. package/packages/tallow-tui/src/border-styles.ts +60 -0
  183. package/packages/tallow-tui/src/components/bordered-box.ts +113 -0
  184. package/packages/tallow-tui/src/components/box.ts +137 -0
  185. package/packages/tallow-tui/src/components/cancellable-loader.ts +40 -0
  186. package/packages/tallow-tui/src/components/editor.ts +2143 -0
  187. package/packages/tallow-tui/src/components/image.ts +315 -0
  188. package/packages/tallow-tui/src/components/input.ts +522 -0
  189. package/packages/tallow-tui/src/components/loader.ts +187 -0
  190. package/packages/tallow-tui/src/components/markdown.ts +780 -0
  191. package/packages/tallow-tui/src/components/select-list.ts +197 -0
  192. package/packages/tallow-tui/src/components/settings-list.ts +264 -0
  193. package/packages/tallow-tui/src/components/spacer.ts +28 -0
  194. package/packages/tallow-tui/src/components/text.ts +113 -0
  195. package/packages/tallow-tui/src/components/truncated-text.ts +65 -0
  196. package/packages/tallow-tui/src/editor-component.ts +92 -0
  197. package/packages/tallow-tui/src/fuzzy.ts +133 -0
  198. package/packages/tallow-tui/src/index.ts +118 -0
  199. package/packages/tallow-tui/src/keybindings.ts +183 -0
  200. package/packages/tallow-tui/src/keys.ts +1189 -0
  201. package/packages/tallow-tui/src/kill-ring.ts +46 -0
  202. package/packages/tallow-tui/src/stdin-buffer.ts +386 -0
  203. package/packages/tallow-tui/src/terminal-image.ts +619 -0
  204. package/packages/tallow-tui/src/terminal.ts +350 -0
  205. package/packages/tallow-tui/src/test-utils/capability-env.ts +56 -0
  206. package/packages/tallow-tui/src/tui.ts +1336 -0
  207. package/packages/tallow-tui/src/undo-stack.ts +28 -0
  208. package/packages/tallow-tui/src/utils.ts +948 -0
  209. package/packages/tallow-tui/tsconfig.build.json +21 -0
  210. package/runtime/agent-runner.ts +20 -0
  211. package/runtime/atomic-write.ts +8 -0
  212. package/runtime/otel.ts +12 -0
  213. package/runtime/resolve-module.ts +23 -0
  214. package/runtime/runtime-path-provider.ts +12 -0
  215. package/runtime/runtime-provenance.ts +17 -0
  216. package/runtime/workspace-transition-relay.ts +21 -0
  217. package/runtime/workspace-transition.ts +29 -0
@@ -0,0 +1,1336 @@
1
+ /**
2
+ * Minimal TUI implementation with differential rendering
3
+ */
4
+
5
+ import * as fs from "node:fs";
6
+ import * as os from "node:os";
7
+ import * as path from "node:path";
8
+ import { isKeyRelease, matchesKey } from "./keys.js";
9
+ import type { Terminal } from "./terminal.js";
10
+ import { getCapabilities, isImageLine, setCellDimensions } from "./terminal-image.js";
11
+ import {
12
+ extractSegments,
13
+ sliceByColumn,
14
+ sliceWithWidth,
15
+ truncateToWidth,
16
+ visibleWidth,
17
+ } from "./utils.js";
18
+
19
+ /**
20
+ * Component interface - all components must implement this
21
+ */
22
+ export interface Component {
23
+ /**
24
+ * Render the component to lines for the given viewport width
25
+ * @param width - Current viewport width
26
+ * @returns Array of strings, each representing a line
27
+ */
28
+ render(width: number): string[];
29
+
30
+ /**
31
+ * Optional handler for keyboard input when component has focus
32
+ */
33
+ handleInput?(data: string): void;
34
+
35
+ /**
36
+ * If true, component receives key release events (Kitty protocol).
37
+ * Default is false - release events are filtered out.
38
+ */
39
+ wantsKeyRelease?: boolean;
40
+
41
+ /**
42
+ * Invalidate any cached rendering state.
43
+ * Called when theme changes or when component needs to re-render from scratch.
44
+ */
45
+ invalidate(): void;
46
+ }
47
+
48
+ /**
49
+ * Interface for components that can receive focus and display a hardware cursor.
50
+ * When focused, the component should emit CURSOR_MARKER at the cursor position
51
+ * in its render output. TUI will find this marker and position the hardware
52
+ * cursor there for proper IME candidate window positioning.
53
+ */
54
+ export interface Focusable {
55
+ /** Set by TUI when focus changes. Component should emit CURSOR_MARKER when true. */
56
+ focused: boolean;
57
+ }
58
+
59
+ /** Type guard to check if a component implements Focusable */
60
+ export function isFocusable(component: Component | null): component is Component & Focusable {
61
+ return component !== null && "focused" in component;
62
+ }
63
+
64
+ /**
65
+ * Cursor position marker - APC (Application Program Command) sequence.
66
+ * This is a zero-width escape sequence that terminals ignore.
67
+ * Components emit this at the cursor position when focused.
68
+ * TUI finds and strips this marker, then positions the hardware cursor there.
69
+ */
70
+ export const CURSOR_MARKER = "\x1b_pi:c\x07";
71
+
72
+ export { visibleWidth };
73
+
74
+ /**
75
+ * Anchor position for overlays
76
+ */
77
+ export type OverlayAnchor =
78
+ | "center"
79
+ | "top-left"
80
+ | "top-right"
81
+ | "bottom-left"
82
+ | "bottom-right"
83
+ | "top-center"
84
+ | "bottom-center"
85
+ | "left-center"
86
+ | "right-center";
87
+
88
+ /**
89
+ * Margin configuration for overlays
90
+ */
91
+ export interface OverlayMargin {
92
+ top?: number;
93
+ right?: number;
94
+ bottom?: number;
95
+ left?: number;
96
+ }
97
+
98
+ /** Value that can be absolute (number) or percentage (string like "50%") */
99
+ export type SizeValue = number | `${number}%`;
100
+
101
+ /** Parse a SizeValue into absolute value given a reference size */
102
+ function parseSizeValue(value: SizeValue | undefined, referenceSize: number): number | undefined {
103
+ if (value === undefined) return undefined;
104
+ if (typeof value === "number") return value;
105
+ // Parse percentage string like "50%"
106
+ const match = value.match(/^(\d+(?:\.\d+)?)%$/);
107
+ if (match) {
108
+ return Math.floor((referenceSize * parseFloat(match[1])) / 100);
109
+ }
110
+ return undefined;
111
+ }
112
+
113
+ /**
114
+ * Options for overlay positioning and sizing.
115
+ * Values can be absolute numbers or percentage strings (e.g., "50%").
116
+ */
117
+ export interface OverlayOptions {
118
+ // === Sizing ===
119
+ /** Width in columns, or percentage of terminal width (e.g., "50%") */
120
+ width?: SizeValue;
121
+ /** Minimum width in columns */
122
+ minWidth?: number;
123
+ /** Maximum height in rows, or percentage of terminal height (e.g., "50%") */
124
+ maxHeight?: SizeValue;
125
+
126
+ // === Positioning - anchor-based ===
127
+ /** Anchor point for positioning (default: 'center') */
128
+ anchor?: OverlayAnchor;
129
+ /** Horizontal offset from anchor position (positive = right) */
130
+ offsetX?: number;
131
+ /** Vertical offset from anchor position (positive = down) */
132
+ offsetY?: number;
133
+
134
+ // === Positioning - percentage or absolute ===
135
+ /** Row position: absolute number, or percentage (e.g., "25%" = 25% from top) */
136
+ row?: SizeValue;
137
+ /** Column position: absolute number, or percentage (e.g., "50%" = centered horizontally) */
138
+ col?: SizeValue;
139
+
140
+ // === Margin from terminal edges ===
141
+ /** Margin from terminal edges. Number applies to all sides. */
142
+ margin?: OverlayMargin | number;
143
+
144
+ // === Visibility ===
145
+ /**
146
+ * Control overlay visibility based on terminal dimensions.
147
+ * If provided, overlay is only rendered when this returns true.
148
+ * Called each render cycle with current terminal dimensions.
149
+ */
150
+ visible?: (termWidth: number, termHeight: number) => boolean;
151
+ }
152
+
153
+ /**
154
+ * Handle returned by showOverlay for controlling the overlay
155
+ */
156
+ export interface OverlayHandle {
157
+ /** Permanently remove the overlay (cannot be shown again) */
158
+ hide(): void;
159
+ /** Temporarily hide or show the overlay */
160
+ setHidden(hidden: boolean): void;
161
+ /** Check if overlay is temporarily hidden */
162
+ isHidden(): boolean;
163
+ }
164
+
165
+ /**
166
+ * Container - a component that contains other components
167
+ */
168
+ export class Container implements Component {
169
+ children: Component[] = [];
170
+
171
+ addChild(component: Component): void {
172
+ this.children.push(component);
173
+ }
174
+
175
+ removeChild(component: Component): void {
176
+ const index = this.children.indexOf(component);
177
+ if (index !== -1) {
178
+ this.children.splice(index, 1);
179
+ }
180
+ }
181
+
182
+ clear(): void {
183
+ this.children = [];
184
+ }
185
+
186
+ invalidate(): void {
187
+ for (const child of this.children) {
188
+ child.invalidate?.();
189
+ }
190
+ }
191
+
192
+ render(width: number): string[] {
193
+ const lines: string[] = [];
194
+ for (const child of this.children) {
195
+ lines.push(...child.render(width));
196
+ }
197
+ return lines;
198
+ }
199
+ }
200
+
201
+ /**
202
+ * TUI - Main class for managing terminal UI with differential rendering
203
+ */
204
+ export class TUI extends Container {
205
+ public terminal: Terminal;
206
+ private previousLines: string[] = [];
207
+ private previousWidth = 0;
208
+ private focusedComponent: Component | null = null;
209
+
210
+ /** Global callback for debug key (Shift+Ctrl+D). Called before input is forwarded to focused component. */
211
+ public onDebug?: () => void;
212
+ private renderRequested = false;
213
+ private pendingRenderHandle?: ReturnType<typeof setTimeout>;
214
+ private cursorRow = 0; // Logical cursor row (end of rendered content)
215
+ private hardwareCursorRow = 0; // Actual terminal cursor row (may differ due to IME positioning)
216
+ private inputBuffer = ""; // Buffer for parsing terminal responses
217
+ private cellSizeQueryPending = false;
218
+ private showHardwareCursor = process.env.PI_HARDWARE_CURSOR === "1";
219
+ private clearOnShrink = process.env.PI_CLEAR_ON_SHRINK === "1"; // Clear empty rows when content shrinks (default: off)
220
+ private maxLinesRendered = 0; // Track terminal's working area (max lines ever rendered)
221
+ private previousViewportTop = 0; // Track previous viewport top for resize-aware cursor moves
222
+ private fullRedrawCount = 0;
223
+ private stopped = false;
224
+
225
+ // Overlay stack for modal components rendered on top of base content
226
+ private overlayStack: {
227
+ component: Component;
228
+ options?: OverlayOptions;
229
+ preFocus: Component | null;
230
+ hidden: boolean;
231
+ }[] = [];
232
+
233
+ constructor(terminal: Terminal, showHardwareCursor?: boolean) {
234
+ super();
235
+ this.terminal = terminal;
236
+ if (showHardwareCursor !== undefined) {
237
+ this.showHardwareCursor = showHardwareCursor;
238
+ }
239
+ }
240
+
241
+ get fullRedraws(): number {
242
+ return this.fullRedrawCount;
243
+ }
244
+
245
+ getShowHardwareCursor(): boolean {
246
+ return this.showHardwareCursor;
247
+ }
248
+
249
+ setShowHardwareCursor(enabled: boolean): void {
250
+ if (this.showHardwareCursor === enabled) return;
251
+ this.showHardwareCursor = enabled;
252
+ if (!enabled) {
253
+ this.terminal.hideCursor();
254
+ }
255
+ this.requestRender();
256
+ }
257
+
258
+ getClearOnShrink(): boolean {
259
+ return this.clearOnShrink;
260
+ }
261
+
262
+ /**
263
+ * Set whether to trigger full re-render when content shrinks.
264
+ * When true (default), empty rows are cleared when content shrinks.
265
+ * When false, empty rows remain (reduces redraws on slower terminals).
266
+ */
267
+ setClearOnShrink(enabled: boolean): void {
268
+ this.clearOnShrink = enabled;
269
+ }
270
+
271
+ setFocus(component: Component | null): void {
272
+ // Clear focused flag on old component
273
+ if (isFocusable(this.focusedComponent)) {
274
+ this.focusedComponent.focused = false;
275
+ }
276
+
277
+ this.focusedComponent = component;
278
+
279
+ // Set focused flag on new component
280
+ if (isFocusable(component)) {
281
+ component.focused = true;
282
+ }
283
+ }
284
+
285
+ /**
286
+ * Show an overlay component with configurable positioning and sizing.
287
+ * Returns a handle to control the overlay's visibility.
288
+ */
289
+ showOverlay(component: Component, options?: OverlayOptions): OverlayHandle {
290
+ const entry = { component, options, preFocus: this.focusedComponent, hidden: false };
291
+ this.overlayStack.push(entry);
292
+ // Only focus if overlay is actually visible
293
+ if (this.isOverlayVisible(entry)) {
294
+ this.setFocus(component);
295
+ }
296
+ this.terminal.hideCursor();
297
+ this.requestRender();
298
+
299
+ // Return handle for controlling this overlay
300
+ return {
301
+ hide: () => {
302
+ const index = this.overlayStack.indexOf(entry);
303
+ if (index !== -1) {
304
+ this.overlayStack.splice(index, 1);
305
+ // Restore focus if this overlay had focus
306
+ if (this.focusedComponent === component) {
307
+ const topVisible = this.getTopmostVisibleOverlay();
308
+ this.setFocus(topVisible?.component ?? entry.preFocus);
309
+ }
310
+ if (this.overlayStack.length === 0) this.terminal.hideCursor();
311
+ this.requestRender();
312
+ }
313
+ },
314
+ setHidden: (hidden: boolean) => {
315
+ if (entry.hidden === hidden) return;
316
+ entry.hidden = hidden;
317
+ // Update focus when hiding/showing
318
+ if (hidden) {
319
+ // If this overlay had focus, move focus to next visible or preFocus
320
+ if (this.focusedComponent === component) {
321
+ const topVisible = this.getTopmostVisibleOverlay();
322
+ this.setFocus(topVisible?.component ?? entry.preFocus);
323
+ }
324
+ } else {
325
+ // Restore focus to this overlay when showing (if it's actually visible)
326
+ if (this.isOverlayVisible(entry)) {
327
+ this.setFocus(component);
328
+ }
329
+ }
330
+ this.requestRender();
331
+ },
332
+ isHidden: () => entry.hidden,
333
+ };
334
+ }
335
+
336
+ /** Hide the topmost overlay and restore previous focus. */
337
+ hideOverlay(): void {
338
+ const overlay = this.overlayStack.pop();
339
+ if (!overlay) return;
340
+ // Find topmost visible overlay, or fall back to preFocus
341
+ const topVisible = this.getTopmostVisibleOverlay();
342
+ this.setFocus(topVisible?.component ?? overlay.preFocus);
343
+ if (this.overlayStack.length === 0) this.terminal.hideCursor();
344
+ this.requestRender();
345
+ }
346
+
347
+ /** Check if there are any visible overlays */
348
+ hasOverlay(): boolean {
349
+ return this.overlayStack.some((o) => this.isOverlayVisible(o));
350
+ }
351
+
352
+ /** Check if an overlay entry is currently visible */
353
+ private isOverlayVisible(entry: (typeof this.overlayStack)[number]): boolean {
354
+ if (entry.hidden) return false;
355
+ if (entry.options?.visible) {
356
+ return entry.options.visible(this.terminal.columns, this.terminal.rows);
357
+ }
358
+ return true;
359
+ }
360
+
361
+ /** Find the topmost visible overlay, if any */
362
+ private getTopmostVisibleOverlay(): (typeof this.overlayStack)[number] | undefined {
363
+ for (let i = this.overlayStack.length - 1; i >= 0; i--) {
364
+ if (this.isOverlayVisible(this.overlayStack[i])) {
365
+ return this.overlayStack[i];
366
+ }
367
+ }
368
+ return undefined;
369
+ }
370
+
371
+ override invalidate(): void {
372
+ super.invalidate();
373
+ for (const overlay of this.overlayStack) overlay.component.invalidate?.();
374
+ }
375
+
376
+ start(): void {
377
+ this.stopped = false;
378
+ this.terminal.start(
379
+ (data) => this.handleInput(data),
380
+ () => this.requestRender()
381
+ );
382
+ this.terminal.hideCursor();
383
+ this.queryCellSize();
384
+ this.requestRender();
385
+ }
386
+
387
+ private queryCellSize(): void {
388
+ // Only query if terminal supports images (cell size is only used for image rendering)
389
+ if (!getCapabilities().images) {
390
+ return;
391
+ }
392
+ // Query terminal for cell size in pixels: CSI 16 t
393
+ // Response format: CSI 6 ; height ; width t
394
+ this.cellSizeQueryPending = true;
395
+ this.terminal.write("\x1b[16t");
396
+ }
397
+
398
+ stop(): void {
399
+ this.stopped = true;
400
+ if (this.pendingRenderHandle !== undefined) {
401
+ clearTimeout(this.pendingRenderHandle);
402
+ this.pendingRenderHandle = undefined;
403
+ this.renderRequested = false;
404
+ }
405
+ // Move cursor to the end of the content to prevent overwriting/artifacts on exit
406
+ if (this.previousLines.length > 0) {
407
+ const targetRow = this.previousLines.length; // Line after the last content
408
+ const lineDiff = targetRow - this.hardwareCursorRow;
409
+ if (lineDiff > 0) {
410
+ this.terminal.write(`\x1b[${lineDiff}B`);
411
+ } else if (lineDiff < 0) {
412
+ this.terminal.write(`\x1b[${-lineDiff}A`);
413
+ }
414
+ this.terminal.write("\r\n");
415
+ }
416
+
417
+ this.terminal.showCursor();
418
+ this.terminal.stop();
419
+ }
420
+
421
+ requestRender(force = false): void {
422
+ if (force) {
423
+ this.previousLines = [];
424
+ this.previousWidth = -1; // -1 triggers widthChanged, forcing a full clear
425
+ this.cursorRow = 0;
426
+ this.hardwareCursorRow = 0;
427
+ this.maxLinesRendered = 0;
428
+ this.previousViewportTop = 0;
429
+ }
430
+ if (this.renderRequested) return;
431
+ this.scheduleRender();
432
+ }
433
+
434
+ /**
435
+ * Schedule a single coalesced render in the check phase.
436
+ *
437
+ * On Bun, `setImmediate` behaves like a microtask and never enters the I/O poll
438
+ * phase, so stdin data callbacks are starved during streaming. `setTimeout(fn, 0)`
439
+ * forces a real timer (~1ms) that guarantees I/O polling between renders.
440
+ *
441
+ * On Node.js, `setTimeout(0)` has a 1ms minimum delay — slightly slower than
442
+ * `setImmediate` but still imperceptible (<13ms human threshold).
443
+ *
444
+ * @see Plan 177 — Bun setImmediate does not yield to I/O
445
+ * @returns {void}
446
+ */
447
+ private scheduleRender(): void {
448
+ this.renderRequested = true;
449
+ this.pendingRenderHandle = setTimeout(() => {
450
+ this.pendingRenderHandle = undefined;
451
+ this.renderRequested = false;
452
+ if (this.stopped) return;
453
+ this.doRender();
454
+ }, 0);
455
+ }
456
+
457
+ /** Input listener functions — called before the focused component receives input. */
458
+ private inputListeners = new Set<
459
+ (data: string) => { consume?: boolean; data?: string } | undefined
460
+ >();
461
+
462
+ /**
463
+ * Register an input listener. Listeners run before the focused component and can
464
+ * consume input (return `{consume: true}`) or transform it (return `{data: newData}`).
465
+ *
466
+ * @param listener - Listener function
467
+ * @returns Unsubscribe function
468
+ */
469
+ addInputListener(
470
+ listener: (data: string) => { consume?: boolean; data?: string } | undefined
471
+ ): () => void {
472
+ this.inputListeners.add(listener);
473
+ return () => {
474
+ this.inputListeners.delete(listener);
475
+ };
476
+ }
477
+
478
+ /**
479
+ * Remove a previously registered input listener.
480
+ *
481
+ * @param listener - The listener function to remove
482
+ */
483
+ removeInputListener(
484
+ listener: (data: string) => { consume?: boolean; data?: string } | undefined
485
+ ): void {
486
+ this.inputListeners.delete(listener);
487
+ }
488
+
489
+ private handleInput(data: string): void {
490
+ // If we're waiting for cell size response, buffer input and parse
491
+ if (this.cellSizeQueryPending) {
492
+ this.inputBuffer += data;
493
+ const filtered = this.parseCellSizeResponse();
494
+ if (filtered.length === 0) return;
495
+ data = filtered;
496
+ }
497
+
498
+ // Global debug key handler (Shift+Ctrl+D)
499
+ if (matchesKey(data, "shift+ctrl+d") && this.onDebug) {
500
+ this.onDebug();
501
+ return;
502
+ }
503
+
504
+ // If focused component is an overlay, verify it's still visible
505
+ // (visibility can change due to terminal resize or visible() callback)
506
+ const focusedOverlay = this.overlayStack.find((o) => o.component === this.focusedComponent);
507
+ if (focusedOverlay && !this.isOverlayVisible(focusedOverlay)) {
508
+ // Focused overlay is no longer visible, redirect to topmost visible overlay
509
+ const topVisible = this.getTopmostVisibleOverlay();
510
+ if (topVisible) {
511
+ this.setFocus(topVisible.component);
512
+ } else {
513
+ // No visible overlays, restore to preFocus
514
+ this.setFocus(focusedOverlay.preFocus);
515
+ }
516
+ }
517
+
518
+ // Run input listeners — can consume or transform input
519
+ if (this.inputListeners.size > 0) {
520
+ let current = data;
521
+ for (const listener of this.inputListeners) {
522
+ const result = listener(current);
523
+ if (result?.consume) {
524
+ return;
525
+ }
526
+ if (result?.data !== undefined) {
527
+ current = result.data;
528
+ }
529
+ }
530
+ if (current.length === 0) {
531
+ return;
532
+ }
533
+ data = current;
534
+ }
535
+
536
+ // Pass input to focused component (including Ctrl+C)
537
+ // The focused component can decide how to handle Ctrl+C
538
+ if (this.focusedComponent?.handleInput) {
539
+ // Filter out key release events unless component opts in
540
+ if (isKeyRelease(data) && !this.focusedComponent.wantsKeyRelease) {
541
+ return;
542
+ }
543
+ this.focusedComponent.handleInput(data);
544
+ this.requestRender();
545
+ }
546
+ }
547
+
548
+ private parseCellSizeResponse(): string {
549
+ // Response format: ESC [ 6 ; height ; width t
550
+ // Match the response pattern
551
+ const responsePattern = /\x1b\[6;(\d+);(\d+)t/;
552
+ const match = this.inputBuffer.match(responsePattern);
553
+
554
+ if (match) {
555
+ const heightPx = parseInt(match[1], 10);
556
+ const widthPx = parseInt(match[2], 10);
557
+
558
+ if (heightPx > 0 && widthPx > 0) {
559
+ setCellDimensions({ widthPx, heightPx });
560
+ // Invalidate all components so images re-render with correct dimensions
561
+ this.invalidate();
562
+ this.requestRender();
563
+ }
564
+
565
+ // Remove the response from buffer
566
+ this.inputBuffer = this.inputBuffer.replace(responsePattern, "");
567
+ this.cellSizeQueryPending = false;
568
+ }
569
+
570
+ // Check if we have a partial cell size response starting (wait for more data)
571
+ // Patterns that could be incomplete cell size response: \x1b, \x1b[, \x1b[6, \x1b[6;...(no t yet)
572
+ const partialCellSizePattern = /\x1b(\[6?;?[\d;]*)?$/;
573
+ if (partialCellSizePattern.test(this.inputBuffer)) {
574
+ // Check if it's actually a complete different escape sequence (ends with a letter)
575
+ // Cell size response ends with 't', Kitty keyboard ends with 'u', arrows end with A-D, etc.
576
+ const lastChar = this.inputBuffer[this.inputBuffer.length - 1];
577
+ if (!/[a-zA-Z~]/.test(lastChar)) {
578
+ // Doesn't end with a terminator, might be incomplete - wait for more
579
+ return "";
580
+ }
581
+ }
582
+
583
+ // No cell size response found, return buffered data as user input
584
+ const result = this.inputBuffer;
585
+ this.inputBuffer = "";
586
+ this.cellSizeQueryPending = false; // Give up waiting
587
+ return result;
588
+ }
589
+
590
+ /**
591
+ * Resolve overlay layout from options.
592
+ * Returns { width, row, col, maxHeight } for rendering.
593
+ */
594
+ private resolveOverlayLayout(
595
+ options: OverlayOptions | undefined,
596
+ overlayHeight: number,
597
+ termWidth: number,
598
+ termHeight: number
599
+ ): { width: number; row: number; col: number; maxHeight: number | undefined } {
600
+ const opt = options ?? {};
601
+
602
+ // Parse margin (clamp to non-negative)
603
+ const margin =
604
+ typeof opt.margin === "number"
605
+ ? { top: opt.margin, right: opt.margin, bottom: opt.margin, left: opt.margin }
606
+ : (opt.margin ?? {});
607
+ const marginTop = Math.max(0, margin.top ?? 0);
608
+ const marginRight = Math.max(0, margin.right ?? 0);
609
+ const marginBottom = Math.max(0, margin.bottom ?? 0);
610
+ const marginLeft = Math.max(0, margin.left ?? 0);
611
+
612
+ // Available space after margins
613
+ const availWidth = Math.max(1, termWidth - marginLeft - marginRight);
614
+ const availHeight = Math.max(1, termHeight - marginTop - marginBottom);
615
+
616
+ // === Resolve width ===
617
+ let width = parseSizeValue(opt.width, termWidth) ?? Math.min(80, availWidth);
618
+ // Apply minWidth
619
+ if (opt.minWidth !== undefined) {
620
+ width = Math.max(width, opt.minWidth);
621
+ }
622
+ // Clamp to available space
623
+ width = Math.max(1, Math.min(width, availWidth));
624
+
625
+ // === Resolve maxHeight ===
626
+ let maxHeight = parseSizeValue(opt.maxHeight, termHeight);
627
+ // Clamp to available space
628
+ if (maxHeight !== undefined) {
629
+ maxHeight = Math.max(1, Math.min(maxHeight, availHeight));
630
+ }
631
+
632
+ // Effective overlay height (may be clamped by maxHeight)
633
+ const effectiveHeight =
634
+ maxHeight !== undefined ? Math.min(overlayHeight, maxHeight) : overlayHeight;
635
+
636
+ // === Resolve position ===
637
+ let row: number;
638
+ let col: number;
639
+
640
+ if (opt.row !== undefined) {
641
+ if (typeof opt.row === "string") {
642
+ // Percentage: 0% = top, 100% = bottom (overlay stays within bounds)
643
+ const match = opt.row.match(/^(\d+(?:\.\d+)?)%$/);
644
+ if (match) {
645
+ const maxRow = Math.max(0, availHeight - effectiveHeight);
646
+ const percent = parseFloat(match[1]) / 100;
647
+ row = marginTop + Math.floor(maxRow * percent);
648
+ } else {
649
+ // Invalid format, fall back to center
650
+ row = this.resolveAnchorRow("center", effectiveHeight, availHeight, marginTop);
651
+ }
652
+ } else {
653
+ // Absolute row position
654
+ row = opt.row;
655
+ }
656
+ } else {
657
+ // Anchor-based (default: center)
658
+ const anchor = opt.anchor ?? "center";
659
+ row = this.resolveAnchorRow(anchor, effectiveHeight, availHeight, marginTop);
660
+ }
661
+
662
+ if (opt.col !== undefined) {
663
+ if (typeof opt.col === "string") {
664
+ // Percentage: 0% = left, 100% = right (overlay stays within bounds)
665
+ const match = opt.col.match(/^(\d+(?:\.\d+)?)%$/);
666
+ if (match) {
667
+ const maxCol = Math.max(0, availWidth - width);
668
+ const percent = parseFloat(match[1]) / 100;
669
+ col = marginLeft + Math.floor(maxCol * percent);
670
+ } else {
671
+ // Invalid format, fall back to center
672
+ col = this.resolveAnchorCol("center", width, availWidth, marginLeft);
673
+ }
674
+ } else {
675
+ // Absolute column position
676
+ col = opt.col;
677
+ }
678
+ } else {
679
+ // Anchor-based (default: center)
680
+ const anchor = opt.anchor ?? "center";
681
+ col = this.resolveAnchorCol(anchor, width, availWidth, marginLeft);
682
+ }
683
+
684
+ // Apply offsets
685
+ if (opt.offsetY !== undefined) row += opt.offsetY;
686
+ if (opt.offsetX !== undefined) col += opt.offsetX;
687
+
688
+ // Clamp to terminal bounds (respecting margins)
689
+ row = Math.max(marginTop, Math.min(row, termHeight - marginBottom - effectiveHeight));
690
+ col = Math.max(marginLeft, Math.min(col, termWidth - marginRight - width));
691
+
692
+ return { width, row, col, maxHeight };
693
+ }
694
+
695
+ private resolveAnchorRow(
696
+ anchor: OverlayAnchor,
697
+ height: number,
698
+ availHeight: number,
699
+ marginTop: number
700
+ ): number {
701
+ switch (anchor) {
702
+ case "top-left":
703
+ case "top-center":
704
+ case "top-right":
705
+ return marginTop;
706
+ case "bottom-left":
707
+ case "bottom-center":
708
+ case "bottom-right":
709
+ return marginTop + availHeight - height;
710
+ case "left-center":
711
+ case "center":
712
+ case "right-center":
713
+ return marginTop + Math.floor((availHeight - height) / 2);
714
+ }
715
+ }
716
+
717
+ private resolveAnchorCol(
718
+ anchor: OverlayAnchor,
719
+ width: number,
720
+ availWidth: number,
721
+ marginLeft: number
722
+ ): number {
723
+ switch (anchor) {
724
+ case "top-left":
725
+ case "left-center":
726
+ case "bottom-left":
727
+ return marginLeft;
728
+ case "top-right":
729
+ case "right-center":
730
+ case "bottom-right":
731
+ return marginLeft + availWidth - width;
732
+ case "top-center":
733
+ case "center":
734
+ case "bottom-center":
735
+ return marginLeft + Math.floor((availWidth - width) / 2);
736
+ }
737
+ }
738
+
739
+ /** Composite all overlays into content lines (in stack order, later = on top). */
740
+ private compositeOverlays(lines: string[], termWidth: number, termHeight: number): string[] {
741
+ if (this.overlayStack.length === 0) return lines;
742
+ const result = [...lines];
743
+
744
+ // Pre-render all visible overlays and calculate positions
745
+ const rendered: { overlayLines: string[]; row: number; col: number; w: number }[] = [];
746
+ let minLinesNeeded = result.length;
747
+
748
+ for (const entry of this.overlayStack) {
749
+ // Skip invisible overlays (hidden or visible() returns false)
750
+ if (!this.isOverlayVisible(entry)) continue;
751
+
752
+ const { component, options } = entry;
753
+
754
+ // Get layout with height=0 first to determine width and maxHeight
755
+ // (width and maxHeight don't depend on overlay height)
756
+ const { width, maxHeight } = this.resolveOverlayLayout(options, 0, termWidth, termHeight);
757
+
758
+ // Render component at calculated width
759
+ let overlayLines = component.render(width);
760
+
761
+ // Apply maxHeight if specified
762
+ if (maxHeight !== undefined && overlayLines.length > maxHeight) {
763
+ overlayLines = overlayLines.slice(0, maxHeight);
764
+ }
765
+
766
+ // Get final row/col with actual overlay height
767
+ const { row, col } = this.resolveOverlayLayout(
768
+ options,
769
+ overlayLines.length,
770
+ termWidth,
771
+ termHeight
772
+ );
773
+
774
+ rendered.push({ overlayLines, row, col, w: width });
775
+ minLinesNeeded = Math.max(minLinesNeeded, row + overlayLines.length);
776
+ }
777
+
778
+ // Ensure result covers the terminal working area to keep overlay positioning stable across resizes.
779
+ // maxLinesRendered can exceed current content length after a shrink; pad to keep viewportStart consistent.
780
+ const workingHeight = Math.max(this.maxLinesRendered, minLinesNeeded);
781
+
782
+ // Extend result with empty lines if content is too short for overlay placement or working area
783
+ while (result.length < workingHeight) {
784
+ result.push("");
785
+ }
786
+
787
+ const viewportStart = Math.max(0, workingHeight - termHeight);
788
+
789
+ // Track which lines were modified for final verification
790
+ const modifiedLines = new Set<number>();
791
+
792
+ // Composite each overlay
793
+ for (const { overlayLines, row, col, w } of rendered) {
794
+ for (let i = 0; i < overlayLines.length; i++) {
795
+ const idx = viewportStart + row + i;
796
+ if (idx >= 0 && idx < result.length) {
797
+ // Defensive: truncate overlay line to declared width before compositing
798
+ // (components should already respect width, but this ensures it)
799
+ const truncatedOverlayLine =
800
+ visibleWidth(overlayLines[i]) > w
801
+ ? sliceByColumn(overlayLines[i], 0, w, true)
802
+ : overlayLines[i];
803
+ result[idx] = this.compositeLineAt(result[idx], truncatedOverlayLine, col, w, termWidth);
804
+ modifiedLines.add(idx);
805
+ }
806
+ }
807
+ }
808
+
809
+ // Final verification: ensure no composited line exceeds terminal width
810
+ // This is a belt-and-suspenders safeguard - compositeLineAt should already
811
+ // guarantee this, but we verify here to prevent crashes from any edge cases
812
+ // Only check lines that were actually modified (optimization)
813
+ for (const idx of modifiedLines) {
814
+ const lineWidth = visibleWidth(result[idx]);
815
+ if (lineWidth > termWidth) {
816
+ result[idx] = sliceByColumn(result[idx], 0, termWidth, true);
817
+ }
818
+ }
819
+
820
+ return result;
821
+ }
822
+
823
+ private static readonly SEGMENT_RESET = "\x1b[0m\x1b]8;;\x07";
824
+
825
+ private applyLineResets(lines: string[]): string[] {
826
+ const reset = TUI.SEGMENT_RESET;
827
+ for (let i = 0; i < lines.length; i++) {
828
+ const line = lines[i];
829
+ if (!isImageLine(line)) {
830
+ lines[i] = line + reset;
831
+ }
832
+ }
833
+ return lines;
834
+ }
835
+
836
+ /** Splice overlay content into a base line at a specific column. Single-pass optimized. */
837
+ private compositeLineAt(
838
+ baseLine: string,
839
+ overlayLine: string,
840
+ startCol: number,
841
+ overlayWidth: number,
842
+ totalWidth: number
843
+ ): string {
844
+ if (isImageLine(baseLine)) return baseLine;
845
+
846
+ // Single pass through baseLine extracts both before and after segments
847
+ const afterStart = startCol + overlayWidth;
848
+ const base = extractSegments(baseLine, startCol, afterStart, totalWidth - afterStart, true);
849
+
850
+ // Extract overlay with width tracking (strict=true to exclude wide chars at boundary)
851
+ const overlay = sliceWithWidth(overlayLine, 0, overlayWidth, true);
852
+
853
+ // Pad segments to target widths
854
+ const beforePad = Math.max(0, startCol - base.beforeWidth);
855
+ const overlayPad = Math.max(0, overlayWidth - overlay.width);
856
+ const actualBeforeWidth = Math.max(startCol, base.beforeWidth);
857
+ const actualOverlayWidth = Math.max(overlayWidth, overlay.width);
858
+ const afterTarget = Math.max(0, totalWidth - actualBeforeWidth - actualOverlayWidth);
859
+ const afterPad = Math.max(0, afterTarget - base.afterWidth);
860
+
861
+ // Compose result
862
+ const r = TUI.SEGMENT_RESET;
863
+ const result =
864
+ base.before +
865
+ " ".repeat(beforePad) +
866
+ r +
867
+ overlay.text +
868
+ " ".repeat(overlayPad) +
869
+ r +
870
+ base.after +
871
+ " ".repeat(afterPad);
872
+
873
+ // CRITICAL: Always verify and truncate to terminal width.
874
+ // This is the final safeguard against width overflow which would crash the TUI.
875
+ // Width tracking can drift from actual visible width due to:
876
+ // - Complex ANSI/OSC sequences (hyperlinks, colors)
877
+ // - Wide characters at segment boundaries
878
+ // - Edge cases in segment extraction
879
+ const resultWidth = visibleWidth(result);
880
+ if (resultWidth <= totalWidth) {
881
+ return result;
882
+ }
883
+ // Truncate with strict=true to ensure we don't exceed totalWidth
884
+ return sliceByColumn(result, 0, totalWidth, true);
885
+ }
886
+
887
+ /**
888
+ * Find and extract cursor position from rendered lines.
889
+ * Searches for CURSOR_MARKER, calculates its position, and strips it from the output.
890
+ * Only scans the bottom terminal height lines (visible viewport).
891
+ * @param lines - Rendered lines to search
892
+ * @param height - Terminal height (visible viewport size)
893
+ * @returns Cursor position { row, col } or null if no marker found
894
+ */
895
+ private extractCursorPosition(
896
+ lines: string[],
897
+ height: number
898
+ ): { row: number; col: number } | null {
899
+ // Only scan the bottom `height` lines (visible viewport)
900
+ const viewportTop = Math.max(0, lines.length - height);
901
+ for (let row = lines.length - 1; row >= viewportTop; row--) {
902
+ const line = lines[row];
903
+ const markerIndex = line.indexOf(CURSOR_MARKER);
904
+ if (markerIndex !== -1) {
905
+ // Calculate visual column (width of text before marker)
906
+ const beforeMarker = line.slice(0, markerIndex);
907
+ const col = visibleWidth(beforeMarker);
908
+
909
+ // Strip marker from the line
910
+ lines[row] = line.slice(0, markerIndex) + line.slice(markerIndex + CURSOR_MARKER.length);
911
+
912
+ return { row, col };
913
+ }
914
+ }
915
+ return null;
916
+ }
917
+
918
+ private doRender(): void {
919
+ if (this.stopped) return;
920
+ const width = this.terminal.columns;
921
+ const height = this.terminal.rows;
922
+ let viewportTop = Math.max(0, this.maxLinesRendered - height);
923
+ let prevViewportTop = this.previousViewportTop;
924
+ let hardwareCursorRow = this.hardwareCursorRow;
925
+ const computeLineDiff = (targetRow: number): number => {
926
+ const currentScreenRow = hardwareCursorRow - prevViewportTop;
927
+ const targetScreenRow = targetRow - viewportTop;
928
+ return targetScreenRow - currentScreenRow;
929
+ };
930
+
931
+ // Render all components to get new lines
932
+ let newLines = this.render(width);
933
+
934
+ // Composite overlays into the rendered lines (before differential compare)
935
+ if (this.overlayStack.length > 0) {
936
+ newLines = this.compositeOverlays(newLines, width, height);
937
+ }
938
+
939
+ // Extract cursor position before applying line resets (marker must be found first)
940
+ const cursorPos = this.extractCursorPosition(newLines, height);
941
+
942
+ newLines = this.applyLineResets(newLines);
943
+
944
+ // Width changed - need full re-render (line wrapping changes)
945
+ const widthChanged = this.previousWidth !== 0 && this.previousWidth !== width;
946
+
947
+ // Helper to clear scrollback and viewport and render all new lines
948
+ const fullRender = (clear: boolean): void => {
949
+ this.fullRedrawCount += 1;
950
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
951
+ if (clear) buffer += "\x1b[2J\x1b[H"; // Clear screen and home (preserve scrollback)
952
+ for (let i = 0; i < newLines.length; i++) {
953
+ if (i > 0) buffer += "\r\n";
954
+ buffer += newLines[i];
955
+ }
956
+ buffer += "\x1b[?2026l"; // End synchronized output
957
+ this.terminal.write(buffer);
958
+ this.cursorRow = Math.max(0, newLines.length - 1);
959
+ this.hardwareCursorRow = this.cursorRow;
960
+ // Reset max lines when clearing, otherwise track growth
961
+ if (clear) {
962
+ this.maxLinesRendered = newLines.length;
963
+ } else {
964
+ this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
965
+ }
966
+ this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
967
+ this.positionHardwareCursor(cursorPos, newLines.length);
968
+ this.previousLines = newLines;
969
+ this.previousWidth = width;
970
+ };
971
+
972
+ const debugRedraw = process.env.PI_DEBUG_REDRAW === "1";
973
+ const logRedraw = (reason: string): void => {
974
+ if (!debugRedraw) return;
975
+ const logPath = path.join(os.homedir(), ".pi", "agent", "pi-debug.log");
976
+ const msg = `[${new Date().toISOString()}] fullRender: ${reason} (prev=${this.previousLines.length}, new=${newLines.length}, height=${height})\n`;
977
+ fs.appendFileSync(logPath, msg);
978
+ };
979
+
980
+ // First render - just output everything without clearing (assumes clean screen)
981
+ if (this.previousLines.length === 0 && !widthChanged) {
982
+ logRedraw("first render");
983
+ fullRender(false);
984
+ return;
985
+ }
986
+
987
+ // Width changed - full re-render (line wrapping changes)
988
+ if (widthChanged) {
989
+ logRedraw(`width changed (${this.previousWidth} -> ${width})`);
990
+ fullRender(true);
991
+ return;
992
+ }
993
+
994
+ // Content shrunk below the working area and no overlays - re-render to clear empty rows
995
+ // (overlays need the padding, so only do this when no overlays are active)
996
+ // Configurable via setClearOnShrink() or PI_CLEAR_ON_SHRINK=0 env var
997
+ if (
998
+ this.clearOnShrink &&
999
+ newLines.length < this.maxLinesRendered &&
1000
+ this.overlayStack.length === 0
1001
+ ) {
1002
+ logRedraw(`clearOnShrink (maxLinesRendered=${this.maxLinesRendered})`);
1003
+ fullRender(true);
1004
+ return;
1005
+ }
1006
+
1007
+ // Large content shrinks (e.g., tool output collapse) are hard to partially redraw
1008
+ // correctly — cursor positions drift when many lines disappear at once, causing
1009
+ // ghost copies of previous frames. Force a full redraw when the shrink exceeds a
1010
+ // threshold. More targeted than clearOnShrink (which fires on ANY shrink).
1011
+ const shrinkDelta = this.previousLines.length - newLines.length;
1012
+ if (shrinkDelta > 5 && this.overlayStack.length === 0) {
1013
+ logRedraw(`large shrink (${shrinkDelta} lines)`);
1014
+ fullRender(true);
1015
+ return;
1016
+ }
1017
+
1018
+ const previousContentViewportTop = Math.max(0, this.previousLines.length - height);
1019
+ // Detect viewport basis drift: maxLinesRendered exceeds actual content,
1020
+ // causing viewportTop to be computed from a stale high-water mark.
1021
+ // Compare against newLines.length (not previousLines.length) so the
1022
+ // correction fires on the same render cycle as a shrink, not one cycle late.
1023
+ const hasViewportBasisDrift =
1024
+ this.overlayStack.length === 0 &&
1025
+ this.previousLines.length > 0 &&
1026
+ this.maxLinesRendered > newLines.length &&
1027
+ prevViewportTop !== previousContentViewportTop;
1028
+
1029
+ // After shrink-heavy updates with clearOnShrink disabled, maxLinesRendered can stay larger
1030
+ // than current content. Instead of a destructive full redraw (which clears the screen and
1031
+ // disrupts scrollback), realign the working-area coordinates so the partial-redraw path
1032
+ // can operate on a consistent viewport basis.
1033
+ // Use newLines.length (not previousLines.length) so the correction is exact for the
1034
+ // current frame — previousLines.length can still exceed newLines.length, leaving
1035
+ // residual drift for one more cycle and causing ghost lines.
1036
+ if (hasViewportBasisDrift) {
1037
+ this.maxLinesRendered = newLines.length;
1038
+ viewportTop = Math.max(0, this.maxLinesRendered - height);
1039
+ prevViewportTop = viewportTop;
1040
+ this.previousViewportTop = viewportTop;
1041
+ }
1042
+
1043
+ // Find first and last changed lines
1044
+ let firstChanged = -1;
1045
+ let lastChanged = -1;
1046
+ const maxLines = Math.max(newLines.length, this.previousLines.length);
1047
+ for (let i = 0; i < maxLines; i++) {
1048
+ const oldLine = i < this.previousLines.length ? this.previousLines[i] : "";
1049
+ const newLine = i < newLines.length ? newLines[i] : "";
1050
+
1051
+ if (oldLine !== newLine) {
1052
+ if (firstChanged === -1) {
1053
+ firstChanged = i;
1054
+ }
1055
+ lastChanged = i;
1056
+ }
1057
+ }
1058
+ const appendedLines = newLines.length > this.previousLines.length;
1059
+ if (appendedLines) {
1060
+ if (firstChanged === -1) {
1061
+ firstChanged = this.previousLines.length;
1062
+ }
1063
+ lastChanged = newLines.length - 1;
1064
+ }
1065
+ const appendStart =
1066
+ appendedLines && firstChanged === this.previousLines.length && firstChanged > 0;
1067
+
1068
+ // No changes - but still need to update hardware cursor position if it moved
1069
+ if (firstChanged === -1) {
1070
+ this.positionHardwareCursor(cursorPos, newLines.length);
1071
+ this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
1072
+ return;
1073
+ }
1074
+
1075
+ // All changes are in deleted lines (nothing to render, just clear)
1076
+ if (firstChanged >= newLines.length) {
1077
+ if (this.previousLines.length > newLines.length) {
1078
+ let buffer = "\x1b[?2026h";
1079
+ // Move to end of new content (clamp to 0 for empty content)
1080
+ const targetRow = Math.max(0, newLines.length - 1);
1081
+ const lineDiff = computeLineDiff(targetRow);
1082
+ if (lineDiff > 0) buffer += `\x1b[${lineDiff}B`;
1083
+ else if (lineDiff < 0) buffer += `\x1b[${-lineDiff}A`;
1084
+ buffer += "\r";
1085
+ // Clear extra lines without scrolling
1086
+ const extraLines = this.previousLines.length - newLines.length;
1087
+ if (extraLines > height) {
1088
+ logRedraw(`extraLines > height (${extraLines} > ${height})`);
1089
+ fullRender(true);
1090
+ return;
1091
+ }
1092
+ if (extraLines > 0) {
1093
+ buffer += "\x1b[1B";
1094
+ }
1095
+ for (let i = 0; i < extraLines; i++) {
1096
+ buffer += "\r\x1b[2K";
1097
+ if (i < extraLines - 1) buffer += "\x1b[1B";
1098
+ }
1099
+ if (extraLines > 0) {
1100
+ buffer += `\x1b[${extraLines}A`;
1101
+ }
1102
+ buffer += "\x1b[?2026l";
1103
+ this.terminal.write(buffer);
1104
+ this.cursorRow = targetRow;
1105
+ this.hardwareCursorRow = targetRow;
1106
+ }
1107
+ this.positionHardwareCursor(cursorPos, newLines.length);
1108
+ this.previousLines = newLines;
1109
+ this.previousWidth = width;
1110
+ this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
1111
+ return;
1112
+ }
1113
+
1114
+ // If first changed line is above the current viewport basis, partial redraw is unsafe.
1115
+ if (firstChanged < prevViewportTop) {
1116
+ logRedraw(`firstChanged < viewportTop (${firstChanged} < ${prevViewportTop})`);
1117
+ fullRender(true);
1118
+ return;
1119
+ }
1120
+
1121
+ // Render from first changed line to end
1122
+ // Build buffer with all updates wrapped in synchronized output
1123
+ let buffer = "\x1b[?2026h"; // Begin synchronized output
1124
+ const prevViewportBottom = prevViewportTop + height - 1;
1125
+ const moveTargetRow = appendStart ? firstChanged - 1 : firstChanged;
1126
+ if (moveTargetRow > prevViewportBottom) {
1127
+ const currentScreenRow = Math.max(
1128
+ 0,
1129
+ Math.min(height - 1, hardwareCursorRow - prevViewportTop)
1130
+ );
1131
+ const moveToBottom = height - 1 - currentScreenRow;
1132
+ if (moveToBottom > 0) {
1133
+ buffer += `\x1b[${moveToBottom}B`;
1134
+ }
1135
+ const scroll = moveTargetRow - prevViewportBottom;
1136
+ buffer += "\r\n".repeat(scroll);
1137
+ prevViewportTop += scroll;
1138
+ viewportTop += scroll;
1139
+ hardwareCursorRow = moveTargetRow;
1140
+ }
1141
+
1142
+ // Move cursor to first changed line (use hardwareCursorRow for actual position)
1143
+ const lineDiff = computeLineDiff(moveTargetRow);
1144
+ if (lineDiff > 0) {
1145
+ buffer += `\x1b[${lineDiff}B`; // Move down
1146
+ } else if (lineDiff < 0) {
1147
+ buffer += `\x1b[${-lineDiff}A`; // Move up
1148
+ }
1149
+
1150
+ buffer += appendStart ? "\r\n" : "\r"; // Move to column 0
1151
+
1152
+ // Only render changed lines (firstChanged to lastChanged), not all lines to end
1153
+ // This reduces flicker when only a single line changes (e.g., spinner animation)
1154
+ const renderEnd = Math.min(lastChanged, newLines.length - 1);
1155
+ for (let i = firstChanged; i <= renderEnd; i++) {
1156
+ if (i > firstChanged) buffer += "\r\n";
1157
+ buffer += "\x1b[2K"; // Clear current line
1158
+ let line = newLines[i];
1159
+ const isImage = isImageLine(line);
1160
+ if (!isImage && visibleWidth(line) > width) {
1161
+ // Log all lines to crash file for debugging
1162
+ const crashLogPath = path.join(os.homedir(), ".pi", "agent", "pi-crash.log");
1163
+ const crashData = [
1164
+ `Crash at ${new Date().toISOString()}`,
1165
+ `Terminal width: ${width}`,
1166
+ `Line ${i} visible width: ${visibleWidth(line)}`,
1167
+ "",
1168
+ "=== All rendered lines ===",
1169
+ ...newLines.map((l, idx) => `[${idx}] (w=${visibleWidth(l)}) ${l}`),
1170
+ "",
1171
+ ].join("\n");
1172
+ fs.mkdirSync(path.dirname(crashLogPath), { recursive: true });
1173
+ fs.writeFileSync(crashLogPath, crashData);
1174
+
1175
+ if (process.env.TALLOW_DEBUG || process.env.PI_DEBUG) {
1176
+ // In debug mode, throw to surface the problem for fixing
1177
+ this.stop();
1178
+ const errorMsg = [
1179
+ `Rendered line ${i} exceeds terminal width (${visibleWidth(line)} > ${width}).`,
1180
+ "",
1181
+ "This is likely caused by a custom TUI component not truncating its output.",
1182
+ "Use visibleWidth() to measure and truncateToWidth() to truncate lines.",
1183
+ "",
1184
+ `Debug log written to: ${crashLogPath}`,
1185
+ ].join("\n");
1186
+ throw new Error(errorMsg);
1187
+ }
1188
+
1189
+ // Production: defensively clamp the line instead of crashing
1190
+ line = truncateToWidth(line, width, "");
1191
+ newLines[i] = line;
1192
+ }
1193
+ buffer += line;
1194
+ }
1195
+
1196
+ // Track where cursor ended up after rendering
1197
+ let finalCursorRow = renderEnd;
1198
+
1199
+ // If we had more lines before, clear them and move cursor back
1200
+ if (this.previousLines.length > newLines.length) {
1201
+ const extraLines = this.previousLines.length - newLines.length;
1202
+ // Safety guard: when extraLines exceeds terminal height, the \r\n
1203
+ // sequence would scroll the viewport and desynchronize cursor tracking.
1204
+ // Fall back to a full redraw instead (same guard as the deleted-lines-only path).
1205
+ if (extraLines > height) {
1206
+ logRedraw(`extraLines > height in diff path (${extraLines} > ${height})`);
1207
+ fullRender(true);
1208
+ return;
1209
+ }
1210
+ // Move to end of new content first if we stopped before it
1211
+ if (renderEnd < newLines.length - 1) {
1212
+ const moveDown = newLines.length - 1 - renderEnd;
1213
+ buffer += `\x1b[${moveDown}B`;
1214
+ finalCursorRow = newLines.length - 1;
1215
+ }
1216
+ for (let i = newLines.length; i < this.previousLines.length; i++) {
1217
+ buffer += "\r\n\x1b[2K";
1218
+ }
1219
+ // Move cursor back to end of new content
1220
+ buffer += `\x1b[${extraLines}A`;
1221
+ }
1222
+
1223
+ buffer += "\x1b[?2026l"; // End synchronized output
1224
+
1225
+ if (process.env.PI_TUI_DEBUG === "1") {
1226
+ const debugDir = "/tmp/tui";
1227
+ fs.mkdirSync(debugDir, { recursive: true });
1228
+
1229
+ // Log large height fluctuations for diagnosing intermittent ghost gaps
1230
+ const heightDelta = newLines.length - this.previousLines.length;
1231
+ if (Math.abs(heightDelta) > 5) {
1232
+ const fluctPath = path.join(debugDir, "height-fluctuations.log");
1233
+ const fluctMsg =
1234
+ `[${new Date().toISOString()}] heightFluctuation: ${heightDelta > 0 ? "+" : ""}${heightDelta} ` +
1235
+ `(prev=${this.previousLines.length}, new=${newLines.length}, ` +
1236
+ `maxLR=${this.maxLinesRendered}, viewportTop=${viewportTop})\n`;
1237
+ fs.appendFileSync(fluctPath, fluctMsg);
1238
+ }
1239
+
1240
+ const debugPath = path.join(
1241
+ debugDir,
1242
+ `render-${Date.now()}-${crypto.randomUUID().slice(0, 8)}.log`
1243
+ );
1244
+ const debugData = [
1245
+ `firstChanged: ${firstChanged}`,
1246
+ `viewportTop: ${viewportTop}`,
1247
+ `cursorRow: ${this.cursorRow}`,
1248
+ `height: ${height}`,
1249
+ `lineDiff: ${lineDiff}`,
1250
+ `hardwareCursorRow: ${hardwareCursorRow}`,
1251
+ `renderEnd: ${renderEnd}`,
1252
+ `finalCursorRow: ${finalCursorRow}`,
1253
+ `cursorPos: ${JSON.stringify(cursorPos)}`,
1254
+ `newLines.length: ${newLines.length}`,
1255
+ `previousLines.length: ${this.previousLines.length}`,
1256
+ `maxLinesRendered: ${this.maxLinesRendered}`,
1257
+ "",
1258
+ "=== newLines ===",
1259
+ JSON.stringify(newLines, null, 2),
1260
+ "",
1261
+ "=== previousLines ===",
1262
+ JSON.stringify(this.previousLines, null, 2),
1263
+ "",
1264
+ "=== buffer ===",
1265
+ JSON.stringify(buffer),
1266
+ ].join("\n");
1267
+ fs.writeFileSync(debugPath, debugData, { mode: 0o600 });
1268
+ }
1269
+
1270
+ // Write entire buffer at once
1271
+ this.terminal.write(buffer);
1272
+
1273
+ // Track cursor position for next render
1274
+ // cursorRow tracks end of content (for viewport calculation)
1275
+ // hardwareCursorRow tracks actual terminal cursor position (for movement)
1276
+ this.cursorRow = Math.max(0, newLines.length - 1);
1277
+ this.hardwareCursorRow = finalCursorRow;
1278
+ // Track terminal's working area.
1279
+ // When overlays are active, keep the high-water mark so overlay padding stays
1280
+ // stable. When no overlays are active and content shrank, decay to actual
1281
+ // content size — this prevents permanent ghost height from stale maxLinesRendered.
1282
+ if (this.overlayStack.length === 0) {
1283
+ this.maxLinesRendered = newLines.length;
1284
+ } else {
1285
+ this.maxLinesRendered = Math.max(this.maxLinesRendered, newLines.length);
1286
+ }
1287
+ this.previousViewportTop = Math.max(0, this.maxLinesRendered - height);
1288
+
1289
+ // Position hardware cursor for IME
1290
+ this.positionHardwareCursor(cursorPos, newLines.length);
1291
+
1292
+ this.previousLines = newLines;
1293
+ this.previousWidth = width;
1294
+ }
1295
+
1296
+ /**
1297
+ * Position the hardware cursor for IME candidate window.
1298
+ * @param cursorPos The cursor position extracted from rendered output, or null
1299
+ * @param totalLines Total number of rendered lines
1300
+ */
1301
+ private positionHardwareCursor(
1302
+ cursorPos: { row: number; col: number } | null,
1303
+ totalLines: number
1304
+ ): void {
1305
+ if (!cursorPos || totalLines <= 0) {
1306
+ this.terminal.hideCursor();
1307
+ return;
1308
+ }
1309
+
1310
+ // Clamp cursor position to valid range
1311
+ const targetRow = Math.max(0, Math.min(cursorPos.row, totalLines - 1));
1312
+ const targetCol = Math.max(0, cursorPos.col);
1313
+
1314
+ // Move cursor from current position to target
1315
+ const rowDelta = targetRow - this.hardwareCursorRow;
1316
+ let buffer = "";
1317
+ if (rowDelta > 0) {
1318
+ buffer += `\x1b[${rowDelta}B`; // Move down
1319
+ } else if (rowDelta < 0) {
1320
+ buffer += `\x1b[${-rowDelta}A`; // Move up
1321
+ }
1322
+ // Move to absolute column (1-indexed)
1323
+ buffer += `\x1b[${targetCol + 1}G`;
1324
+
1325
+ if (buffer) {
1326
+ this.terminal.write(buffer);
1327
+ }
1328
+
1329
+ this.hardwareCursorRow = targetRow;
1330
+ if (this.showHardwareCursor) {
1331
+ this.terminal.showCursor();
1332
+ } else {
1333
+ this.terminal.hideCursor();
1334
+ }
1335
+ }
1336
+ }