perf: cache ANSI prefix regex in truncate instead of re-creating per call
This commit is contained in:
@@ -2,6 +2,7 @@ import type { ModuleSpec, RenderedModule } from "./types";
|
|||||||
|
|
||||||
// eslint-disable-next-line no-control-regex
|
// eslint-disable-next-line no-control-regex
|
||||||
const ANSI_RE = new RegExp("\\u001b\\[[0-9;]*m", "g");
|
const ANSI_RE = new RegExp("\\u001b\\[[0-9;]*m", "g");
|
||||||
|
const ANSI_PREFIX_RE = /^\u001b\[[0-9;]*m/;
|
||||||
|
|
||||||
export function visibleWidth(text: string): number {
|
export function visibleWidth(text: string): number {
|
||||||
return text.replace(ANSI_RE, "").length;
|
return text.replace(ANSI_RE, "").length;
|
||||||
@@ -16,8 +17,7 @@ export function truncate(text: string, width: number): string {
|
|||||||
let count = 0;
|
let count = 0;
|
||||||
for (let index = 0; index < text.length && count < width; index++) {
|
for (let index = 0; index < text.length && count < width; index++) {
|
||||||
if (text[index] === "\x1b") {
|
if (text[index] === "\x1b") {
|
||||||
// eslint-disable-next-line no-control-regex
|
const match = text.slice(index).match(ANSI_PREFIX_RE);
|
||||||
const match = text.slice(index).match(new RegExp("^\\u001b\\[[0-9;]*m"));
|
|
||||||
if (match) {
|
if (match) {
|
||||||
output += match[0];
|
output += match[0];
|
||||||
index += match[0].length - 1;
|
index += match[0].length - 1;
|
||||||
|
|||||||
Reference in New Issue
Block a user