chalk/source/index.js

223 lines
5.7 KiB
JavaScript
Raw Normal View History

2013-08-03 02:16:26 +02:00
'use strict';
2017-06-20 19:18:21 +02:00
const ansiStyles = require('ansi-styles');
2019-09-22 12:07:33 +03:00
const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
2019-07-12 09:40:23 +03:00
const {
stringReplaceAll,
stringEncaseCRLFWithFirstIndex
2019-07-12 13:51:07 +07:00
} = require('./util');
2019-07-12 09:40:23 +03:00
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',
'ansi16m'
];
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
class ChalkClass {
constructor(options) {
2020-04-02 15:56:21 +08:00
// eslint-disable-next-line no-constructor-return
return chalkFactory(options);
}
}
2017-06-29 16:46:19 -07:00
2019-03-14 23:53:22 +07:00
const chalkFactory = options => {
const chalk = {};
applyOptions(chalk, options);
2017-06-29 16:46:19 -07:00
2019-03-12 20:11:31 +07:00
chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
2017-06-29 16:46:19 -07:00
Object.setPrototypeOf(chalk, Chalk.prototype);
Object.setPrototypeOf(chalk.template, chalk);
2017-06-29 16:46:19 -07:00
chalk.template.constructor = () => {
throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
};
2019-03-12 20:11:31 +07:00
chalk.template.Instance = ChalkClass;
return chalk.template;
2019-03-14 23:53:22 +07:00
};
2017-06-29 16:46:19 -07:00
function Chalk(options) {
return chalkFactory(options);
}
2013-08-29 16:14:18 +02:00
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() {
2019-07-12 09:40:23 +03:00
const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty);
Object.defineProperty(this, styleName, {value: builder});
return builder;
2017-06-20 10:02:09 -07:00
}
};
2017-06-20 19:18:21 +02:00
}
2017-06-20 10:02:09 -07:00
styles.visible = {
get() {
2019-07-12 09:40:23 +03:00
const builder = createBuilder(this, this._styler, true);
Object.defineProperty(this, 'visible', {value: builder});
return builder;
}
};
2019-09-27 06:55:19 +03:00
const usedModels = ['rgb', 'hex', 'keyword', 'hsl', 'hsv', 'hwb', 'ansi', '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_) {
2019-07-12 09:40:23 +03:00
const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler);
return createBuilder(this, styler, this._isEmpty);
2017-06-20 10:02:09 -07:00
};
}
};
2017-06-20 19:18:21 +02:00
}
2017-06-20 10:02:09 -07:00
2019-09-27 06:55:19 +03:00
for (const model of usedModels) {
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_) {
2019-07-12 09:40:23 +03:00
const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler);
return createBuilder(this, styler, this._isEmpty);
2017-06-20 10:02:09 -07: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: {
enumerable: true,
2017-06-20 19:18:21 +02:00
get() {
2019-07-12 09:40:23 +03:00
return this._generator.level;
},
2017-06-20 19:18:21 +02:00
set(level) {
2019-07-12 09:40:23 +03:00
this._generator.level = level;
}
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,
parent
};
};
const createBuilder = (self, _styler, _isEmpty) => {
const builder = (...arguments_) => {
// Single argument is hot path, implicit coercion is faster than anything
// eslint-disable-next-line no-implicit-coercion
return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
};
2017-06-29 13:34:00 -07: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
Object.setPrototypeOf(builder, proto);
2015-06-02 17:12:01 +02:00
2019-07-12 09:40:23 +03:00
builder._generator = self;
builder._styler = _styler;
builder._isEmpty = _isEmpty;
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) => {
if (self.level <= 0 || !string) {
2019-03-14 23:53:22 +07:00
return self._isEmpty ? '' : string;
}
2019-07-12 09:40:23 +03:00
let styler = self._styler;
if (styler === undefined) {
return string;
}
const {openAll, closeAll} = styler;
if (string.indexOf('\u001B') !== -1) {
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;
}
}
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);
}
2019-07-12 09:40:23 +03:00
return openAll + string + closeAll;
2019-03-14 23:53:22 +07:00
};
2019-09-27 06:55:19 +03:00
let template;
2019-03-14 23:53:22 +07:00
const chalkTag = (chalk, ...strings) => {
const [firstString] = strings;
2018-09-18 14:05:08 +07:00
if (!Array.isArray(firstString)) {
// If chalk() was called by itself or with a string,
// return the string itself as a string.
2018-09-18 14:05:08 +07:00
return strings.join(' ');
2017-06-29 16:46:19 -07:00
}
2019-03-12 20:11:31 +07:00
const arguments_ = strings.slice(1);
2018-09-18 14:05:08 +07:00
const parts = [firstString.raw[0]];
2017-06-29 16:46:19 -07:00
2018-09-18 14:05:08 +07:00
for (let i = 1; i < firstString.length; i++) {
2018-12-26 02:37:03 +01:00
parts.push(
2019-03-12 20:11:31 +07:00
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
2018-12-26 02:37:03 +01:00
String(firstString.raw[i])
);
2017-06-29 16:46:19 -07:00
}
2019-09-27 06:55:19 +03:00
if (template === undefined) {
template = require('./templates');
}
2017-06-29 16:46:19 -07:00
return template(chalk, parts.join(''));
2019-03-14 23:53:22 +07:00
};
2017-06-29 16:46:19 -07:00
2017-06-20 21:17:16 +02:00
Object.defineProperties(Chalk.prototype, styles);
2013-12-13 19:36:43 +01:00
2019-09-22 12:07:33 +03:00
const chalk = Chalk(); // eslint-disable-line new-cap
chalk.supportsColor = stdoutColor;
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
chalk.stderr.supportsColor = stderrColor;
module.exports = chalk;