70 lines
1.7 KiB
JavaScript
Executable File
70 lines
1.7 KiB
JavaScript
Executable File
import chalk from 'chalk';
|
|
let mode = 'chalk';
|
|
export function setMode(newMode) {
|
|
mode = newMode;
|
|
}
|
|
function withLevels(levels, strategy) {
|
|
const fn = str => strategy(levels, str);
|
|
Object.assign(fn, generateColoredBody(fn, levels, strategy));
|
|
return fn;
|
|
}
|
|
const allColors = [
|
|
'black',
|
|
'red',
|
|
'green',
|
|
'yellow',
|
|
'blue',
|
|
'magenta',
|
|
'cyan',
|
|
'white',
|
|
'gray',
|
|
'grey',
|
|
'blackBright',
|
|
'redBright',
|
|
'greenBright',
|
|
'yellowBright',
|
|
'blueBright',
|
|
'magentaBright',
|
|
'cyanBright',
|
|
'whiteBright',
|
|
'italic',
|
|
'bold',
|
|
'underline',
|
|
];
|
|
function generateColoredBody(wrapper, levels, strategy) {
|
|
const c = allColors.reduce((acc, curr) => {
|
|
Object.defineProperty(acc, curr, {
|
|
get() {
|
|
const x = withLevels([...levels, curr], strategy);
|
|
return x;
|
|
},
|
|
});
|
|
return acc;
|
|
}, wrapper);
|
|
return c;
|
|
}
|
|
const chalked = generateColoredBody({}, [], (levels, str) => {
|
|
const color = levels.reduce((c, curr) => c[curr], chalk);
|
|
return color(str);
|
|
});
|
|
const tagged = generateColoredBody({}, [], (levels, str) => {
|
|
const [start, end] = levels.reduce((acc, curr) => {
|
|
acc[0] += `<${curr}>`;
|
|
acc[1] = `</${curr}>${acc[1]}`;
|
|
return acc;
|
|
}, ['', '']);
|
|
return `${start}${str}${end}`;
|
|
});
|
|
const disabled = generateColoredBody({}, [], (_levels, str) => str);
|
|
export function colored() {
|
|
if (mode === 'chalk')
|
|
return chalked;
|
|
if (mode === 'disabled')
|
|
return disabled;
|
|
return tagged;
|
|
}
|
|
setMode('tags');
|
|
console.log(colored().red.bold('hello'));
|
|
setMode('chalk');
|
|
console.log(colored().red.bold('hello'));
|
|
//# sourceMappingURL=chalk.js.map
|