function

bun.wrapAnsi

function wrapAnsi(
input: string,
columns: number,
options?: WrapAnsiOptions
): string;

Wrap a string to fit within the specified column width, preserving ANSI escape codes.

Designed to be compatible with the wrap-ansi npm package.

Features:

  • Preserves ANSI escape codes (colors, styles) across line breaks
  • Supports SGR codes (colors, bold, italic, etc.) and OSC 8 hyperlinks
  • Respects Unicode display widths (full-width characters, emoji)
  • Word wrapping at word boundaries (configurable)
@param input

The string to wrap

@param columns

The maximum column width

@param options

Wrapping options

@returns

The wrapped string

import { wrapAnsi } from "bun";

console.log(wrapAnsi("hello world", 5));
// Output:
// hello
// world

// Preserves ANSI colors across line breaks
console.log(wrapAnsi("\u001b[31mhello world\u001b[0m", 5));
// Output:
// \u001b[31mhello\u001b[0m
// \u001b[31mworld\u001b[0m

// Hard wrap long words
console.log(wrapAnsi("abcdefghij", 3, { hard: true }));
// Output:
// abc
// def
// ghi
// j