Skip to content

01: Create src/render/terminal.ts facade for Bun 1.4 ANSI APIs - #159

Merged
shouze merged 2 commits into
mainfrom
feat/bun14-terminal/01-terminal
Aug 23, 2026
Merged

01: Create src/render/terminal.ts facade for Bun 1.4 ANSI APIs#159
shouze merged 2 commits into
mainfrom
feat/bun14-terminal/01-terminal

Conversation

@shouze

@shouze shouze commented Aug 23, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

How did you verify your code works?

- 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
@github-actions

Copy link
Copy Markdown

Coverage after merging feat/bun14-terminal/01-terminal into main will be

96.02%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
src
   aggregate.ts100%100%100%100%
   api-utils.ts93.20%100%93.75%93.13%101–103, 65, 73, 86–87, 91–92
   api.ts94.74%100%100%94.07%340–344, 405, 422, 63–69
   cache.ts94.67%100%100%94.29%139–141, 39
   completions.ts99.42%100%100%99.37%270
   group.ts99.50%100%97.44%100%
   output.ts99.29%100%95.65%99.61%80
   regex.ts99.39%100%100%99.34%329
   render.ts89.89%100%89.47%89.91%167, 191–196, 198–200, 202–203, 254–255, 276, 463–464, 487–489, 555–559, 571–572, 577–584, 586–594, 596–597
   upgrade.ts88.38%100%94.44%87.89%128, 131, 133, 153, 167–168, 188–195, 198–204, 209, 214, 250–253
src/render
   filter-match.ts97.44%100%92.31%100%
   filter.ts100%100%100%100%
   highlight.ts96.63%100%90.40%99.31%284–285
   rows.ts97.58%100%100%97.44%168, 54–55
   selection.ts100%100%100%100%
   summary.ts100%100%100%100%
   team-pick.ts100%100%100%100%
   terminal.ts100%100%100%100%

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.
@github-actions

Copy link
Copy Markdown

Coverage after merging feat/bun14-terminal/01-terminal into main will be

96.02%

Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
src
   aggregate.ts100%100%100%100%
   api-utils.ts93.20%100%93.75%93.13%101–103, 65, 73, 86–87, 91–92
   api.ts94.74%100%100%94.07%340–344, 405, 422, 63–69
   cache.ts94.67%100%100%94.29%139–141, 39
   completions.ts99.42%100%100%99.37%270
   group.ts99.50%100%97.44%100%
   output.ts99.29%100%95.65%99.61%80
   regex.ts99.39%100%100%99.34%329
   render.ts89.87%100%89.47%89.89%166, 190–195, 197–199, 201–202, 253–254, 275, 462–463, 486–488, 554–558, 570–571, 576–583, 585–593, 595–596
   upgrade.ts88.38%100%94.44%87.89%128, 131, 133, 153, 167–168, 188–195, 198–204, 209, 214, 250–253
src/render
   filter-match.ts97.44%100%92.31%100%
   filter.ts100%100%100%100%
   highlight.ts96.63%100%90.40%99.31%284–285
   rows.ts97.58%100%100%97.44%168, 54–55
   selection.ts100%100%100%100%
   summary.ts100%100%100%100%
   team-pick.ts100%100%100%100%
   terminal.ts100%100%100%100%

@shouze
shouze requested a lite review from Copilot August 23, 2026 15:56
@shouze shouze self-assigned this Aug 23, 2026
@shouze
shouze marked this pull request as ready for review August 23, 2026 15:56

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.sliceAnsi closes active ANSI state at the slice boundary, so appending \x1b[22;39m cannot preserve a caller-owned background: clipping a string beginning with \x1b[48;5;53m can 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 thread src/render/terminal.ts
Comment on lines +32 to +36
const width = visibleWidth(str);
if (width <= maxCols) {
return str;
}
const clipped = Bun.sliceAnsi(str, 0, maxCols);
Comment thread src/render/terminal.ts
* Correctly handles emoji, CJK characters, and multi-code-point grapheme clusters.
*/
export function visibleWidth(str: string): number {
return Bun.stringWidth(str);
Comment thread src/render/terminal.ts
Comment on lines +9 to +10
export function visibleWidth(str: string): number {
return Bun.stringWidth(str);
@shouze
shouze merged commit 1f454eb into main Aug 23, 2026
6 checks passed
@shouze
shouze deleted the feat/bun14-terminal/01-terminal branch August 23, 2026 16:10
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