chalk/source/index.js

236 lines
5.6 KiB
JavaScript
Raw Normal View History

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,
stringEncaseCRLFWithFirstIndex,
} from "./utilities.js";
2019-07-12 09:40:23 +03:00
const { stdout: stdoutColor, stderr: stderrColor } = supportsColor;
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
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 = {}) => {
if (
options.level &&
!(
Number.isInteger(options.level) &&
options.level >= 0 &&
options.level <= 3
)
) {
throw new Error("The `level` option should be an integer from 0 to 3");
2018-09-18 15:33:20 +07:00
}
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 {
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
const chalkFactory = (options) => {
const chalk = (...strings) => strings.join(" ");
applyOptions(chalk, options);
2017-06-29 16:46:19 -07:00
2021-04-18 00:33:03 +12:00
Object.setPrototypeOf(chalk, createChalk.prototype);
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) {
return chalkFactory(options);
}
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() {
const builder = createBuilder(
this,
createStyler(style.open, style.close, this[STYLER]),
this[IS_EMPTY]
);
Object.defineProperty(this, styleName, { value: builder });
2019-07-12 09:40:23 +03:00
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() {
const builder = createBuilder(this, this[STYLER], true);
Object.defineProperty(this, "visible", { value: builder });
2019-07-12 09:40:23 +03:00
return builder;
},
};
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() {
const { level } = this;
2019-03-14 23:53:22 +07:00
return function (...arguments_) {
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
};
},
2017-06-20 10:02:09 -07: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() {
const { level } = this;
2019-03-12 20:11:31 +07:00
return function (...arguments_) {
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
};
},
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() {
return this[GENERATOR].level;
},
2017-06-20 19:18:21 +02:00
set(level) {
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,
2019-07-12 09:40:23 +03:00
};
};
const createBuilder = (self, _styler, _isEmpty) => {
// 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
// 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
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) => {
if (self.level <= 0 || !string) {
return self[IS_EMPTY] ? "" : string;
}
let styler = self[STYLER];
2019-07-12 09:40:23 +03:00
if (styler === undefined) {
return string;
}
const { openAll, closeAll } = styler;
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;
}
}
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
const lfIndex = string.indexOf("\n");
2019-07-12 09:40:23 +03:00
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
};
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,
});
2021-04-18 00:33:03 +12:00
export { stdoutColor as supportsColor, stderrColor as supportsColorStderr };
2019-09-22 12:07:33 +03:00
2021-04-16 15:23:29 +07:00
export default chalk;