11cb0ef41Sopenharmony_ciimport ansiStyles from '#ansi-styles'; 21cb0ef41Sopenharmony_ciimport supportsColor from '#supports-color'; 31cb0ef41Sopenharmony_ciimport { // eslint-disable-line import/order 41cb0ef41Sopenharmony_ci stringReplaceAll, 51cb0ef41Sopenharmony_ci stringEncaseCRLFWithFirstIndex, 61cb0ef41Sopenharmony_ci} from './utilities.js'; 71cb0ef41Sopenharmony_ci 81cb0ef41Sopenharmony_ciconst {stdout: stdoutColor, stderr: stderrColor} = supportsColor; 91cb0ef41Sopenharmony_ci 101cb0ef41Sopenharmony_ciconst GENERATOR = Symbol('GENERATOR'); 111cb0ef41Sopenharmony_ciconst STYLER = Symbol('STYLER'); 121cb0ef41Sopenharmony_ciconst IS_EMPTY = Symbol('IS_EMPTY'); 131cb0ef41Sopenharmony_ci 141cb0ef41Sopenharmony_ci// `supportsColor.level` → `ansiStyles.color[name]` mapping 151cb0ef41Sopenharmony_ciconst levelMapping = [ 161cb0ef41Sopenharmony_ci 'ansi', 171cb0ef41Sopenharmony_ci 'ansi', 181cb0ef41Sopenharmony_ci 'ansi256', 191cb0ef41Sopenharmony_ci 'ansi16m', 201cb0ef41Sopenharmony_ci]; 211cb0ef41Sopenharmony_ci 221cb0ef41Sopenharmony_ciconst styles = Object.create(null); 231cb0ef41Sopenharmony_ci 241cb0ef41Sopenharmony_ciconst applyOptions = (object, options = {}) => { 251cb0ef41Sopenharmony_ci if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) { 261cb0ef41Sopenharmony_ci throw new Error('The `level` option should be an integer from 0 to 3'); 271cb0ef41Sopenharmony_ci } 281cb0ef41Sopenharmony_ci 291cb0ef41Sopenharmony_ci // Detect level if not set manually 301cb0ef41Sopenharmony_ci const colorLevel = stdoutColor ? stdoutColor.level : 0; 311cb0ef41Sopenharmony_ci object.level = options.level === undefined ? colorLevel : options.level; 321cb0ef41Sopenharmony_ci}; 331cb0ef41Sopenharmony_ci 341cb0ef41Sopenharmony_ciexport class Chalk { 351cb0ef41Sopenharmony_ci constructor(options) { 361cb0ef41Sopenharmony_ci // eslint-disable-next-line no-constructor-return 371cb0ef41Sopenharmony_ci return chalkFactory(options); 381cb0ef41Sopenharmony_ci } 391cb0ef41Sopenharmony_ci} 401cb0ef41Sopenharmony_ci 411cb0ef41Sopenharmony_ciconst chalkFactory = options => { 421cb0ef41Sopenharmony_ci const chalk = (...strings) => strings.join(' '); 431cb0ef41Sopenharmony_ci applyOptions(chalk, options); 441cb0ef41Sopenharmony_ci 451cb0ef41Sopenharmony_ci Object.setPrototypeOf(chalk, createChalk.prototype); 461cb0ef41Sopenharmony_ci 471cb0ef41Sopenharmony_ci return chalk; 481cb0ef41Sopenharmony_ci}; 491cb0ef41Sopenharmony_ci 501cb0ef41Sopenharmony_cifunction createChalk(options) { 511cb0ef41Sopenharmony_ci return chalkFactory(options); 521cb0ef41Sopenharmony_ci} 531cb0ef41Sopenharmony_ci 541cb0ef41Sopenharmony_ciObject.setPrototypeOf(createChalk.prototype, Function.prototype); 551cb0ef41Sopenharmony_ci 561cb0ef41Sopenharmony_cifor (const [styleName, style] of Object.entries(ansiStyles)) { 571cb0ef41Sopenharmony_ci styles[styleName] = { 581cb0ef41Sopenharmony_ci get() { 591cb0ef41Sopenharmony_ci const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]); 601cb0ef41Sopenharmony_ci Object.defineProperty(this, styleName, {value: builder}); 611cb0ef41Sopenharmony_ci return builder; 621cb0ef41Sopenharmony_ci }, 631cb0ef41Sopenharmony_ci }; 641cb0ef41Sopenharmony_ci} 651cb0ef41Sopenharmony_ci 661cb0ef41Sopenharmony_cistyles.visible = { 671cb0ef41Sopenharmony_ci get() { 681cb0ef41Sopenharmony_ci const builder = createBuilder(this, this[STYLER], true); 691cb0ef41Sopenharmony_ci Object.defineProperty(this, 'visible', {value: builder}); 701cb0ef41Sopenharmony_ci return builder; 711cb0ef41Sopenharmony_ci }, 721cb0ef41Sopenharmony_ci}; 731cb0ef41Sopenharmony_ci 741cb0ef41Sopenharmony_ciconst getModelAnsi = (model, level, type, ...arguments_) => { 751cb0ef41Sopenharmony_ci if (model === 'rgb') { 761cb0ef41Sopenharmony_ci if (level === 'ansi16m') { 771cb0ef41Sopenharmony_ci return ansiStyles[type].ansi16m(...arguments_); 781cb0ef41Sopenharmony_ci } 791cb0ef41Sopenharmony_ci 801cb0ef41Sopenharmony_ci if (level === 'ansi256') { 811cb0ef41Sopenharmony_ci return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_)); 821cb0ef41Sopenharmony_ci } 831cb0ef41Sopenharmony_ci 841cb0ef41Sopenharmony_ci return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_)); 851cb0ef41Sopenharmony_ci } 861cb0ef41Sopenharmony_ci 871cb0ef41Sopenharmony_ci if (model === 'hex') { 881cb0ef41Sopenharmony_ci return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_)); 891cb0ef41Sopenharmony_ci } 901cb0ef41Sopenharmony_ci 911cb0ef41Sopenharmony_ci return ansiStyles[type][model](...arguments_); 921cb0ef41Sopenharmony_ci}; 931cb0ef41Sopenharmony_ci 941cb0ef41Sopenharmony_ciconst usedModels = ['rgb', 'hex', 'ansi256']; 951cb0ef41Sopenharmony_ci 961cb0ef41Sopenharmony_cifor (const model of usedModels) { 971cb0ef41Sopenharmony_ci styles[model] = { 981cb0ef41Sopenharmony_ci get() { 991cb0ef41Sopenharmony_ci const {level} = this; 1001cb0ef41Sopenharmony_ci return function (...arguments_) { 1011cb0ef41Sopenharmony_ci const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]); 1021cb0ef41Sopenharmony_ci return createBuilder(this, styler, this[IS_EMPTY]); 1031cb0ef41Sopenharmony_ci }; 1041cb0ef41Sopenharmony_ci }, 1051cb0ef41Sopenharmony_ci }; 1061cb0ef41Sopenharmony_ci 1071cb0ef41Sopenharmony_ci const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1); 1081cb0ef41Sopenharmony_ci styles[bgModel] = { 1091cb0ef41Sopenharmony_ci get() { 1101cb0ef41Sopenharmony_ci const {level} = this; 1111cb0ef41Sopenharmony_ci return function (...arguments_) { 1121cb0ef41Sopenharmony_ci const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]); 1131cb0ef41Sopenharmony_ci return createBuilder(this, styler, this[IS_EMPTY]); 1141cb0ef41Sopenharmony_ci }; 1151cb0ef41Sopenharmony_ci }, 1161cb0ef41Sopenharmony_ci }; 1171cb0ef41Sopenharmony_ci} 1181cb0ef41Sopenharmony_ci 1191cb0ef41Sopenharmony_ciconst proto = Object.defineProperties(() => {}, { 1201cb0ef41Sopenharmony_ci ...styles, 1211cb0ef41Sopenharmony_ci level: { 1221cb0ef41Sopenharmony_ci enumerable: true, 1231cb0ef41Sopenharmony_ci get() { 1241cb0ef41Sopenharmony_ci return this[GENERATOR].level; 1251cb0ef41Sopenharmony_ci }, 1261cb0ef41Sopenharmony_ci set(level) { 1271cb0ef41Sopenharmony_ci this[GENERATOR].level = level; 1281cb0ef41Sopenharmony_ci }, 1291cb0ef41Sopenharmony_ci }, 1301cb0ef41Sopenharmony_ci}); 1311cb0ef41Sopenharmony_ci 1321cb0ef41Sopenharmony_ciconst createStyler = (open, close, parent) => { 1331cb0ef41Sopenharmony_ci let openAll; 1341cb0ef41Sopenharmony_ci let closeAll; 1351cb0ef41Sopenharmony_ci if (parent === undefined) { 1361cb0ef41Sopenharmony_ci openAll = open; 1371cb0ef41Sopenharmony_ci closeAll = close; 1381cb0ef41Sopenharmony_ci } else { 1391cb0ef41Sopenharmony_ci openAll = parent.openAll + open; 1401cb0ef41Sopenharmony_ci closeAll = close + parent.closeAll; 1411cb0ef41Sopenharmony_ci } 1421cb0ef41Sopenharmony_ci 1431cb0ef41Sopenharmony_ci return { 1441cb0ef41Sopenharmony_ci open, 1451cb0ef41Sopenharmony_ci close, 1461cb0ef41Sopenharmony_ci openAll, 1471cb0ef41Sopenharmony_ci closeAll, 1481cb0ef41Sopenharmony_ci parent, 1491cb0ef41Sopenharmony_ci }; 1501cb0ef41Sopenharmony_ci}; 1511cb0ef41Sopenharmony_ci 1521cb0ef41Sopenharmony_ciconst createBuilder = (self, _styler, _isEmpty) => { 1531cb0ef41Sopenharmony_ci // Single argument is hot path, implicit coercion is faster than anything 1541cb0ef41Sopenharmony_ci // eslint-disable-next-line no-implicit-coercion 1551cb0ef41Sopenharmony_ci const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' ')); 1561cb0ef41Sopenharmony_ci 1571cb0ef41Sopenharmony_ci // We alter the prototype because we must return a function, but there is 1581cb0ef41Sopenharmony_ci // no way to create a function with a different prototype 1591cb0ef41Sopenharmony_ci Object.setPrototypeOf(builder, proto); 1601cb0ef41Sopenharmony_ci 1611cb0ef41Sopenharmony_ci builder[GENERATOR] = self; 1621cb0ef41Sopenharmony_ci builder[STYLER] = _styler; 1631cb0ef41Sopenharmony_ci builder[IS_EMPTY] = _isEmpty; 1641cb0ef41Sopenharmony_ci 1651cb0ef41Sopenharmony_ci return builder; 1661cb0ef41Sopenharmony_ci}; 1671cb0ef41Sopenharmony_ci 1681cb0ef41Sopenharmony_ciconst applyStyle = (self, string) => { 1691cb0ef41Sopenharmony_ci if (self.level <= 0 || !string) { 1701cb0ef41Sopenharmony_ci return self[IS_EMPTY] ? '' : string; 1711cb0ef41Sopenharmony_ci } 1721cb0ef41Sopenharmony_ci 1731cb0ef41Sopenharmony_ci let styler = self[STYLER]; 1741cb0ef41Sopenharmony_ci 1751cb0ef41Sopenharmony_ci if (styler === undefined) { 1761cb0ef41Sopenharmony_ci return string; 1771cb0ef41Sopenharmony_ci } 1781cb0ef41Sopenharmony_ci 1791cb0ef41Sopenharmony_ci const {openAll, closeAll} = styler; 1801cb0ef41Sopenharmony_ci if (string.includes('\u001B')) { 1811cb0ef41Sopenharmony_ci while (styler !== undefined) { 1821cb0ef41Sopenharmony_ci // Replace any instances already present with a re-opening code 1831cb0ef41Sopenharmony_ci // otherwise only the part of the string until said closing code 1841cb0ef41Sopenharmony_ci // will be colored, and the rest will simply be 'plain'. 1851cb0ef41Sopenharmony_ci string = stringReplaceAll(string, styler.close, styler.open); 1861cb0ef41Sopenharmony_ci 1871cb0ef41Sopenharmony_ci styler = styler.parent; 1881cb0ef41Sopenharmony_ci } 1891cb0ef41Sopenharmony_ci } 1901cb0ef41Sopenharmony_ci 1911cb0ef41Sopenharmony_ci // We can move both next actions out of loop, because remaining actions in loop won't have 1921cb0ef41Sopenharmony_ci // any/visible effect on parts we add here. Close the styling before a linebreak and reopen 1931cb0ef41Sopenharmony_ci // after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92 1941cb0ef41Sopenharmony_ci const lfIndex = string.indexOf('\n'); 1951cb0ef41Sopenharmony_ci if (lfIndex !== -1) { 1961cb0ef41Sopenharmony_ci string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex); 1971cb0ef41Sopenharmony_ci } 1981cb0ef41Sopenharmony_ci 1991cb0ef41Sopenharmony_ci return openAll + string + closeAll; 2001cb0ef41Sopenharmony_ci}; 2011cb0ef41Sopenharmony_ci 2021cb0ef41Sopenharmony_ciObject.defineProperties(createChalk.prototype, styles); 2031cb0ef41Sopenharmony_ci 2041cb0ef41Sopenharmony_ciconst chalk = createChalk(); 2051cb0ef41Sopenharmony_ciexport const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0}); 2061cb0ef41Sopenharmony_ci 2071cb0ef41Sopenharmony_ciexport { 2081cb0ef41Sopenharmony_ci modifierNames, 2091cb0ef41Sopenharmony_ci foregroundColorNames, 2101cb0ef41Sopenharmony_ci backgroundColorNames, 2111cb0ef41Sopenharmony_ci colorNames, 2121cb0ef41Sopenharmony_ci 2131cb0ef41Sopenharmony_ci // TODO: Remove these aliases in the next major version 2141cb0ef41Sopenharmony_ci modifierNames as modifiers, 2151cb0ef41Sopenharmony_ci foregroundColorNames as foregroundColors, 2161cb0ef41Sopenharmony_ci backgroundColorNames as backgroundColors, 2171cb0ef41Sopenharmony_ci colorNames as colors, 2181cb0ef41Sopenharmony_ci} from './vendor/ansi-styles/index.js'; 2191cb0ef41Sopenharmony_ci 2201cb0ef41Sopenharmony_ciexport { 2211cb0ef41Sopenharmony_ci stdoutColor as supportsColor, 2221cb0ef41Sopenharmony_ci stderrColor as supportsColorStderr, 2231cb0ef41Sopenharmony_ci}; 2241cb0ef41Sopenharmony_ci 2251cb0ef41Sopenharmony_ciexport default chalk; 226