| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent d5be94e commit 38758a7
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -129,21 +129,24 @@ function getHexStyleCache() { | |||
| 129 | 129 | return hexStyleCache; | |
| 130 | 130 | } | |
| 131 | 131 | ||
| 132 | + function codesToStyle(codes) { | ||
| 133 | + const openNum = codes[0]; | ||
| 134 | + return { | ||
| 135 | + __proto__: null, | ||
| 136 | + openSeq: kEscape + openNum + kEscapeEnd, | ||
| 137 | + closeSeq: kEscape + codes[1] + kEscapeEnd, | ||
| 138 | + keepClose: openNum === kDimCode || openNum === kBoldCode, | ||
| 139 | + }; | ||
| 140 | + } | ||
| 141 | + | ||
| 132 | 142 | function getStyleCache() { | |
| 133 | 143 | if (styleCache === undefined) { | |
| 134 | 144 | styleCache = { __proto__: null }; | |
| 135 | 145 | const colors = inspect.colors; | |
| 136 | 146 | for (const key of ObjectGetOwnPropertyNames(colors)) { | |
| 137 | 147 | const codes = colors[key]; | |
| 138 | 148 | if (codes) { | |
| 139 | - const openNum = codes[0]; | ||
| 140 | - const closeNum = codes[1]; | ||
| 141 | - styleCache[key] = { | ||
| 142 | - __proto__: null, | ||
| 143 | - openSeq: kEscape + openNum + kEscapeEnd, | ||
| 144 | - closeSeq: kEscape + closeNum + kEscapeEnd, | ||
| 145 | - keepClose: openNum === kDimCode || openNum === kBoldCode, | ||
| 146 | - }; | ||
| 149 | + styleCache[key] = codesToStyle(codes); | ||
| 147 | 150 | } | |
| 148 | 151 | } | |
| 149 | 152 | } | |
@@ -241,10 +244,10 @@ function rgbToAnsi24Bit(r, g, b) { | |||
| 241 | 244 | */ | |
| 242 | 245 | function styleText(format, text, options) { | |
| 243 | 246 | const validateStream = options?.validateStream ?? true; | |
| 244 | - const cache = getStyleCache(); | ||
| 245 | 247 | ||
| 246 | 248 | // Fast path: single format string with validateStream=false | |
| 247 | 249 | if (!validateStream && typeof format === 'string' && typeof text === 'string') { | |
| 250 | + const cache = getStyleCache(); | ||
| 248 | 251 | if (format === 'none') return text; | |
| 249 | 252 | const style = cache[format]; | |
| 250 | 253 | if (style !== undefined) { | |
@@ -284,6 +287,7 @@ function styleText(format, text, options) { | |||
| 284 | 287 | } | |
| 285 | 288 | ||
| 286 | 289 | const formatArray = ArrayIsArray(format) ? format : [format]; | |
| 290 | + const colors = inspect.colors; | ||
| 287 | 291 | ||
| 288 | 292 | let openCodes = ''; | |
| 289 | 293 | let closeCodes = ''; | |
@@ -293,30 +297,27 @@ function styleText(format, text, options) { | |||
| 293 | 297 | if (key === 'none') continue; | |
| 294 | 298 | ||
| 295 | 299 | if (typeof key === 'string' && key[0] === '#') { | |
| 296 | - let hexStyle = getHexStyleCache().get(key); | ||
| 297 | - if (hexStyle === undefined) { | ||
| 298 | - if (RegExpPrototypeExec(hexColorRegExp, key) === null) { | ||
| 299 | - throw new ERR_INVALID_ARG_VALUE('format', key, | ||
| 300 | - 'must be a valid hex color (#RGB or #RRGGBB)'); | ||
| 301 | - } | ||
| 302 | - if (skipColorize) continue; | ||
| 303 | - hexStyle = getHexStyle(key); | ||
| 304 | - } else if (skipColorize) { | ||
| 305 | - continue; | ||
| 300 | + if (RegExpPrototypeExec(hexColorRegExp, key) === null) { | ||
| 301 | + throw new ERR_INVALID_ARG_VALUE('format', key, | ||
| 302 | + 'must be a valid hex color (#RGB or #RRGGBB)'); | ||
| 306 | 303 | } | |
| 307 | - openCodes += hexStyle.openSeq; | ||
| 308 | - closeCodes = hexStyle.closeSeq + closeCodes; | ||
| 309 | - processedText = replaceCloseCode(processedText, hexStyle.closeSeq, hexStyle.openSeq, false); | ||
| 304 | + if (skipColorize) continue; | ||
| 305 | + const { 0: r, 1: g, 2: b } = hexToRgb(key); | ||
| 306 | + const hexOpenSeq = kEscape + rgbToAnsi24Bit(r, g, b) + kEscapeEnd; | ||
| 307 | + openCodes += hexOpenSeq; | ||
| 308 | + closeCodes = kHexCloseSeq + closeCodes; | ||
| 309 | + processedText = replaceCloseCode(processedText, kHexCloseSeq, hexOpenSeq, false); | ||
| 310 | 310 | continue; | |
| 311 | 311 | } | |
| 312 | 312 | ||
| 313 | - const style = cache[key]; | ||
| 314 | - if (style === undefined) { | ||
| 313 | + const codes = colors[key]; | ||
| 314 | + if (!codes) { | ||
| 315 | 315 | validateOneOf(key, 'format', ObjectGetOwnPropertyNames(inspect.colors)); | |
| 316 | 316 | } | |
| 317 | - openCodes += style.openSeq; | ||
| 318 | - closeCodes = style.closeSeq + closeCodes; | ||
| 319 | - processedText = replaceCloseCode(processedText, style.closeSeq, style.openSeq, style.keepClose); | ||
| 317 | + const { openSeq, closeSeq, keepClose } = codesToStyle(codes); | ||
| 318 | + openCodes += openSeq; | ||
| 319 | + closeCodes = closeSeq + closeCodes; | ||
| 320 | + processedText = replaceCloseCode(processedText, closeSeq, openSeq, keepClose); | ||
| 320 | 321 | } | |
| 321 | 322 | ||
| 322 | 323 | if (skipColorize) return text; | |
| Back | FazBrowse Home | New Git URL |
0 commit comments