import fastMemoize from 'fast-memoize' ; import colorette from 'colorette' ; import ansiStyles from 'ansi-styles' ; import * as supportsColor from 'supports-color' ; import colorConvert from 'color-convert' ; import { KEYWORD } from 'color-convert/conversions' ; colorette . options . enabled = process . env . CI ? true : colorette . options . enabled ; export const CSI = '\u001b[' ; export const reset = fastMemoize ( ( text ) => text ? `${ CSI }0m${ text }${ CSI }0m` : text ) ; export function applyAnsiColor ( color , text ) { if ( color in colorette ) { return ( ( colorette as unknown ) as { [ key ] : ( text : string ) => string ; } ) [ color ] ( text ) ; } return text ; } export function applyRgbColor ( [ r , g , b ] , text , level = supportsColor . stdout . level ) { switch ( level ) { case 1 : return `${ ansiStyles . color . ansi . rgb ( r , g , b ) }${ text }${ ansiStyles . color . close }` ; case 2 : return `${ ansiStyles . color . ansi256 . rgb ( r , g , b ) }${ text }${ ansiStyles . color . close }` ; case 3 : return `${ ansiStyles . color . ansi16m . rgb ( r , g , b ) }${ text }${ ansiStyles . color . close }` ; default : return text ; } } export function applyRgbBackgroundColor ( [ r , g , b ] , text , level = supportsColor . stdout . level ) { switch ( level ) { case 1 : return `${ ansiStyles . bgColor . ansi . rgb ( r , g , b ) }${ text }${ ansiStyles . bgColor . close }` ; case 2 : return `${ ansiStyles . bgColor . ansi256 . rgb ( r , g , b ) }${ text }${ ansiStyles . bgColor . close }` ; case 3 : return `${ ansiStyles . bgColor . ansi16m . rgb ( r , g , b ) }${ text }${ ansiStyles . bgColor . close }` ; default : return text ; } } export function applyHexColor ( color , text , level = supportsColor . stdout . level ) { return applyRgbColor ( colorConvert . hex . rgb ( color ) , text , level ) ; } export function applyHexBackgroundColor ( color , text , level = supportsColor . stdout . level ) { return applyRgbBackgroundColor ( colorConvert . hex . rgb ( color ) , text , level ) ; } export function applyKeywordColor ( color , text , level = supportsColor . stdout . level ) { try { return applyRgbColor ( colorConvert . keyword . rgb ( ( color as unknown ) as KEYWORD ) , text , level ) ; } catch ( error ) { return text ; } } export function applyKeywordBackgroundColor ( color , text , level = supportsColor . stdout . level ) { try { return applyRgbBackgroundColor ( colorConvert . keyword . rgb ( ( color as unknown ) as KEYWORD ) , text , level ) ; } catch ( error ) { return text ; } }