function

bun.stringWidth

function stringWidth(
input: string,
): number;

Get the column count of a string as it would be displayed in a terminal. Supports ANSI escape codes, emoji, and wide characters.

This API is designed to match the string-width npm package, so existing code can be ported in either direction.

@param input

The string to measure

@returns

The width of the string in columns

import { stringWidth } from "bun";

console.log(stringWidth("abc")); // 3
console.log(stringWidth("πŸ‘©β€πŸ‘©β€πŸ‘§β€πŸ‘¦")); // 1
console.log(stringWidth("\u001b[31mhello\u001b[39m")); // 5
console.log(stringWidth("\u001b[31mhello\u001b[39m", { countAnsiEscapeCodes: false })); // 5
console.log(stringWidth("\u001b[31mhello\u001b[39m", { countAnsiEscapeCodes: true })); // 13