import ansiColors from 'ansi-colors' ; interface ColorStyle { modifier ? : string ; color : string ; background ? : boolean ; bright ? : boolean ; symbol ? : string ; } interface Style { icon : string ; render : ansiColors . StyleFunction ; } const COLOR = { PREFIX : 'bg' , SUFFIX : 'Bright' } ; function getStyle ( colorStyle = { color : 'white' } ) { const color = colorStyle . color . toLowerCase ( ) ; let fn = colorStyle . background ? COLOR . PREFIX + color . replace ( /^[a-z]/ , ( str ) => str . toUpperCase ( ) ) : color ; if ( colorStyle . bright ) { fn += COLOR . SUFFIX ; } const colors = ansiColors ; const render = colorStyle . modifier ? colors [ colorStyle . modifier ] [ fn ] : colors [ fn ] ; const symbols = ansiColors . symbols ; const icon = colorStyle . symbol && symbols [ colorStyle . symbol ] ? `${ symbols [ colorStyle . symbol ] } ` : '' ; return { icon , render } ; } function color ( label , colorStyle ? ) { const result = getStyle ( colorStyle ) ; return result . render ( `${ result . icon }${ label }` ) ; } export default color ;