2021-11-26 16:34:27 +07:00
|
|
|
import ansiStyles from '#ansi-styles';
|
|
|
|
|
import supportsColor from '#supports-color';
|
|
|
|
|
import { // eslint-disable-line import/order
|
2019-07-12 09:40:23 +03:00
|
|
|
stringReplaceAll,
|
2021-07-30 17:30:19 +02:00
|
|
|
stringEncaseCRLFWithFirstIndex,
|
2021-11-26 16:34:27 +07:00
|
|
|
} from './utilities.js';
|
2019-07-12 09:40:23 +03:00
|
|
|
|
2021-04-16 15:23:29 +07:00
|
|
|
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
|
2020-06-09 09:36:34 +02:00
|
|
|
|
2021-04-22 20:03:48 +12:00
|
|
|
const GENERATOR = Symbol('GENERATOR');
|
|
|
|
|
const STYLER = Symbol('STYLER');
|
|
|
|
|
const IS_EMPTY = Symbol('IS_EMPTY');
|
|
|
|
|
|
2017-06-20 19:18:21 +02:00
|
|
|
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
2019-03-12 20:11:31 +07:00
|
|
|
const levelMapping = [
|
|
|
|
|
'ansi',
|
|
|
|
|
'ansi',
|
|
|
|
|
'ansi256',
|
2021-07-30 17:30:19 +02:00
|
|
|
'ansi16m',
|
2019-03-12 20:11:31 +07:00
|
|
|
];
|
2017-06-20 21:17:16 +02:00
|
|
|
|
2017-06-29 16:46:19 -07:00
|
|
|
const styles = Object.create(null);
|
|
|
|
|
|
2019-03-14 23:53:22 +07:00
|
|
|
const applyOptions = (object, options = {}) => {
|
2020-04-02 15:56:21 +08:00
|
|
|
if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
|
2018-09-18 15:33:20 +07:00
|
|
|
throw new Error('The `level` option should be an integer from 0 to 3');
|
|
|
|
|
}
|
2017-06-29 16:46:19 -07:00
|
|
|
|
2017-06-20 19:18:21 +02:00
|
|
|
// Detect level if not set manually
|
2018-12-26 02:37:03 +01:00
|
|
|
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
|
|
|
|
object.level = options.level === undefined ? colorLevel : options.level;
|
2019-03-14 23:53:22 +07:00
|
|
|
};
|
2017-06-29 16:46:19 -07:00
|
|
|
|
2021-04-18 00:33:03 +12:00
|
|
|
export class Chalk {
|
2019-03-12 12:53:03 +00:00
|
|
|
constructor(options) {
|
2020-04-02 15:56:21 +08:00
|
|
|
// eslint-disable-next-line no-constructor-return
|
2019-03-12 12:53:03 +00:00
|
|
|
return chalkFactory(options);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-06-29 16:46:19 -07:00
|
|
|
|
2019-03-14 23:53:22 +07:00
|
|
|
const chalkFactory = options => {
|
2021-11-10 22:12:33 +13:00
|
|
|
const chalk = (...strings) => strings.join(' ');
|
2019-03-12 12:53:03 +00:00
|
|
|
applyOptions(chalk, options);
|
2017-06-29 16:46:19 -07:00
|
|
|
|
2021-04-18 00:33:03 +12:00
|
|
|
Object.setPrototypeOf(chalk, createChalk.prototype);
|
2019-03-12 12:53:03 +00:00
|
|
|
|
2021-11-10 22:12:33 +13:00
|
|
|
return chalk;
|
2019-03-14 23:53:22 +07:00
|
|
|
};
|
2017-06-29 16:46:19 -07:00
|
|
|
|
2021-04-18 00:33:03 +12:00
|
|
|
function createChalk(options) {
|
2019-03-12 12:53:03 +00:00
|
|
|
return chalkFactory(options);
|
2015-02-17 15:18:18 +07:00
|
|
|
}
|
2013-08-29 16:14:18 +02:00
|
|
|
|
2021-04-20 18:48:30 +12:00
|
|
|
Object.setPrototypeOf(createChalk.prototype, Function.prototype);
|
|
|
|
|
|
2019-03-12 20:11:31 +07:00
|
|
|
for (const [styleName, style] of Object.entries(ansiStyles)) {
|
|
|
|
|
styles[styleName] = {
|
2017-06-20 19:18:21 +02:00
|
|
|
get() {
|
2021-04-22 20:03:48 +12:00
|
|
|
const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
|
2019-07-12 09:40:23 +03:00
|
|
|
Object.defineProperty(this, styleName, {value: builder});
|
|
|
|
|
return builder;
|
2021-07-30 17:30:19 +02:00
|
|
|
},
|
2017-06-20 10:02:09 -07:00
|
|
|
};
|
2017-06-20 19:18:21 +02:00
|
|
|
}
|
2017-06-20 10:02:09 -07:00
|
|
|
|
2017-10-23 20:39:21 -05:00
|
|
|
styles.visible = {
|
|
|
|
|
get() {
|
2021-04-22 20:03:48 +12:00
|
|
|
const builder = createBuilder(this, this[STYLER], true);
|
2019-07-12 09:40:23 +03:00
|
|
|
Object.defineProperty(this, 'visible', {value: builder});
|
|
|
|
|
return builder;
|
2021-07-30 17:30:19 +02:00
|
|
|
},
|
2017-10-23 20:39:21 -05:00
|
|
|
};
|
|
|
|
|
|
2021-04-22 15:54:42 +12:00
|
|
|
const getModelAnsi = (model, level, type, ...arguments_) => {
|
|
|
|
|
if (model === 'rgb') {
|
|
|
|
|
if (level === 'ansi16m') {
|
|
|
|
|
return ansiStyles[type].ansi16m(...arguments_);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (level === 'ansi256') {
|
|
|
|
|
return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (model === 'hex') {
|
|
|
|
|
return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ansiStyles[type][model](...arguments_);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const usedModels = ['rgb', 'hex', 'ansi256'];
|
2017-06-20 10:02:09 -07:00
|
|
|
|
2019-09-27 06:55:19 +03:00
|
|
|
for (const model of usedModels) {
|
2017-06-20 10:02:09 -07:00
|
|
|
styles[model] = {
|
2017-06-20 19:18:21 +02:00
|
|
|
get() {
|
2018-09-18 14:05:08 +07:00
|
|
|
const {level} = this;
|
2019-03-14 23:53:22 +07:00
|
|
|
return function (...arguments_) {
|
2021-04-22 20:03:48 +12:00
|
|
|
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
|
|
|
|
|
return createBuilder(this, styler, this[IS_EMPTY]);
|
2017-06-20 10:02:09 -07:00
|
|
|
};
|
2021-07-30 17:30:19 +02:00
|
|
|
},
|
2017-06-20 10:02:09 -07:00
|
|
|
};
|
|
|
|
|
|
2017-06-20 21:17:16 +02:00
|
|
|
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
2017-06-20 10:02:09 -07:00
|
|
|
styles[bgModel] = {
|
2017-06-20 19:18:21 +02:00
|
|
|
get() {
|
2018-09-18 14:05:08 +07:00
|
|
|
const {level} = this;
|
2019-03-12 20:11:31 +07:00
|
|
|
return function (...arguments_) {
|
2021-04-22 20:03:48 +12:00
|
|
|
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
|
|
|
|
|
return createBuilder(this, styler, this[IS_EMPTY]);
|
2017-06-20 10:02:09 -07:00
|
|
|
};
|
2021-07-30 17:30:19 +02:00
|
|
|
},
|
2016-01-09 16:20:22 +01:00
|
|
|
};
|
2017-06-20 19:18:21 +02:00
|
|
|
}
|
2013-08-03 02:16:26 +02:00
|
|
|
|
2019-07-12 09:40:23 +03:00
|
|
|
const proto = Object.defineProperties(() => {}, {
|
|
|
|
|
...styles,
|
|
|
|
|
level: {
|
2017-01-30 04:10:02 -05:00
|
|
|
enumerable: true,
|
2017-06-20 19:18:21 +02:00
|
|
|
get() {
|
2021-04-22 20:03:48 +12:00
|
|
|
return this[GENERATOR].level;
|
2017-01-30 04:10:02 -05:00
|
|
|
},
|
2017-06-20 19:18:21 +02:00
|
|
|
set(level) {
|
2021-04-22 20:03:48 +12:00
|
|
|
this[GENERATOR].level = level;
|
2021-07-30 17:30:19 +02:00
|
|
|
},
|
|
|
|
|
},
|
2019-07-12 09:40:23 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const createStyler = (open, close, parent) => {
|
|
|
|
|
let openAll;
|
|
|
|
|
let closeAll;
|
|
|
|
|
if (parent === undefined) {
|
|
|
|
|
openAll = open;
|
|
|
|
|
closeAll = close;
|
|
|
|
|
} else {
|
|
|
|
|
openAll = parent.openAll + open;
|
|
|
|
|
closeAll = close + parent.closeAll;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
open,
|
|
|
|
|
close,
|
|
|
|
|
openAll,
|
|
|
|
|
closeAll,
|
2021-07-30 17:30:19 +02:00
|
|
|
parent,
|
2019-07-12 09:40:23 +03:00
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const createBuilder = (self, _styler, _isEmpty) => {
|
2021-11-10 22:12:33 +13:00
|
|
|
// Single argument is hot path, implicit coercion is faster than anything
|
|
|
|
|
// eslint-disable-next-line no-implicit-coercion
|
|
|
|
|
const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
|
2017-06-29 13:34:00 -07:00
|
|
|
|
2020-04-02 02:47:12 -05:00
|
|
|
// We alter the prototype because we must return a function, but there is
|
2017-06-30 12:42:24 +02:00
|
|
|
// no way to create a function with a different prototype
|
2020-04-02 02:47:12 -05:00
|
|
|
Object.setPrototypeOf(builder, proto);
|
2015-06-02 17:12:01 +02:00
|
|
|
|
2021-04-22 20:03:48 +12:00
|
|
|
builder[GENERATOR] = self;
|
|
|
|
|
builder[STYLER] = _styler;
|
|
|
|
|
builder[IS_EMPTY] = _isEmpty;
|
2019-07-12 09:40:23 +03:00
|
|
|
|
2015-06-02 17:12:01 +02:00
|
|
|
return builder;
|
2019-03-14 23:53:22 +07:00
|
|
|
};
|
2015-06-02 17:12:01 +02:00
|
|
|
|
2019-07-12 09:40:23 +03:00
|
|
|
const applyStyle = (self, string) => {
|
2019-07-13 07:45:31 +02:00
|
|
|
if (self.level <= 0 || !string) {
|
2021-04-22 20:03:48 +12:00
|
|
|
return self[IS_EMPTY] ? '' : string;
|
2014-06-24 22:59:31 +02:00
|
|
|
}
|
|
|
|
|
|
2021-04-22 20:03:48 +12:00
|
|
|
let styler = self[STYLER];
|
2019-07-12 09:40:23 +03:00
|
|
|
|
|
|
|
|
if (styler === undefined) {
|
|
|
|
|
return string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const {openAll, closeAll} = styler;
|
2021-04-16 15:23:29 +07:00
|
|
|
if (string.includes('\u001B')) {
|
2019-07-12 09:40:23 +03:00
|
|
|
while (styler !== undefined) {
|
|
|
|
|
// Replace any instances already present with a re-opening code
|
|
|
|
|
// otherwise only the part of the string until said closing code
|
|
|
|
|
// will be colored, and the rest will simply be 'plain'.
|
|
|
|
|
string = stringReplaceAll(string, styler.close, styler.open);
|
|
|
|
|
|
|
|
|
|
styler = styler.parent;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-17 12:35:27 +01:00
|
|
|
|
2019-07-12 13:59:50 +07:00
|
|
|
// We can move both next actions out of loop, because remaining actions in loop won't have
|
|
|
|
|
// any/visible effect on parts we add here. Close the styling before a linebreak and reopen
|
|
|
|
|
// after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
|
2019-07-12 09:40:23 +03:00
|
|
|
const lfIndex = string.indexOf('\n');
|
|
|
|
|
if (lfIndex !== -1) {
|
|
|
|
|
string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
|
2014-06-26 00:15:15 +02:00
|
|
|
}
|
|
|
|
|
|
2019-07-12 09:40:23 +03:00
|
|
|
return openAll + string + closeAll;
|
2019-03-14 23:53:22 +07:00
|
|
|
};
|
2014-06-24 22:59:31 +02:00
|
|
|
|
2021-04-18 00:33:03 +12:00
|
|
|
Object.defineProperties(createChalk.prototype, styles);
|
2013-12-13 19:36:43 +01:00
|
|
|
|
2021-04-18 00:33:03 +12:00
|
|
|
const chalk = createChalk();
|
|
|
|
|
export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0});
|
|
|
|
|
|
2022-10-12 17:35:02 +08:00
|
|
|
export {
|
|
|
|
|
modifierNames,
|
|
|
|
|
foregroundColorNames,
|
|
|
|
|
backgroundColorNames,
|
|
|
|
|
colorNames,
|
|
|
|
|
|
|
|
|
|
// TODO: Remove these aliases in the next major version
|
|
|
|
|
modifierNames as modifiers,
|
|
|
|
|
foregroundColorNames as foregroundColors,
|
|
|
|
|
backgroundColorNames as backgroundColors,
|
|
|
|
|
colorNames as colors,
|
|
|
|
|
} from './vendor/ansi-styles/index.js';
|
|
|
|
|
|
2021-04-18 00:33:03 +12:00
|
|
|
export {
|
|
|
|
|
stdoutColor as supportsColor,
|
2021-07-30 17:30:19 +02:00
|
|
|
stderrColor as supportsColorStderr,
|
2021-04-18 00:33:03 +12:00
|
|
|
};
|
2019-09-22 12:07:33 +03:00
|
|
|
|
2021-04-16 15:23:29 +07:00
|
|
|
export default chalk;
|