Skip to content

Keep redundant OSC 8 closes in the outer clipping cursor context - #308

Closed
RealBhupesh wants to merge 3 commits into
jquast:masterfrom
RealBhupesh:master
Closed

RealBhupesh wants to merge 3 commits into
jquast:masterfrom
RealBhupesh:master

Conversation

@RealBhupesh

Copy link
Copy Markdown
Contributor

A legal OSC 8 close without an active link can make clip() treat ordinary following text as a hyperlink unit, up to the next close. That inner text is then measured/clipped from column zero, losing its actual cursor position.

from wcwidth import clip, strip_sequences

close = '\x1b]8;;\x07'
strip_sequences(clip('a' + close + '\tb' + close, 0, 20))
# before: 'a        b'  (8 spaces)
# after:  'a       b'   (7 spaces, same as clipping 'a\tb')

strip_sequences(clip('ab' + close + '\rc' + close, 0, 20))
# before: 'abc'
# after:  'cb'

The OSC 8 description explicitly allows closing a hyperlink when none is open.

When empty-URL parameters are followed by nonempty text before the next close, use the existing ordinary-sequence fallback instead of processing a hyperlink unit. This keeps the following text in the outer cursor context. Adjacent empty close-close units retain their existing behavior. HyperlinkParams.parse() is unchanged.

Added 12 cases covering BEL/ST terminators, tabs/carriage returns and three clipping windows, with explicit visible output expectations.

Validation on Python 3.10 after editable installation:

  • New tests against the unchanged implementation: 322 passed, 10 failed, 35 skipped in the targeted hyperlink/clip files.
  • With the fix: 332 passed, 35 skipped in those files.
  • Full pure-Python suite (WCWIDTH_PYTHON=1): 1,782 passed, 47 skipped.
  • Full suite with the locally built C extension: 1,818 passed, 11 skipped.
  • Flake8, isort, configured docformatter checks on both changed files and git diff --check pass.
  • 12,000 seeded comparisons against equivalent plain-text clipping pass for redundant closes inserted at grapheme-unit boundaries, including tabs, carriage returns, backspaces, CJK and combining text.

Scope: this fixes redundant closes, not general cursor movement inside genuine hyperlink contents. Arbitrary escape insertion inside a grapheme cluster can expose separate preexisting segmentation differences and is not claimed fixed. No coverage percentage or CI result is claimed.

Developed and tested with AI assistance.

RealBhupesh and others added 3 commits November 7, 2026 02:15
Do not process the text between redundant OSC 8 closes as a hyperlink unit. Preserve the existing adjacent empty-unit behavior.

Developed with AI assistance.

Assisted-by: LLM
LLM: gpt-6-luna
Check BEL and ST terminators across three clipping windows with explicit visible output expectations.

Developed with AI assistance.

Assisted-by: LLM
LLM: gpt-6-luna
@jquast

jquast commented Nov 9, 2026 •

Copy link
Copy Markdown
Owner

This is borderline "unlikely sequence", and documented here and references this issue, https://wcwidth.readthedocs.io/en/latest/intro.html#unlikely-sequences

but if performance impact is negligible, I think I may accept it, it is probably best to prevent additional AI Agents from boiling water to make the same "bugfixes" of non-likely sequences, redundant closing tags are a bit like html and should probably not have run-on or ambient effects

@codspeed

codspeed Bot commented Nov 9, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 142 untouched benchmarks


Comparing RealBhupesh:master (c849914) with master (fa21975)

Open in CodSpeed

@codecov

codecov Bot commented Nov 9, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.74%. Comparing base (fa21975) to head (c849914).

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #308   +/-   ##
=======================================
  Coverage   88.74%   88.74%           
=======================================
  Files          41       41           
  Lines        5669     5671    +2     
  Branches     1522     1523    +1     
=======================================
+ Hits         5031     5033    +2     
  Misses        254      254           
  Partials      384      384           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@RealBhupesh

Copy link
Copy Markdown
Contributor Author

Thanks for the review. I checked the existing clip() guard directly, separately from the 142 untouched benchmarks.

Compared upstream fa21975c0106cc1242a618d769301e3499e8fdb7 with PR head c84991439c49f61649e29ebd1b59b1dc0b66993c on Linux, CPython 3.10.12, pure Python (WCWIDTH_PYTHON=1). Seven paired batches alternated version order, with 2,000 calls per fixture per version in each batch. Each version used a fresh process per batch; imports and output checks were outside timing. Inputs came from the existing tests, with no new cases.

Same-output controls, microseconds per call:

Existing fixture Upstream median (min-max) PR median (min-max)
ASCII clip('hello world', 6, 11) 0.3729 (0.3653-0.3748) 0.3717 (0.3672-0.3872)
Valid BEL-terminated hyperlink, link, window 0:4 7.9256 (7.7275-8.2508) 7.9382 (7.8283-7.9746)

The four existing redundant-close tab/CR fixtures (BEL and ST) also produced the expected outputs for both revisions. Their measured times were lower on the PR, but the outputs and work differ because of the correction, so I wouldn't use them as an equivalent-output speed comparison.

No apparent large slowdown showed up in these two short controls in this environment. This is a small local measurement, not an equivalence test or a guarantee of negligible impact: only seven pairs, no CPU affinity/frequency control, no C-extension measurement, and no broad workload coverage.

jquast added a commit that referenced this pull request Nov 10, 2026
This is PR #308 with tests and comments rewritten.

This is something of an Unlikely Sequence, it would be a bug in any TUI library to generate extra close tags. Like an extra/errant HTML close tag, we should strive not to have strange side-effects when they are present, defending against it has negligible performance.

> Do not process the text between redundant OSC 8 closes as a hyperlink unit. Preserve the existing adjacent empty-unit behavior.
>
> Developed with AI assistance.
>
> Assisted-by: LLM
> LLM: gpt-6-luna

---------

Co-authored-by: Bhupesh Cholake <[email protected]>
@jquast

jquast commented Nov 10, 2026

Copy link
Copy Markdown
Owner

Thanks, it is merged for next release by #319

@jquast jquast closed this Nov 10, 2026
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.

2 participants