@react-slate/core
Version:
Write interactive CLI apps with React
93 lines • 3.7 kB
JavaScript
;
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k];
result["default"] = mod;
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const colorette = __importStar(require("colorette"));
const fast_memoize_1 = __importDefault(require("fast-memoize"));
const colors_1 = require("./colors");
const utils_1 = require("../../utils");
const colorize = fast_memoize_1.default((color, isBackground, text) => {
if (color === 'initial') {
return colors_1.reset(text);
}
if (color.startsWith('raw')) {
const rawMatch = /raw\((.+)\)/.exec(color);
if (rawMatch) {
const [, rawColor] = rawMatch;
const openCode = isBackground ? 48 : 38;
const closeCode = openCode + 1;
return `${colors_1.CSI}${openCode};${rawColor}m${text}${colors_1.CSI}${closeCode}m`;
}
return text;
}
if (color.startsWith('#')) {
return isBackground
? colors_1.applyHexBackgroundColor(color, text)
: colors_1.applyHexColor(color, text);
}
else if (color.startsWith('rgb')) {
const rgbColorMatch = /rgb\((\d+),\s?(\d+),\s?(\d+)\)/.exec(color);
if (rgbColorMatch) {
const rgb = rgbColorMatch
.slice(1, 4)
.map((v) => parseInt(v, 10));
return isBackground
? colors_1.applyRgbBackgroundColor(rgb, text)
: colors_1.applyRgbColor(rgb, text);
}
const rgbKeywordMatch = /rgb\((.+)\)/.exec(color);
if (rgbKeywordMatch) {
return isBackground
? colors_1.applyKeywordBackgroundColor(rgbKeywordMatch[1].toLowerCase(), text)
: colors_1.applyKeywordColor(rgbKeywordMatch[1].toLowerCase(), text);
}
return text;
}
return isBackground
? colors_1.applyAnsiColor(`bg${utils_1.capitalize(color)}`, text)
: colors_1.applyAnsiColor(color, text);
});
const applyModifier = fast_memoize_1.default((modifier, text) => {
if (modifier in colorette) {
return colorette[modifier](text);
}
return text;
});
function applyStyle(style, text) {
if (process.env.NO_COLOR) {
return text;
}
const { color, bgColor, modifiers } = style;
let output = modifiers
? modifiers.reduce((acc, modifier) => applyModifier(modifier, acc), text)
: text;
// Special cases for color and background color, since `reset` will remove both color
// and background color, we need to change ordering depending on which one has `initial` value,
// so that we don't remove the other.
if (color === 'initial' && bgColor && bgColor !== 'initial') {
output = colorize(color, false, colorize(bgColor, true, output));
}
else if (bgColor === 'initial' && color && color !== 'initial') {
output = colorize(bgColor, true, colorize(color, false, output));
}
else if (bgColor && bgColor !== 'initial' && color && color !== 'initial') {
output = colorize(bgColor, true, colorize(color, false, output));
}
else if (bgColor) {
output = colorize(bgColor, true, output);
}
else if (color) {
output = colorize(color, false, output);
}
return colors_1.reset(output);
}
exports.default = applyStyle;
//# sourceMappingURL=applyStyle.js.map