Code style tweaks
This commit is contained in:
parent
076f0c9eb6
commit
587a5fbcbb
2 changed files with 41 additions and 49 deletions
50
index.js
50
index.js
|
|
@ -1,8 +1,7 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
const escapeStringRegexp = require('escape-string-regexp');
|
const escapeStringRegexp = require('escape-string-regexp');
|
||||||
const ansiStyles = require('ansi-styles');
|
const ansiStyles = require('ansi-styles');
|
||||||
const stdoutColor = require('supports-color').stdout;
|
const {stdout: stdoutColor} = require('supports-color');
|
||||||
|
|
||||||
const template = require('./templates.js');
|
const template = require('./templates.js');
|
||||||
|
|
||||||
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
|
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
|
||||||
|
|
@ -15,15 +14,15 @@ const skipModels = new Set(['gray']);
|
||||||
|
|
||||||
const styles = Object.create(null);
|
const styles = Object.create(null);
|
||||||
|
|
||||||
function applyOptions(obj, options = {}) {
|
function applyOptions(object, options = {}) {
|
||||||
if (options.level > 3 || options.level < 0) {
|
if (options.level > 3 || options.level < 0) {
|
||||||
throw new Error('The `level` option should be an integer from 0 to 3');
|
throw new Error('The `level` option should be an integer from 0 to 3');
|
||||||
}
|
}
|
||||||
|
|
||||||
// Detect level if not set manually
|
// Detect level if not set manually
|
||||||
const scLevel = stdoutColor ? stdoutColor.level : 0;
|
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
||||||
obj.level = options.level === undefined ? scLevel : options.level;
|
object.level = options.level === undefined ? colorLevel : options.level;
|
||||||
obj.enabled = 'enabled' in options ? options.enabled : obj.level > 0;
|
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
function Chalk(options) {
|
function Chalk(options) {
|
||||||
|
|
@ -33,7 +32,7 @@ function Chalk(options) {
|
||||||
const chalk = {};
|
const chalk = {};
|
||||||
applyOptions(chalk, options);
|
applyOptions(chalk, options);
|
||||||
|
|
||||||
chalk.template = (...args) => chalkTag(...[chalk.template].concat(args));
|
chalk.template = (...args) => chalkTag(chalk.template, ...args);
|
||||||
|
|
||||||
Object.setPrototypeOf(chalk, Chalk.prototype);
|
Object.setPrototypeOf(chalk, Chalk.prototype);
|
||||||
Object.setPrototypeOf(chalk.template, chalk);
|
Object.setPrototypeOf(chalk.template, chalk);
|
||||||
|
|
@ -57,7 +56,7 @@ for (const key of Object.keys(ansiStyles)) {
|
||||||
styles[key] = {
|
styles[key] = {
|
||||||
get() {
|
get() {
|
||||||
const codes = ansiStyles[key];
|
const codes = ansiStyles[key];
|
||||||
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, key);
|
return build.call(this, [...(this._styles || []), codes], this._empty, key);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
@ -84,7 +83,7 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
|
||||||
close: ansiStyles.color.close,
|
close: ansiStyles.color.close,
|
||||||
closeRe: ansiStyles.color.closeRe
|
closeRe: ansiStyles.color.closeRe
|
||||||
};
|
};
|
||||||
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
|
return build.call(this, [...(this._styles || []), codes], this._empty, model);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -107,7 +106,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
||||||
close: ansiStyles.bgColor.close,
|
close: ansiStyles.bgColor.close,
|
||||||
closeRe: ansiStyles.bgColor.closeRe
|
closeRe: ansiStyles.bgColor.closeRe
|
||||||
};
|
};
|
||||||
return build.call(this, this._styles ? this._styles.concat(codes) : [codes], this._empty, model);
|
return build.call(this, [...(this._styles || []), codes], this._empty, model);
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
@ -153,23 +152,10 @@ function build(_styles, _empty, key) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function applyStyle(...args) {
|
function applyStyle(...args) {
|
||||||
// Support varags, but simply cast to string in case there's only one arg
|
let string = args.join(' ');
|
||||||
const argsLen = args.length;
|
|
||||||
let str = String(args[0]);
|
|
||||||
|
|
||||||
if (argsLen === 0) {
|
if (!this.enabled || this.level <= 0 || !string) {
|
||||||
return '';
|
return this._empty ? '' : string;
|
||||||
}
|
|
||||||
|
|
||||||
if (argsLen > 1) {
|
|
||||||
// Don't slice `arguments`, it prevents V8 optimizations
|
|
||||||
for (let a = 1; a < argsLen; a++) {
|
|
||||||
str += ' ' + args[a];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.enabled || this.level <= 0 || !str) {
|
|
||||||
return this._empty ? '' : str;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
|
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
|
||||||
|
|
@ -184,18 +170,18 @@ function applyStyle(...args) {
|
||||||
// Replace any instances already present with a re-opening code
|
// Replace any instances already present with a re-opening code
|
||||||
// otherwise only the part of the string until said closing code
|
// otherwise only the part of the string until said closing code
|
||||||
// will be colored, and the rest will simply be 'plain'.
|
// will be colored, and the rest will simply be 'plain'.
|
||||||
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
string = code.open + string.replace(code.closeRe, code.open) + code.close;
|
||||||
|
|
||||||
// Close the styling before a linebreak and reopen
|
// Close the styling before a linebreak and reopen
|
||||||
// after next line to fix a bleed issue on macOS
|
// after next line to fix a bleed issue on macOS
|
||||||
// https://github.com/chalk/chalk/pull/92
|
// https://github.com/chalk/chalk/pull/92
|
||||||
str = str.replace(/\r?\n/g, `${code.close}$&${code.open}`);
|
string = string.replace(/\r?\n/g, `${code.close}$&${code.open}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
|
// Reset the original `dim` if we changed it to work around the Windows dimmed gray issue
|
||||||
ansiStyles.dim.open = originalDim;
|
ansiStyles.dim.open = originalDim;
|
||||||
|
|
||||||
return str;
|
return string;
|
||||||
}
|
}
|
||||||
|
|
||||||
function chalkTag(chalk, ...strings) {
|
function chalkTag(chalk, ...strings) {
|
||||||
|
|
@ -211,8 +197,10 @@ function chalkTag(chalk, ...strings) {
|
||||||
const parts = [firstString.raw[0]];
|
const parts = [firstString.raw[0]];
|
||||||
|
|
||||||
for (let i = 1; i < firstString.length; i++) {
|
for (let i = 1; i < firstString.length; i++) {
|
||||||
parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
|
parts.push(
|
||||||
parts.push(String(firstString.raw[i]));
|
String(args[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
||||||
|
String(firstString.raw[i])
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return template(chalk, parts.join(''));
|
return template(chalk, parts.join(''));
|
||||||
|
|
|
||||||
40
templates.js
40
templates.js
|
|
@ -31,10 +31,11 @@ function parseArguments(name, args) {
|
||||||
let matches;
|
let matches;
|
||||||
|
|
||||||
for (const chunk of chunks) {
|
for (const chunk of chunks) {
|
||||||
if (!isNaN(chunk)) {
|
const number = Number(chunk);
|
||||||
results.push(Number(chunk));
|
if (!Number.isNaN(number)) {
|
||||||
|
results.push(number);
|
||||||
} else if ((matches = chunk.match(STRING_REGEX))) {
|
} else if ((matches = chunk.match(STRING_REGEX))) {
|
||||||
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, chr) => escape ? unescape(escape) : chr));
|
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, character) => escape ? unescape(escape) : character));
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
|
throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
|
||||||
}
|
}
|
||||||
|
|
@ -73,17 +74,20 @@ function buildStyle(chalk, styles) {
|
||||||
}
|
}
|
||||||
|
|
||||||
let current = chalk;
|
let current = chalk;
|
||||||
|
// TODO: Use `Object.entries` when targeting Node.js 8
|
||||||
for (const styleName of Object.keys(enabled)) {
|
for (const styleName of Object.keys(enabled)) {
|
||||||
if (Array.isArray(enabled[styleName])) {
|
if (!Array.isArray(enabled[styleName])) {
|
||||||
if (!(styleName in current)) {
|
continue;
|
||||||
throw new Error(`Unknown Chalk style: ${styleName}`);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (enabled[styleName].length > 0) {
|
if (!(styleName in current)) {
|
||||||
current = current[styleName](...enabled[styleName]);
|
throw new Error(`Unknown Chalk style: ${styleName}`);
|
||||||
} else {
|
}
|
||||||
current = current[styleName];
|
|
||||||
}
|
if (enabled[styleName].length > 0) {
|
||||||
|
current = current[styleName](...enabled[styleName]);
|
||||||
|
} else {
|
||||||
|
current = current[styleName];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -96,13 +100,13 @@ module.exports = (chalk, tmp) => {
|
||||||
let chunk = [];
|
let chunk = [];
|
||||||
|
|
||||||
// eslint-disable-next-line max-params
|
// eslint-disable-next-line max-params
|
||||||
tmp.replace(TEMPLATE_REGEX, (m, escapeChar, inverse, style, close, chr) => {
|
tmp.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
|
||||||
if (escapeChar) {
|
if (escapeCharacter) {
|
||||||
chunk.push(unescape(escapeChar));
|
chunk.push(unescape(escapeCharacter));
|
||||||
} else if (style) {
|
} else if (style) {
|
||||||
const str = chunk.join('');
|
const string = chunk.join('');
|
||||||
chunk = [];
|
chunk = [];
|
||||||
chunks.push(styles.length === 0 ? str : buildStyle(chalk, styles)(str));
|
chunks.push(styles.length === 0 ? string : buildStyle(chalk, styles)(string));
|
||||||
styles.push({inverse, styles: parseStyle(style)});
|
styles.push({inverse, styles: parseStyle(style)});
|
||||||
} else if (close) {
|
} else if (close) {
|
||||||
if (styles.length === 0) {
|
if (styles.length === 0) {
|
||||||
|
|
@ -113,7 +117,7 @@ module.exports = (chalk, tmp) => {
|
||||||
chunk = [];
|
chunk = [];
|
||||||
styles.pop();
|
styles.pop();
|
||||||
} else {
|
} else {
|
||||||
chunk.push(chr);
|
chunk.push(character);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue