Skip to content

text: trim snip indicators to the target width - #438

Open
Don-Yin wants to merge 1 commit into
jedib0t:mainfrom
Don-Yin:fix/snip-indicator-width
Open

Don-Yin wants to merge 1 commit into
jedib0t:mainfrom
Don-Yin:fix/snip-indicator-width

Conversation

@Don-Yin

@Don-Yin Don-Yin commented Oct 23, 2026

Copy link
Copy Markdown

Proposed Changes

  • Trim snipIndicator with the existing ANSI-aware Trim before reserving its display width.
  • Extend TestSnip with short limits, colored and wide indicators, and an input that already fits.

When the indicator is wider than the limit, Snip currently drops the input but still appends the whole indicator:

text.Snip("abcdef", 1, "...") // before: "..."; after: "."
text.Snip("abcdef", 2, "...") // before: "..."; after: ".."

The one-line change keeps the indicator within the same display-width budget as the input. Inputs that already fit are unchanged.

Checked: make test (including format, vet, lint and complexity), make test-race, and make bench on Go 1.26.1. The new assertions fail before the production change and pass afterward.

The figure shows measured output widths for plain ... and the same indicator wrapped in ANSI color sequences:

Measured Snip output widths before and after the change

Trim the indicator before reserving its display width, so a short target
cannot be exceeded by the indicator alone. Reuse Trim to retain ANSI
sequences and account for wide characters.

Signed-off-by: Don Yin <Don_Yin@outlook.com>
@sonarqubecloud

Copy link
Copy Markdown

@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 39913107077

Coverage remained the same at 100.0%

Details

  • Coverage remained the same as the base build.
  • Patch coverage: 1 of 1 lines across 1 file are fully covered (100%).
  • No coverage regressions found.

Uncovered Changes

No uncovered changes found.

Coverage Regressions

No coverage regressions found.


Coverage Stats

Coverage Status
Relevant Lines: 5165
Covered Lines: 5165
Line Coverage: 100.0%
Coverage Strength: 1.21 hits per line

💛 - Coveralls

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