Skip to content

gh-145448: Fix REPL tab completion cursor position in pending wrap state#145727

Open
mvanhorn wants to merge 1 commit intopython:mainfrom
mvanhorn:osc/145448-fix-repl-tab-cursor
Open

gh-145448: Fix REPL tab completion cursor position in pending wrap state#145727
mvanhorn wants to merge 1 commit intopython:mainfrom
mvanhorn:osc/145448-fix-repl-tab-cursor

Conversation

@mvanhorn
Copy link
Contributor

@mvanhorn mvanhorn commented Mar 10, 2026

Fixes #145448

Summary

When the PyREPL's __write_changed_line writes to the last column of the terminal, the cursor enters "pending wrap" state - it physically stays at column width-1 but posxy was recording width. This caused subsequent CUB (Cursor Backward) escape sequences to overshoot by one cell, moving the cursor one position too far left.

Root cause: In unix_console.py, three code paths in __write_changed_line set posxy to the full line width without accounting for pending wrap state. When the cursor is later moved back (e.g., to return to the input line after showing "[ complete but not unique ]"), the CUB count is calculated as target_x - width instead of target_x - (width-1), producing one extra backward movement.

Fix: Cap posxy[0] at self.width - 1 when writing reaches the terminal edge. This matches the physical cursor position in pending wrap state, so CUB calculations produce the correct count.

Why Terminal.app isn't affected: Terminal.app compensates by subtracting 1 from CUB in pending wrap state. xterm and Ghostty do not compensate, matching the spec more strictly.

Test plan

  • All 244 test_pyrepl tests pass
  • NEWS entry added
  • Fix applied to all three posxy assignment paths in __write_changed_line

This contribution was developed with AI assistance (Claude Code).

…rap state

When __write_changed_line writes to the last column of the terminal,
the cursor enters "pending wrap" state where it physically stays at
width-1. However, posxy was recording width, causing subsequent CUB
(Cursor Backward) calculations to overshoot by one cell.

Cap posxy x-coordinate at width-1 to match the physical cursor
position in pending wrap state. This fixes the cursor moving backward
during tab completion in xterm and Ghostty.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

REPL import tab completion moves cursor backward

1 participant