colors-option
Version:
Let users toggle colors
36 lines (19 loc) • 589 B
JavaScript
import{Chalk}from"chalk";
import{getOpts}from"./options.js";
const colorsOption=(opts)=>{
const{colors,stream,chalkOpts}=getOpts(opts);
const level=getLevel(colors,stream);
const chalk=new Chalk({...chalkOpts,level});
return chalk
};
export default colorsOption;
const getLevel=(colors,stream)=>{
if(colors===false){
return 0
}
const terminalLevel=getTerminalLevel(stream);
return colors===undefined?terminalLevel:Math.max(terminalLevel,1)
};
const getTerminalLevel=(stream)=>
stream.isTTY?DEPTH_TO_LEVEL[stream.getColorDepth()]:0;
const DEPTH_TO_LEVEL={1:0,4:1,8:2,24:3};