Skip to content

wrap() retains trailing spaces before SGR sequences with drop_whitespace=True #332

Backlog

Description

@PriyansuMaurya

Python wrap() can retain a trailing visible space when a colour sequence begins a long word at a line boundary, despite drop_whitespace=True.

from wcwidth import wrap, strip_sequences

text = "a \x1b[31mlongword\x1b[0m"
lines = wrap(text, width=2)
print([strip_sequences(line) for line in lines])
# ['a ', 'lo', 'ng', 'wo', 'rd']
# expected: ['a', 'lo', 'ng', 'wo', 'rd']

This affects coloured narrow-column CLI output: the plain text version trims the space, but adding an SGR prefix leaves it visible. Reproduced in both line_break='unicode' and 'ascii', with SGR propagation enabled and disabled, on current master / Linux / CPython 3.12.14.

When a long word's leading sequence is assigned to the preceding full line, its final character is no longer whitespace, so the final str.rstrip() misses the space before that sequence. The existing _rstrip_visible() helper can remove that whitespace while preserving trailing sequences; sequence-only inputs still need to be retained.

Four regression cases fail before the fix; six controls cover drop_whitespace=False and sequence-only input. A focused Python fix passes all 392 text-wrapping tests and the full Python suite/doctests (1,966 passed, 56 skipped).

Activity

  1. added 2 commits that reference this issue on Nov 25, 2026
    b271890
    3bcca17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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