diff --git a/render.ts b/render.ts index 97c055d..77f1b64 100644 --- a/render.ts +++ b/render.ts @@ -2,6 +2,7 @@ import type { ModuleSpec, RenderedModule } from "./types"; // eslint-disable-next-line no-control-regex const ANSI_RE = new RegExp("\\u001b\\[[0-9;]*m", "g"); +const ANSI_PREFIX_RE = /^\u001b\[[0-9;]*m/; export function visibleWidth(text: string): number { return text.replace(ANSI_RE, "").length; @@ -16,8 +17,7 @@ export function truncate(text: string, width: number): string { let count = 0; for (let index = 0; index < text.length && count < width; index++) { if (text[index] === "\x1b") { - // eslint-disable-next-line no-control-regex - const match = text.slice(index).match(new RegExp("^\\u001b\\[[0-9;]*m")); + const match = text.slice(index).match(ANSI_PREFIX_RE); if (match) { output += match[0]; index += match[0].length - 1;