chalk/index.js

248 lines
6 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');
2018-12-26 02:37:03 +01:00
const {stdout: stdoutColor} = require('supports-color');
2017-06-29 16:46:19 -07:00
const template = require('./templates.js');
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
const stringReplaceAll = (str, substr, replacer) => {
let idx = str.indexOf(substr);
if (idx === -1) {
return str;
}
const subLen = substr.length;
let end = 0;
let res = '';
do {
res += str.substr(end, idx - end) + replacer;
end = idx + subLen;
idx = str.indexOf(substr, end);
} while (idx !== -1);
res += str.substr(end);
return res;
};
const stringEncaseCRLF = (str, prefix, postfix) => {
let idx = str.indexOf('\n');
if (idx === -1) {
return str;
}
let end = 0;
let res = '';
do {
const gotCR = str[idx - 1] === '\r';
res += str.substr(end, (gotCR ? idx - 1 : idx) - end) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
end = idx + 1;
idx = str.indexOf('\n', end);
} while (idx !== -1);
res += str.substr(end);
return res;
};
2017-06-20 19:18:21 +02:00
// `color-convert` models to exclude from the Chalk API due to conflicts and such
2017-06-20 21:17:16 +02:00
const skipModels = new Set(['gray']);
2017-06-20 10:02:09 -07: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 = {}) => {
2018-09-18 15:33:20 +07:00
if (options.level > 3 || options.level < 0) {
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;
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
2019-03-14 23:53:22 +07:00
};
2017-06-29 16:46:19 -07:00
class ChalkClass {
constructor(options) {
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-03-26 18:33:09 +03:00
return createBuilder(this, createStyler(style.open, style.close, 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
styles.visible = {
get() {
2019-03-26 18:33:09 +03:00
return createBuilder(this, this._styler, true);
}
};
2017-06-20 19:18:21 +02:00
for (const model of Object.keys(ansiStyles.color.ansi)) {
2017-06-20 21:17:16 +02:00
if (skipModels.has(model)) {
2017-06-20 19:18:21 +02:00
continue;
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-03-26 18:33:09 +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
2017-06-20 19:18:21 +02:00
for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
2017-06-20 21:17:16 +02:00
if (skipModels.has(model)) {
2017-06-20 19:18:21 +02:00
continue;
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_) {
2019-03-26 18:33:09 +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
const proto = Object.defineProperties(() => {}, {
...styles,
level: {
enumerable: true,
2017-06-20 19:18:21 +02:00
get() {
return this._generator.level;
},
2017-06-20 19:18:21 +02:00
set(level) {
this._generator.level = level;
}
},
enabled: {
2017-06-29 13:34:00 -07:00
enumerable: true,
get() {
return this._generator.enabled;
2017-06-29 13:34:00 -07:00
},
set(enabled) {
this._generator.enabled = enabled;
2017-06-29 13:34:00 -07:00
}
}
});
2019-03-26 18:33:09 +03:00
const createStyler = (open, close, parent) => {
return {
open,
close,
parent
};
};
const createBuilder = (self, _styler, _isEmpty) => {
const builder = (...arguments_) => {
// 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
2017-06-20 19:18:21 +02:00
// `__proto__` is used 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
2017-06-20 19:18:21 +02:00
builder.__proto__ = proto; // eslint-disable-line no-proto
2015-06-02 17:12:01 +02:00
builder._generator = self;
2019-03-26 18:33:09 +03:00
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
const applyStyle = (self, string) => {
2019-03-14 23:53:22 +07:00
if (!self.enabled || self.level <= 0 || !string) {
return self._isEmpty ? '' : string;
}
2019-03-26 18:33:09 +03:00
let styler = self._styler;
2019-03-26 19:09:35 +03:00
if (styler !== undefined) {
let closeAll = '';
let openAll = '';
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);
openAll = styler.open + openAll;
closeAll += styler.close;
styler = styler.parent;
}
// 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
2016-01-17 12:38:10 +01:00
// Close the styling before a linebreak and reopen
2016-06-14 18:20:49 +02:00
// after next line to fix a bleed issue on macOS
2016-01-17 12:38:10 +01:00
// https://github.com/chalk/chalk/pull/92
2019-03-26 19:09:35 +03:00
string = stringEncaseCRLF(string, closeAll, openAll);
string = openAll + string + closeAll;
}
2018-12-26 02:37:03 +01:00
return string;
2019-03-14 23:53:22 +07:00
};
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
}
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
2017-06-29 16:46:19 -07:00
module.exports = Chalk(); // eslint-disable-line new-cap
module.exports.supportsColor = stdoutColor;