The Cursor-Anchor Law: Why Text No Longer Slides Around
November 13, 2026
A small bug report with a big blast radius came in last week: “In jterm text fields and text boxes, the cursor moves text around. It’s never on a character. If the cursor goes right, the character to its right goes to the left of the cursor. It should stay where it is and just have a new cursor-colored background. That’s the way most applications work.”
That sentence contains the whole bug. This post is about how cursor positioning actually works in a terminal app, what jterm was doing wrong, and the new cursor-anchor law that makes text fields behave like every modern editor.
The cursor is two cursors
A TUI really has two cursors, and they usually get conflated:
- The app caret — what the application draws: an inverted cell (swapped foreground/background colors) where your typing would insert. Most terminal apps fake this with color; the terminal’s own cursor is unrelated to it.
- The hardware cursor — the blinking thing the terminal itself positions with
ESC[row;colH. Clients get to decide what to draw there: some draw a block, some draw a bar, some hide it (jterm clients send?25l— hide cursor — and paint their own from the last CUP position).
jterm’s text widgets already painted a proper inverted caret cell — that part was always right. The problem was the hardware cursor’s resting position after a frame, which no component ever managed.
The one-cell drift
Here’s the sequence a text field produced, before the fix:
- You type
cin a field containingab▮(▮ = the inverted caret cell). - The widget repaints the field:
abc▮— three plain characters, one caret cell. - jterm’s diff renderer writes the changed cells — including the last-painted cell — and, like most cell-grid terminal output of this style, the hardware cursor ends up one cell past the last written cell. That’s simply where CUP leaves the pen after drawing.
- The render loop ended there. Nothing ever parked the cursor.
So after every keystroke, the hardware cursor rested at cell after the caret. Two visible consequences stacked up:
- Clients that hide the hardware cursor (
?25l) are unaffected — they never look at it. - Clients that draw their own block cursor at the hardware cursor’s position render the blinking block beside the caret cell. Now the screen shows the app’s inverted cell and a client cursor one cell to the right: the caret looks like it’s between characters, and as you type, the pairing shifts cell by cell. Text looks like it’s being pushed around — exactly what the report said. (The app’s own caret is
ESC[7m-style swapped colors, which some of our clients can’t do — that’s why the client-drawn block cursor exists at all.)
The drift was invisible the whole time on cursor-less clients, which is why it survived: the simplest client hid the bug’s witness.
The fix: the cursor-anchor law
The new law is one sentence: after every emitted frame, the terminal cursor rests ON the focused text widget’s caret cell — or stays wherever the frame left it if nothing is focused.
Three cooperating pieces (this is the interesting design bit — each answers a different constraint):
1. Widgets report their caret cell during draw. Component.getCursorCell() is the new seam: TextBox and TextArea record the absolute on-screen position of their caret cell while they’re focused and painting, and report it back. Why during draw? Because that’s the only moment a widget actually knows where it painted — the diff renderer repaints rectangular slices, widgets can be nested inside panels anywhere on screen, and a stored “widget-relative” position would need re-mapping every time surrounding geometry changed. The absolute cell falls out of the draw for free: sub-graphics track their own origin, so graphics.absoluteOrigin() + caretOffset is recorded while the caret cell is painted.
2. Containers forward. Dialogs and menus focus their root panel, not the input inside it — so AbstractContainer forwards the first non-null child report. Without that, every real dialog (where the input lives inside a window’s panel) would report nothing and the law would hold only for bare-text-field screens.
3. The screen parks it — every frame, every path. DefaultTextGUI clears the anchor at the top of each frame and sets it from the active window’s focused component; DefaultScreen then parks the terminal cursor at the anchor and flushes in the finally-path of refresh() — for all three render modes (full repaint, row-shift, delta). The delta path was the sneaky one: a one-character edit emits a tiny diff frame, and the cursor still has to land correctly on it. Frames without an anchor keep legacy behavior — menus, doors, and everything else that isn’t a text field is completely unchanged.
On the wire it’s modest: a trailing ESC[row;colH after each text-field frame. That’s the entire cost.
The “ab▮ → abc▮” walk, corrected
Field shows ab▮ at columns 11–12–13. Type c:
- Cell 13 gains
c, the caret cell moves to column 14, hardware cursor parks at 14. - Press LEFT: the caret cell moves back onto the
cat column 13 — and the hardware cursor parks on column 13, not past it.
Now the client’s block cursor and the app’s inverted cell agree on the same cell. Text never moves; the cursor appears to “fill” the character it would edit into, exactly like every mainstream editor. That is the anchor on the wire, live: final cursor ESC[14;13H — column 13, the c cell.
How we know (the verification story, briefly)
- 7 laws in
CursorAnchorLawTest, RED first: widgets report absolute caret cells during focused draw, containers forward reports, the GUI clears-then-sets the anchor per frame, the screen parks it on all three render paths, and no-anchor frames stay byte-identical to the old behavior. - The neighboring widget/graphics suites that must not regress stayed green, and the full jterm gate ran 3,226 tests, 0 failures.
- Byte-deep, then wire-deep: the deployed fat jar carries the new field and accessor (
javapon the shipped class), and a scripted telnet probe typed exactly theabc+ LEFT sequence above and read where the frame’s lastESC[r;cHlanded — column 13, ON thec, every time.
What it means for clients
- Nothing changes for clients that hide the hardware cursor — they never consumed its position.
- Clients that render their own cursor from the position get sane behavior for free, for the first time: the blinking block now sits exactly on the character being edited.
- Any screen without a focused text field emits no new bytes beyond what it already emitted.
The whole fix is in jterm c299ebd — the cursor-anchor law, seven laws green, live-verified on the production board. Next time a text field feels haunted, check where the hardware cursor rests; the app caret was telling the truth all along.