szn

Bug 311 — Multiline prompt cursor jumping and text scrambling during line editing

311. Multiline prompt cursor jumping and text scrambling during line editing

File: src/screen.zig:575-597, 803-875, 893-940 Severity: HIGH Status: ✅ FIXED

During interactive prompt history navigation and line editing (e.g. using antigravity CLI inside szn), moving the cursor into the middle of a soft-wrapped multiline prompt and inserting text or spacebar caused screen display scrambling, cursor jumps to blank lines, and trailing space duplication.

Root Cause

  1. Unclamped Right Margin cursor.x on Vertical Movement: When characters filled a line to the rightmost column (cursor.x == grid.width), subsequent vertical cursor movements (cursorUp, cursorDown, cursorLine) updated cursor.y but left cursor.x == grid.width unclamped. Writing the first character on the new row immediately triggered an erroneous auto-wrap to an extra row below, causing desynchronized screen scrolling and cursor jumping.
  2. \r & \n Reset of line.wrapped on Full Lines: When line editors filled a line to the right margin and output \r\n to move to the next row, \r previously reset cursor.x = 0. When \n arrived next, cursor.x was 0, so the filled line was falsely left as wrapped = false. cursorBack treated soft-wrapped lines as independent hard lines and refused to step backwards across line boundaries, trapping the cursor and causing line editors to repeatedly insert space padding.
  3. Asymmetrical Wrapped Line Navigation: cursorForward stopped at grid.width - 1 without stepping forward across soft-wrapped line boundaries onto (0, cursor.y + 1).

Fix

  1. In src/screen.zig, updated cursorUp, cursorDown, cursorLine, cursorPosition, cursorColumn, cursorForward, cursorBack, and setCursorAbs to strictly clamp cursor.x to @min(cursor.x, grid.width -| 1) on any movement command.
  2. Updated writeChar so that if cursor.x >= grid.width when \r or \n occurs, the current line is marked wrapped = true.
  3. Updated cursorForward to step forward across soft-wrapped line boundaries when cursor.x == grid.width - 1 and line.wrapped == true.
  4. Added CSI Z (Cursor Backward Tab) handling in src/input.zig with soft-wrap boundary support.
  5. Added comprehensive unit tests in src/screen.zig for CR/LF wrapped line preservation, cursorForward wrapped line stepping, and vertical movement clamping.