Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 28 additions & 27 deletions lib/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,21 +129,24 @@ function getHexStyleCache() {
return hexStyleCache;
}

function codesToStyle(codes) {
const openNum = codes[0];
return {
__proto__: null,
openSeq: kEscape + openNum + kEscapeEnd,
closeSeq: kEscape + codes[1] + kEscapeEnd,
keepClose: openNum === kDimCode || openNum === kBoldCode,
};
}

function getStyleCache() {
if (styleCache === undefined) {
styleCache = { __proto__: null };
const colors = inspect.colors;
for (const key of ObjectGetOwnPropertyNames(colors)) {
const codes = colors[key];
if (codes) {
const openNum = codes[0];
const closeNum = codes[1];
styleCache[key] = {
__proto__: null,
openSeq: kEscape + openNum + kEscapeEnd,
closeSeq: kEscape + closeNum + kEscapeEnd,
keepClose: openNum === kDimCode || openNum === kBoldCode,
};
styleCache[key] = codesToStyle(codes);
}
}
}
Expand Down Expand Up @@ -241,10 +244,10 @@ function rgbToAnsi24Bit(r, g, b) {
*/
function styleText(format, text, options) {
const validateStream = options?.validateStream ?? true;
const cache = getStyleCache();

// Fast path: single format string with validateStream=false
if (!validateStream && typeof format === 'string' && typeof text === 'string') {
const cache = getStyleCache();
if (format === 'none') return text;
const style = cache[format];
if (style !== undefined) {
Expand Down Expand Up @@ -284,6 +287,7 @@ function styleText(format, text, options) {
}

const formatArray = ArrayIsArray(format) ? format : [format];
const colors = inspect.colors;

let openCodes = '';
let closeCodes = '';
Expand All @@ -293,30 +297,27 @@ function styleText(format, text, options) {
if (key === 'none') continue;

if (typeof key === 'string' && key[0] === '#') {
let hexStyle = getHexStyleCache().get(key);
if (hexStyle === undefined) {
if (RegExpPrototypeExec(hexColorRegExp, key) === null) {
throw new ERR_INVALID_ARG_VALUE('format', key,
'must be a valid hex color (#RGB or #RRGGBB)');
}
if (skipColorize) continue;
hexStyle = getHexStyle(key);
} else if (skipColorize) {
continue;
if (RegExpPrototypeExec(hexColorRegExp, key) === null) {
throw new ERR_INVALID_ARG_VALUE('format', key,
'must be a valid hex color (#RGB or #RRGGBB)');
}
openCodes += hexStyle.openSeq;
closeCodes = hexStyle.closeSeq + closeCodes;
processedText = replaceCloseCode(processedText, hexStyle.closeSeq, hexStyle.openSeq, false);
if (skipColorize) continue;
const { 0: r, 1: g, 2: b } = hexToRgb(key);
const hexOpenSeq = kEscape + rgbToAnsi24Bit(r, g, b) + kEscapeEnd;
openCodes += hexOpenSeq;
closeCodes = kHexCloseSeq + closeCodes;
processedText = replaceCloseCode(processedText, kHexCloseSeq, hexOpenSeq, false);
Comment thread
araujogui marked this conversation as resolved.
continue;
}

const style = cache[key];
if (style === undefined) {
const codes = colors[key];
if (!codes) {
validateOneOf(key, 'format', ObjectGetOwnPropertyNames(inspect.colors));
}
openCodes += style.openSeq;
closeCodes = style.closeSeq + closeCodes;
processedText = replaceCloseCode(processedText, style.closeSeq, style.openSeq, style.keepClose);
const { openSeq, closeSeq, keepClose } = codesToStyle(codes);
openCodes += openSeq;
closeCodes = closeSeq + closeCodes;
processedText = replaceCloseCode(processedText, closeSeq, openSeq, keepClose);
}

if (skipColorize) return text;
Expand Down
Loading