01: Create src/render/terminal.ts facade for Bun 1.4 ANSI APIs - #159
Merged
Conversation
- Add visibleWidth() wrapping Bun.stringWidth() for accurate terminal column measurement - Add stripAnsi() wrapping Bun.stripANSI() to remove all escape sequences - Add clipToWidth() wrapping Bun.sliceAnsi() with partial reset (\x1b[22;39m) to preserve background styling when truncating - Add hasAnsi() to detect presence of ANSI codes - Comprehensive test coverage: ASCII, SGR colors, OSC 8 hyperlinks, emoji, ZWJ sequences, flags, CJK characters, graphemes with skin tone modifiers, mixed content - Re-export from src/render.ts facade Fixes: #141
|
Coverage after merging feat/bun14-terminal/01-terminal into main will be
Coverage Report
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PR #159 was failing knip because render.ts was re-exporting visibleWidth, stripAnsi, clipToWidth, hasAnsi but no one was using them. On branch 01, render.ts still has its own stripAnsi() and clipAnsi() implementations; the migration to Bun 1.4 APIs happens on branch 02. Remove the unused re-export to satisfy knip.
|
Coverage after merging feat/bun14-terminal/01-terminal into main will be
Coverage Report
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a terminal utility facade around Bun 1.4 ANSI and width APIs, with Unicode and styling tests.
Changes:
- Adds width, ANSI stripping/detection, and clipping helpers.
- Adds tests for emoji, CJK, grapheme clusters, styling, and truncation.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Summary | Findings |
|---|---|---|
src/render/terminal.ts |
Bun terminal API facade | Critical (4 votes): Support Bun 1.0–1.3 or raise the documented minimum to Bun 1.4. Moderate (4 votes): Guard non-positive clipping limits. Moderate (2 votes): Re-export the APIs from src/render.ts. |
src/render/terminal.test.ts |
Terminal utility tests | Nit (2 votes): Rename the “single-width emoji” case to reflect that U+1F50D is two columns wide. |
Suppressed comments (2)
src/render/terminal.ts:38
Bun.sliceAnsicloses active ANSI state at the slice boundary, so appending\x1b[22;39mcannot preserve a caller-owned background: clipping a string beginning with\x1b[48;5;53mcan emit a background reset before this suffix. The documented active-row guarantee is therefore false; clip before applying the outer background or preserve/reapply that background after slicing.
const clipped = Bun.sliceAnsi(str, 0, maxCols);
// Append a partial reset: turn off bold (22) and reset foreground (39) without affecting background.
return clipped + "\x1b[22;39m";
src/render/terminal.ts:38
- The appended reset only clears bold and foreground. If the clipped input opens another SGR style such as underline or inverse, that style remains active after the returned string and can affect subsequent terminal output. Close all non-background attributes (or explicitly track and restore the caller's background) while retaining the active background; the current partial reset is not safe for the generic ANSI wrapper described here.
const clipped = Bun.sliceAnsi(str, 0, maxCols);
// Append a partial reset: turn off bold (22) and reset foreground (39) without affecting background.
return clipped + "\x1b[22;39m";
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| expect(visibleWidth(pc.red("world"))).toBe(5); | ||
| }); | ||
|
|
||
| it("handles single-width emoji", () => { |
Comment on lines
+32
to
+36
| const width = visibleWidth(str); | ||
| if (width <= maxCols) { | ||
| return str; | ||
| } | ||
| const clipped = Bun.sliceAnsi(str, 0, maxCols); |
| * Correctly handles emoji, CJK characters, and multi-code-point grapheme clusters. | ||
| */ | ||
| export function visibleWidth(str: string): number { | ||
| return Bun.stringWidth(str); |
Comment on lines
+9
to
+10
| export function visibleWidth(str: string): number { | ||
| return Bun.stringWidth(str); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do?
How did you verify your code works?