2 const ansiStyles = require('ansi-styles');
3 const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
6 stringEncaseCRLFWithFirstIndex
9 const {isArray} = Array;
11 // `supportsColor.level` → `ansiStyles.color[name]` mapping
12 const levelMapping = [
19 const styles = Object.create(null);
21 const applyOptions = (object, options = {}) => {
22 if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
23 throw new Error('The `level` option should be an integer from 0 to 3');
26 // Detect level if not set manually
27 const colorLevel = stdoutColor ? stdoutColor.level : 0;
28 object.level = options.level === undefined ? colorLevel : options.level;
32 constructor(options) {
33 // eslint-disable-next-line no-constructor-return
34 return chalkFactory(options);
38 const chalkFactory = options => {
40 applyOptions(chalk, options);
42 chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
44 Object.setPrototypeOf(chalk, Chalk.prototype);
45 Object.setPrototypeOf(chalk.template, chalk);
47 chalk.template.constructor = () => {
48 throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
51 chalk.template.Instance = ChalkClass;
53 return chalk.template;
56 function Chalk(options) {
57 return chalkFactory(options);
60 for (const [styleName, style] of Object.entries(ansiStyles)) {
63 const builder = createBuilder(this, createStyler(style.open, style.close, this._styler), this._isEmpty);
64 Object.defineProperty(this, styleName, {value: builder});
72 const builder = createBuilder(this, this._styler, true);
73 Object.defineProperty(this, 'visible', {value: builder});
78 const usedModels = ['rgb', 'hex', 'keyword', 'hsl', 'hsv', 'hwb', 'ansi', 'ansi256'];
80 for (const model of usedModels) {
84 return function (...arguments_) {
85 const styler = createStyler(ansiStyles.color[levelMapping[level]][model](...arguments_), ansiStyles.color.close, this._styler);
86 return createBuilder(this, styler, this._isEmpty);
92 for (const model of usedModels) {
93 const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
97 return function (...arguments_) {
98 const styler = createStyler(ansiStyles.bgColor[levelMapping[level]][model](...arguments_), ansiStyles.bgColor.close, this._styler);
99 return createBuilder(this, styler, this._isEmpty);
105 const proto = Object.defineProperties(() => {}, {
110 return this._generator.level;
113 this._generator.level = level;
118 const createStyler = (open, close, parent) => {
121 if (parent === undefined) {
125 openAll = parent.openAll + open;
126 closeAll = close + parent.closeAll;
138 const createBuilder = (self, _styler, _isEmpty) => {
139 const builder = (...arguments_) => {
140 if (isArray(arguments_[0]) && isArray(arguments_[0].raw)) {
141 // Called as a template literal, for example: chalk.red`2 + 3 = {bold ${2+3}}`
142 return applyStyle(builder, chalkTag(builder, ...arguments_));
145 // Single argument is hot path, implicit coercion is faster than anything
146 // eslint-disable-next-line no-implicit-coercion
147 return applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
150 // We alter the prototype because we must return a function, but there is
151 // no way to create a function with a different prototype
152 Object.setPrototypeOf(builder, proto);
154 builder._generator = self;
155 builder._styler = _styler;
156 builder._isEmpty = _isEmpty;
161 const applyStyle = (self, string) => {
162 if (self.level <= 0 || !string) {
163 return self._isEmpty ? '' : string;
166 let styler = self._styler;
168 if (styler === undefined) {
172 const {openAll, closeAll} = styler;
173 if (string.indexOf('\u001B') !== -1) {
174 while (styler !== undefined) {
175 // Replace any instances already present with a re-opening code
176 // otherwise only the part of the string until said closing code
177 // will be colored, and the rest will simply be 'plain'.
178 string = stringReplaceAll(string, styler.close, styler.open);
180 styler = styler.parent;
184 // We can move both next actions out of loop, because remaining actions in loop won't have
185 // any/visible effect on parts we add here. Close the styling before a linebreak and reopen
186 // after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
187 const lfIndex = string.indexOf('\n');
188 if (lfIndex !== -1) {
189 string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
192 return openAll + string + closeAll;
196 const chalkTag = (chalk, ...strings) => {
197 const [firstString] = strings;
199 if (!isArray(firstString) || !isArray(firstString.raw)) {
200 // If chalk() was called by itself or with a string,
201 // return the string itself as a string.
202 return strings.join(' ');
205 const arguments_ = strings.slice(1);
206 const parts = [firstString.raw[0]];
208 for (let i = 1; i < firstString.length; i++) {
210 String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
211 String(firstString.raw[i])
215 if (template === undefined) {
216 template = require('./templates');
219 return template(chalk, parts.join(''));
222 Object.defineProperties(Chalk.prototype, styles);
224 const chalk = Chalk(); // eslint-disable-line new-cap
225 chalk.supportsColor = stdoutColor;
226 chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
227 chalk.stderr.supportsColor = stderrColor;
229 module.exports = chalk;