export const ESC = String.fromCharCode(27); const ANSI_REGEX = new RegExp(`${ESC}\\[[0-9;]*m`, "g"); export const ANSI_SPLIT = new RegExp(`(${ESC}\\[[0-9;]*m)`); export function stripAnsi(str: string): string { return str.replace(ANSI_REGEX, ""); } // Combining marks, zero-width spaces/joiners and variation selectors. const ZERO_WIDTH: ReadonlyArray = [ [0x0300, 0x036f], [0x200b, 0x200f], [0x2060, 0x2064], [0x20d0, 0x20ff], [0xfe00, 0xfe0f], [0xfe20, 0xfe2f], [0xe0100, 0xe01ef], ]; // East Asian Wide and Fullwidth ranges (CJK, Hangul, fullwidth forms) plus // the emoji that terminals draw two columns wide. const WIDE: ReadonlyArray = [ [0x1100, 0x115f], [0x231a, 0x231b], [0x2329, 0x232a], [0x23e9, 0x23ec], [0x23f0, 0x23f0], [0x23f3, 0x23f3], [0x25fd, 0x25fe], [0x2614, 0x2615], [0x2648, 0x2653], [0x267f, 0x267f], [0x2693, 0x2693], [0x26a1, 0x26a1], [0x26aa, 0x26ab], [0x26bd, 0x26be], [0x26c4, 0x26c5], [0x26ce, 0x26ce], [0x26d4, 0x26d4], [0x26ea, 0x26ea], [0x26f2, 0x26f3], [0x26f5, 0x26f5], [0x26fa, 0x26fa], [0x26fd, 0x26fd], [0x2705, 0x2705], [0x270a, 0x270b], [0x2728, 0x2728], [0x274c, 0x274c], [0x274e, 0x274e], [0x2753, 0x2755], [0x2757, 0x2757], [0x2795, 0x2797], [0x27b0, 0x27b0], [0x27bf, 0x27bf], [0x2b1b, 0x2b1c], [0x2b50, 0x2b50], [0x2b55, 0x2b55], [0x2e80, 0x303e], [0x3041, 0x33ff], [0x3400, 0x4dbf], [0x4e00, 0x9fff], [0xa000, 0xa4cf], [0xa960, 0xa97f], [0xac00, 0xd7a3], [0xf900, 0xfaff], [0xfe10, 0xfe19], [0xfe30, 0xfe6f], [0xff00, 0xff60], [0xffe0, 0xffe6], [0x16fe0, 0x16fe4], [0x17000, 0x18aff], [0x1b000, 0x1b2ff], [0x1f004, 0x1f004], [0x1f0cf, 0x1f0cf], [0x1f18e, 0x1f18e], [0x1f191, 0x1f19a], [0x1f200, 0x1f251], [0x1f300, 0x1f64f], [0x1f680, 0x1f6ff], [0x1f7e0, 0x1f7eb], [0x1f90c, 0x1f9ff], [0x1fa70, 0x1faff], [0x20000, 0x3fffd], ]; function inRanges( cp: number, ranges: ReadonlyArray, ): boolean { return ranges.some(([lo, hi]) => cp >= lo && cp <= hi); } /** Terminal columns taken by a single code point. */ export function charWidth(char: string): number { const cp = char.codePointAt(0) ?? 0; if (cp < 0x0300) return 1; if (inRanges(cp, ZERO_WIDTH)) return 0; return inRanges(cp, WIDE) ? 2 : 1; } /** Terminal columns taken by `str`, ignoring ANSI color codes. */ export function visibleLength(str: string): number { let width = 0; for (const char of stripAnsi(str)) { width += charWidth(char); } return width; }