function
bun.sliceAnsi
Slice a string by visible column width, preserving ANSI escape codes.
Like String.prototype.slice, but indices are terminal column widths (accounting for wide CJK characters, emoji grapheme clusters, and zero-width joiners), and ANSI escape sequences (SGR colors, OSC 8 hyperlinks, etc.) are preserved and correctly re-opened/closed at the slice boundaries.
The string to slice
Starting column (default 0). Negative counts from end.
Ending column, exclusive (default end of string). Negative counts from end.
Optional behavior flags (such as ellipsis for truncation)
ambiguousIsNarrow as a positional arg, usable when the 4th arg is an ellipsis string (or undefined). Lets you pass both options without an object: sliceAnsi(s, 0, n, "\u2026", false).
The sliced string with ANSI codes intact
import { sliceAnsi } from "bun";
// Plain slice (replaces the `slice-ansi` npm package)
sliceAnsi("hello", 1, 4); // "ell"
sliceAnsi("\u001b[31mhello\u001b[39m", 1, 4); // "\u001b[31mell\u001b[39m"
sliceAnsi("\u5b89\u5b81\u54c8", 0, 4); // "\u5b89\u5b81" (CJK: width 2 each)
// Truncation (replaces the `cli-truncate` npm package)
sliceAnsi("unicorn", 0, 4, "\u2026"); // "uni\u2026"
sliceAnsi("unicorn", -4, undefined, "\u2026"); // "\u2026orn"