Require Node.js 6
This commit is contained in:
parent
70f22d87ba
commit
0307f263cb
13 changed files with 204 additions and 216 deletions
43
index.js
43
index.js
|
|
@ -31,10 +31,7 @@ function Chalk(options) {
|
|||
const chalk = {};
|
||||
applyOptions(chalk, options);
|
||||
|
||||
chalk.template = function () {
|
||||
const args = [].slice.call(arguments);
|
||||
return chalkTag.apply(null, [chalk.template].concat(args));
|
||||
};
|
||||
chalk.template = (...args) => chalkTag(...[chalk.template].concat(args));
|
||||
|
||||
Object.setPrototypeOf(chalk, Chalk.prototype);
|
||||
Object.setPrototypeOf(chalk.template, chalk);
|
||||
|
|
@ -77,9 +74,9 @@ for (const model of Object.keys(ansiStyles.color.ansi)) {
|
|||
|
||||
styles[model] = {
|
||||
get() {
|
||||
const level = this.level;
|
||||
return function () {
|
||||
const open = ansiStyles.color[levelMapping[level]][model].apply(null, arguments);
|
||||
const {level} = this;
|
||||
return function (...args) {
|
||||
const open = ansiStyles.color[levelMapping[level]][model](...args);
|
||||
const codes = {
|
||||
open,
|
||||
close: ansiStyles.color.close,
|
||||
|
|
@ -100,9 +97,9 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|||
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
||||
styles[bgModel] = {
|
||||
get() {
|
||||
const level = this.level;
|
||||
return function () {
|
||||
const open = ansiStyles.bgColor[levelMapping[level]][model].apply(null, arguments);
|
||||
const {level} = this;
|
||||
return function (...args) {
|
||||
const open = ansiStyles.bgColor[levelMapping[level]][model](...args);
|
||||
const codes = {
|
||||
open,
|
||||
close: ansiStyles.bgColor.close,
|
||||
|
|
@ -117,10 +114,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|||
const proto = Object.defineProperties(() => {}, styles);
|
||||
|
||||
function build(_styles, _empty, key) {
|
||||
const builder = function () {
|
||||
return applyStyle.apply(builder, arguments);
|
||||
};
|
||||
|
||||
const builder = (...args) => applyStyle.call(builder, ...args);
|
||||
builder._styles = _styles;
|
||||
builder._empty = _empty;
|
||||
|
||||
|
|
@ -156,11 +150,10 @@ function build(_styles, _empty, key) {
|
|||
return builder;
|
||||
}
|
||||
|
||||
function applyStyle() {
|
||||
function applyStyle(...args) {
|
||||
// Support varags, but simply cast to string in case there's only one arg
|
||||
const args = arguments;
|
||||
const argsLen = args.length;
|
||||
let str = String(arguments[0]);
|
||||
let str = String(args[0]);
|
||||
|
||||
if (argsLen === 0) {
|
||||
return '';
|
||||
|
|
@ -203,19 +196,21 @@ function applyStyle() {
|
|||
return str;
|
||||
}
|
||||
|
||||
function chalkTag(chalk, strings) {
|
||||
if (!Array.isArray(strings)) {
|
||||
function chalkTag(chalk, ...strings) {
|
||||
const firstString = strings[0];
|
||||
|
||||
if (!Array.isArray(firstString)) {
|
||||
// If chalk() was called by itself or with a string,
|
||||
// return the string itself as a string.
|
||||
return [].slice.call(arguments, 1).join(' ');
|
||||
return strings.join(' ');
|
||||
}
|
||||
|
||||
const args = [].slice.call(arguments, 2);
|
||||
const parts = [strings.raw[0]];
|
||||
const args = strings.slice(1);
|
||||
const parts = [firstString.raw[0]];
|
||||
|
||||
for (let i = 1; i < strings.length; i++) {
|
||||
for (let i = 1; i < firstString.length; i++) {
|
||||
parts.push(String(args[i - 1]).replace(/[{}\\]/g, '\\$&'));
|
||||
parts.push(String(strings.raw[i]));
|
||||
parts.push(String(firstString.raw[i]));
|
||||
}
|
||||
|
||||
return template(chalk, parts.join(''));
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue