Skip to content

fix: prevent colored input prompts from overwriting wrapped lines #535

Description

@Unilinear

Problem

When running a chapter's code.py and entering a prompt longer than the terminal width, the wrapped text can visually overwrite the previous line. The submitted input remains intact; only the terminal rendering is corrupted.

Reproduction

  1. Run python s01_agent_loop/code.py in a terminal using GNU Readline.
  2. Type a prompt long enough to wrap onto a second visual line.
  3. Observe that Readline redraws the wrapped input at the wrong cursor position, hiding part of the first line.

Cause

The colored prompt passed to input() contains raw ANSI escape sequences:

query = input("\033[36ms01 >> \033[0m")

These sequences do not occupy terminal columns, but Readline includes them when calculating the visible prompt width unless they are delimited as non-printing spans.

Proposed fix

Wrap each ANSI sequence with Readline's \001 and \002 markers:

query = input("\001\033[36m\002s01 >> \001\033[0m\002")

The same prompt pattern is present in the current course files from s01 through s12, so the fix should be applied consistently across those chapters.

I reproduced this while working through the course and can submit a focused PR. I used an AI coding assistant to help diagnose the cursor-width issue and prepare the proposed change; I reviewed and tested the solution.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions