Skip to content

Trim trailing spaces before terminal sequences in Python wrap - #333

Open
PriyansuMaurya wants to merge 2 commits into
jquast:masterfrom
PriyansuMaurya:fix/wrap-trailing-space-before-sequences
Open

PriyansuMaurya wants to merge 2 commits into
jquast:masterfrom
PriyansuMaurya:fix/wrap-trailing-space-before-sequences

Conversation

@PriyansuMaurya

@PriyansuMaurya PriyansuMaurya commented Nov 25, 2026 •

Copy link
Copy Markdown

Fixes #332.

wrap('a \x1b[31mlongword\x1b[0m', width=2) leaves a visible space on its first line even with drop_whitespace=True. The plain-text version trims it. A long word's zero-width style prefix can land at the end of the preceding line, hiding the space from str.rstrip().

Use the existing _rstrip_visible() helper in the sequence-ended-line branch when the last visible character is whitespace. Preserve terminal sequences and sequence-only inputs; normal text retains its existing path. Clarify the option's docstring.

Ten test cases cover both line-breaking modes, SGR propagation on/off, drop_whitespace=False, and sequence-only input. Before the fix, four regressions fail and six controls pass.

Validation on Linux:

  • CPython 3.12.14, pure Python: 2,006 passed, 51 skipped.
  • CPython 3.12.14 with compiled extension: 2,044 passed, 13 skipped.
  • CPython 3.14.10 with compiled extension: 2,044 passed, 13 skipped.
  • Full suites include module doctests; 392 wrapping tests pass.
  • Strict mypy: all 53 source files pass. Changed-file Flake8, Pylint, isort, docformatter, pydocstyle, and codespell pass.
  • Strict Sphinx build and git diff --check pass.

This patch changes the Python wrapper; the standalone C wrapper is outside its scope.

Use the existing sequence-aware trimming helper when a wrapped line ends in whitespace followed by terminal sequences. Preserve sequence-only inputs and clarify drop_whitespace.

Fixes jquast#332.
Add four regressions for both line-breaking modes and SGR propagation settings, plus six controls for drop_whitespace=False and sequence-only input. The four regressions fail on the original implementation.
@PriyansuMaurya
PriyansuMaurya force-pushed the fix/wrap-trailing-space-before-sequences branch from b362057 to 34e907f Compare November 25, 2026 22:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant