From 2e7291347ef224fc74561871ec41a44bbe857624 Mon Sep 17 00:00:00 2001 From: Pooya Parsa Date: Wed, 28 Jun 2023 11:15:23 +0200 Subject: [PATCH] fix(fancy): add node 14 compatibility (resolves #204) --- src/reporters/fancy.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/reporters/fancy.ts b/src/reporters/fancy.ts index ae9e19ce..9a60642c 100644 --- a/src/reporters/fancy.ts +++ b/src/reporters/fancy.ts @@ -1,10 +1,11 @@ -import stringWidth from "string-width"; +import _stringWidth from "string-width"; import isUnicodeSupported from "is-unicode-supported"; import { colors } from "../utils/color"; import { parseStack } from "../utils/error"; import { FormatOptions, LogObject } from "../types"; import { LogLevel, LogType } from "../constants"; import { BoxOpts, box } from "../utils/box"; +import { stripAnsi } from "../utils"; import { BasicReporter } from "./basic"; export const TYPE_COLOR_MAP: { [k in LogType]?: string } = { @@ -36,6 +37,14 @@ const TYPE_ICONS: { [k in LogType]?: string } = { log: "", }; +function stringWidth(str: string) { + // https://github.com/unjs/consola/issues/204 + if (!Intl.Segmenter) { + return stripAnsi(str).length; + } + return _stringWidth(str); +} + export class FancyReporter extends BasicReporter { formatStack(stack: string) { return (