Fix crash on Unicode format characters in visibleWidth

Strip all Unicode format characters (category Cf) before passing to
string-width. These are invisible control characters that crash
string-width but have no visible width anyway.

Closes #390
This commit is contained in:
Mario Zechner 2026-01-01 22:33:09 +01:00
parent 7f0cd8bcb5
commit 1d9fa13d58

View file

@ -5,7 +5,8 @@ import stringWidth from "string-width";
*/
export function visibleWidth(str: string): number {
if (!str) return 0;
const normalized = str.replace(/\t/g, " ");
// Replace tabs and strip Unicode format characters (Cf) that crash string-width
const normalized = str.replace(/\t/g, " ").replace(/\p{Cf}/gu, "");
return stringWidth(normalized);
}