@@ -241,10 +241,10 @@ function rgbToAnsi24Bit(r, g, b) {
241241 */
242242function styleText ( format , text , options ) {
243243 const validateStream = options ?. validateStream ?? true ;
244- const cache = getStyleCache ( ) ;
245244
246245 // Fast path: single format string with validateStream=false
247246 if ( ! validateStream && typeof format === 'string' && typeof text === 'string' ) {
247+ const cache = getStyleCache ( ) ;
248248 if ( format === 'none' ) return text ;
249249 const style = cache [ format ] ;
250250 if ( style !== undefined ) {
@@ -284,6 +284,7 @@ function styleText(format, text, options) {
284284 }
285285
286286 const formatArray = ArrayIsArray ( format ) ? format : [ format ] ;
287+ const colors = inspect . colors ;
287288
288289 let openCodes = '' ;
289290 let closeCodes = '' ;
@@ -293,30 +294,29 @@ function styleText(format, text, options) {
293294 if ( key === 'none' ) continue ;
294295
295296 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 ;
297+ if ( RegExpPrototypeExec ( hexColorRegExp , key ) === null ) {
298+ throw new ERR_INVALID_ARG_VALUE ( 'format' , key ,
299+ 'must be a valid hex color (#RGB or #RRGGBB)' ) ;
306300 }
307- openCodes += hexStyle . openSeq ;
308- closeCodes = hexStyle . closeSeq + closeCodes ;
309- processedText = replaceCloseCode ( processedText , hexStyle . closeSeq , hexStyle . openSeq , false ) ;
301+ if ( skipColorize ) continue ;
302+ const { 0 : r , 1 : g , 2 : b } = hexToRgb ( key ) ;
303+ const hexOpenSeq = kEscape + rgbToAnsi24Bit ( r , g , b ) + kEscapeEnd ;
304+ openCodes += hexOpenSeq ;
305+ closeCodes = kHexCloseSeq + closeCodes ;
306+ processedText = replaceCloseCode ( processedText , kHexCloseSeq , hexOpenSeq , false ) ;
310307 continue ;
311308 }
312309
313- const style = cache [ key ] ;
314- if ( style === undefined ) {
310+ const codes = colors [ key ] ;
311+ if ( codes === undefined ) {
315312 validateOneOf ( key , 'format' , ObjectGetOwnPropertyNames ( inspect . colors ) ) ;
316313 }
317- openCodes += style . openSeq ;
318- closeCodes = style . closeSeq + closeCodes ;
319- processedText = replaceCloseCode ( processedText , style . closeSeq , style . openSeq , style . keepClose ) ;
314+ const openSeq = kEscape + codes [ 0 ] + kEscapeEnd ;
315+ const closeSeq = kEscape + codes [ 1 ] + kEscapeEnd ;
316+ const keepClose = codes [ 0 ] === kDimCode || codes [ 0 ] === kBoldCode ;
317+ openCodes += openSeq ;
318+ closeCodes = closeSeq + closeCodes ;
319+ processedText = replaceCloseCode ( processedText , closeSeq , openSeq , keepClose ) ;
320320 }
321321
322322 if ( skipColorize ) return text ;
0 commit comments