@@ -30,6 +30,14 @@ import { getMaxScrollOffset } from "./scroll.js";
3030
3131 type RenderFn = () => Node | Node[];
3232
33+type TerminalCursorState =
34+ | { visible: false }
35+ | {
36+ visible: true;
37+ x: number;
38+ y: number;
39+ };
40+
3341 let terminal: Terminal | null = null;
3442 let activeTheme: Theme = defaultTheme;
3543 let renderFn: RenderFn | null = null;
@@ -60,6 +68,9 @@ const uncontrolledScrollOffsets = new Map<string, number>();
6068 /** Nodes whose props were stamped with scrollOffset during the last paint. */
6169 let stampedScrollNodes: { node: Node; key: string }[] = [];
6270
71+/** The cursor state currently expected on the terminal. */
72+let lastTerminalCursor: TerminalCursorState | null = { visible: false };
73+
6374 function doRender(): void {
6475 renderScheduled = false;
6576 if (!renderFn || !terminal) return;
@@ -105,43 +116,38 @@ function doRender(): void {
105116 unstampUncontrolledFocus();
106117 unstampUncontrolledScroll();
107118
119+ const nextCursor = getDesiredTerminalCursor();
120+
108121 // Emit to terminal โ differential when possible
109122 if (prevBuffer) {
110- const output = emitDiff(prevBuffer, currentBuffer, activeTheme);
123+ const output = emitDiff(prevBuffer, currentBuffer, activeTheme, {
124+ cursor: nextCursor,
125+ previousCursor: lastTerminalCursor,
126+ });
111127 if (output.length > 0) terminal.write(output);
112128 } else {
113- const output = emitBuffer(currentBuffer, activeTheme);
129+ const output = emitBuffer(currentBuffer, activeTheme, {
130+ cursor: nextCursor,
131+ previousCursor: lastTerminalCursor,
132+ });
114133 terminal.write(output);
115134 }
116135
117- // Position the native terminal cursor at the focused TextInput's cursor.
118- // This gives us a blinking cursor for free (terminal-managed blink).
119- positionTerminalCursor();
136+ lastTerminalCursor = nextCursor;
120137 }
121138
122139 /**
123- * After each render, show the native terminal cursor at the focused
124- * TextInput's cursor position (gives blinking for free). When no
125- * TextInput is focused, hide the cursor.
140+ * Resolve the final terminal cursor state for the current frame.
141+ * The cursor is shown only for a focused TextInput whose cursor is visible
142+ * within the clipped viewport.
126143 */
127-function positionTerminalCursor(): void {
128- if (!terminal) return;
129-
130- // Find focused TextInput in the layout tree (check stamped state during
131- // render โ we need to check controlled focus in the current layouts)
144+function getDesiredTerminalCursor(): TerminalCursorState {
132145 const focusedTI = findFocusedTextInputLayout();
133- if (focusedTI) {
134- const props = focusedTI.node
135- .props as import("@cel-tui/types").TextInputProps;
136- const pos = getTextInputCursorScreenPos(props, focusedTI.rect);
137- if (pos) {
138- // CUP: move cursor to (row, col) โ 1-indexed
139- terminal.write(`\x1b[${pos.y + 1};${pos.x + 1}H`);
140- terminal.showCursor();
141- return;
142- }
143- }
144- terminal.hideCursor();
146+ if (!focusedTI) return { visible: false };
147+
148+ const props = focusedTI.node.props as import("@cel-tui/types").TextInputProps;
149+ const pos = getTextInputCursorScreenPos(props, focusedTI.rect);
150+ return pos ? { visible: true, x: pos.x, y: pos.y } : { visible: false };
145151 }
146152
147153 /**
@@ -847,6 +853,7 @@ export const cel = {
847853 init(term: Terminal, options?: { theme?: Theme }): void {
848854 terminal = term;
849855 activeTheme = options?.theme ?? defaultTheme;
856+ lastTerminalCursor = { visible: false };
850857 terminal.start(handleInput, () => cel.render());
851858 },
852859
@@ -892,6 +899,7 @@ export const cel = {
892899 stampedNode = null;
893900 uncontrolledScrollOffsets.clear();
894901 stampedScrollNodes = [];
902+ lastTerminalCursor = { visible: false };
895903 activeTheme = defaultTheme;
896904 },
897905