Merge branch 'master' into remove-blue-workaround
This commit is contained in:
commit
077329e4ec
15 changed files with 166 additions and 142 deletions
|
|
@ -2,6 +2,5 @@ language: node_js
|
|||
node_js:
|
||||
- '10'
|
||||
- '8'
|
||||
- '6'
|
||||
after_success:
|
||||
- './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls'
|
||||
|
|
|
|||
|
|
@ -3,36 +3,39 @@ const chalk = require('..');
|
|||
|
||||
const ignoreChars = /[^!-~]/g;
|
||||
|
||||
function rainbow(str, offset) {
|
||||
if (!str || str.length === 0) {
|
||||
return str;
|
||||
const delay = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds));
|
||||
|
||||
function rainbow(string, offset) {
|
||||
if (!string || string.length === 0) {
|
||||
return string;
|
||||
}
|
||||
|
||||
const hueStep = 360 / str.replace(ignoreChars, '').length;
|
||||
const hueStep = 360 / string.replace(ignoreChars, '').length;
|
||||
|
||||
let hue = offset % 360;
|
||||
const chars = [];
|
||||
for (const c of str) {
|
||||
if (c.match(ignoreChars)) {
|
||||
chars.push(c);
|
||||
const characters = [];
|
||||
for (const character of string) {
|
||||
if (character.match(ignoreChars)) {
|
||||
characters.push(character);
|
||||
} else {
|
||||
chars.push(chalk.hsl(hue, 100, 50)(c));
|
||||
characters.push(chalk.hsl(hue, 100, 50)(character));
|
||||
hue = (hue + hueStep) % 360;
|
||||
}
|
||||
}
|
||||
|
||||
return chars.join('');
|
||||
return characters.join('');
|
||||
}
|
||||
|
||||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
async function animateString(str) {
|
||||
async function animateString(string) {
|
||||
console.log();
|
||||
for (let i = 0; i < 360 * 5; i++) {
|
||||
console.log('\u001B[1F\u001B[G ', rainbow(str, i));
|
||||
await sleep(2); // eslint-disable-line no-await-in-loop
|
||||
console.log('\u001B[1F\u001B[G', rainbow(string, i));
|
||||
await delay(2); // eslint-disable-line no-await-in-loop
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
animateString('We hope you enjoy the new version of Chalk 2! <3').then(() => console.log());
|
||||
(async () => {
|
||||
console.log();
|
||||
await animateString('We hope you enjoy Chalk! <3');
|
||||
console.log();
|
||||
})();
|
||||
|
|
|
|||
6
index.d.ts
vendored
6
index.d.ts
vendored
|
|
@ -35,7 +35,7 @@ export interface Options {
|
|||
level?: Level;
|
||||
}
|
||||
|
||||
export interface Constructor {
|
||||
export interface Instance {
|
||||
/**
|
||||
* Return a new Chalk instance.
|
||||
*/
|
||||
|
|
@ -75,7 +75,7 @@ export interface Chalk {
|
|||
/**
|
||||
* Return a new Chalk instance.
|
||||
*/
|
||||
constructor: Constructor;
|
||||
Instance: Instance;
|
||||
|
||||
/**
|
||||
* Enable or disable Chalk.
|
||||
|
|
@ -271,6 +271,6 @@ export interface Chalk {
|
|||
* Order doesn't matter, and later styles take precedent in case of a conflict.
|
||||
* This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
|
||||
*/
|
||||
declare const chalk: Chalk & { supportsColor: ColorSupport };
|
||||
declare const chalk: Chalk & {supportsColor: ColorSupport};
|
||||
|
||||
export default chalk;
|
||||
|
|
|
|||
72
index.js
72
index.js
|
|
@ -7,7 +7,12 @@ const template = require('./templates.js');
|
|||
const isSimpleWindowsTerm = process.platform === 'win32' && !(process.env.TERM || '').toLowerCase().startsWith('xterm');
|
||||
|
||||
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
||||
const levelMapping = ['ansi', 'ansi', 'ansi256', 'ansi16m'];
|
||||
const levelMapping = [
|
||||
'ansi',
|
||||
'ansi',
|
||||
'ansi256',
|
||||
'ansi16m'
|
||||
];
|
||||
|
||||
// `color-convert` models to exclude from the Chalk API due to conflicts and such
|
||||
const skipModels = new Set(['gray']);
|
||||
|
|
@ -25,33 +30,40 @@ function applyOptions(object, options = {}) {
|
|||
object.enabled = 'enabled' in options ? options.enabled : object.level > 0;
|
||||
}
|
||||
|
||||
function Chalk(options) {
|
||||
// We check for this.template here since calling `chalk.constructor()`
|
||||
// by itself will have a `this` of a previously constructed chalk object
|
||||
if (!this || !(this instanceof Chalk) || this.template) {
|
||||
const chalk = {};
|
||||
applyOptions(chalk, options);
|
||||
|
||||
chalk.template = (...args) => chalkTag(chalk.template, ...args);
|
||||
|
||||
Object.setPrototypeOf(chalk, Chalk.prototype);
|
||||
Object.setPrototypeOf(chalk.template, chalk);
|
||||
|
||||
chalk.template.constructor = Chalk;
|
||||
|
||||
return chalk.template;
|
||||
class ChalkClass {
|
||||
constructor(options) {
|
||||
return chalkFactory(options);
|
||||
}
|
||||
|
||||
applyOptions(this, options);
|
||||
}
|
||||
|
||||
for (const key of Object.keys(ansiStyles)) {
|
||||
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
||||
function chalkFactory(options) {
|
||||
const chalk = {};
|
||||
applyOptions(chalk, options);
|
||||
|
||||
styles[key] = {
|
||||
chalk.template = (...arguments_) => chalkTag(chalk.template, ...arguments_);
|
||||
|
||||
Object.setPrototypeOf(chalk, Chalk.prototype);
|
||||
Object.setPrototypeOf(chalk.template, chalk);
|
||||
|
||||
chalk.template.constructor = () => {
|
||||
throw new Error('`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
|
||||
};
|
||||
|
||||
chalk.template.Instance = ChalkClass;
|
||||
|
||||
return chalk.template;
|
||||
}
|
||||
|
||||
function Chalk(options) {
|
||||
return chalkFactory(options);
|
||||
}
|
||||
|
||||
for (const [styleName, style] of Object.entries(ansiStyles)) {
|
||||
style.closeRe = new RegExp(escapeStringRegexp(style.close), 'g');
|
||||
|
||||
styles[styleName] = {
|
||||
get() {
|
||||
const codes = ansiStyles[key];
|
||||
return build.call(this, [...(this._styles || []), codes], this._empty, key);
|
||||
return build.call(this, [...(this._styles || []), style], this._empty, styleName);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
@ -94,8 +106,8 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|||
styles[bgModel] = {
|
||||
get() {
|
||||
const {level} = this;
|
||||
return function (...args) {
|
||||
const open = ansiStyles.bgColor[levelMapping[level]][model](...args);
|
||||
return function (...arguments_) {
|
||||
const open = ansiStyles.bgColor[levelMapping[level]][model](...arguments_);
|
||||
const codes = {
|
||||
open,
|
||||
close: ansiStyles.bgColor.close,
|
||||
|
|
@ -110,7 +122,7 @@ for (const model of Object.keys(ansiStyles.bgColor.ansi)) {
|
|||
const proto = Object.defineProperties(() => {}, styles);
|
||||
|
||||
function build(_styles, _empty, key) {
|
||||
const builder = (...args) => applyStyle.call(builder, ...args);
|
||||
const builder = (...arguments_) => applyStyle.call(builder, ...arguments_);
|
||||
builder._styles = _styles;
|
||||
builder._empty = _empty;
|
||||
|
||||
|
|
@ -146,8 +158,8 @@ function build(_styles, _empty, key) {
|
|||
return builder;
|
||||
}
|
||||
|
||||
function applyStyle(...args) {
|
||||
let string = args.join(' ');
|
||||
function applyStyle(...arguments_) {
|
||||
let string = arguments_.join(' ');
|
||||
|
||||
if (!this.enabled || this.level <= 0 || !string) {
|
||||
return this._empty ? '' : string;
|
||||
|
|
@ -188,12 +200,12 @@ function chalkTag(chalk, ...strings) {
|
|||
return strings.join(' ');
|
||||
}
|
||||
|
||||
const args = strings.slice(1);
|
||||
const arguments_ = strings.slice(1);
|
||||
const parts = [firstString.raw[0]];
|
||||
|
||||
for (let i = 1; i < firstString.length; i++) {
|
||||
parts.push(
|
||||
String(args[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
||||
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
||||
String(firstString.raw[i])
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@ export type ColorSupport = {|
|
|||
export interface Chalk {
|
||||
(...text: string[]): string,
|
||||
(text: TemplateStringsArray, ...placeholders: mixed[]): string,
|
||||
constructor(options?: Options): Chalk,
|
||||
Instance(options?: Options): Chalk,
|
||||
enabled: boolean,
|
||||
level: Level,
|
||||
rgb(red: number, green: number, blue: number): Chalk,
|
||||
|
|
|
|||
|
|
@ -16,8 +16,8 @@ expectType<boolean>(chalk.supportsColor.has256);
|
|||
expectType<boolean>(chalk.supportsColor.has16m);
|
||||
|
||||
// - Chalk -
|
||||
// -- Constructor --
|
||||
expectType<Chalk>(new chalk.constructor({level: 1}));
|
||||
// -- Instance --
|
||||
expectType<Chalk>(new chalk.Instance({level: 1}));
|
||||
|
||||
// -- Properties --
|
||||
expectType<boolean>(chalk.enabled);
|
||||
|
|
|
|||
16
package.json
16
package.json
|
|
@ -5,7 +5,7 @@
|
|||
"license": "MIT",
|
||||
"repository": "chalk/chalk",
|
||||
"engines": {
|
||||
"node": ">=6"
|
||||
"node": ">=8"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && nyc ava && tsd-check && flow",
|
||||
|
|
@ -43,20 +43,20 @@
|
|||
"dependencies": {
|
||||
"ansi-styles": "^3.2.1",
|
||||
"escape-string-regexp": "^1.0.5",
|
||||
"supports-color": "^6.0.0"
|
||||
"supports-color": "^6.1.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@sindresorhus/tsconfig": "^0.1.1",
|
||||
"ava": "^1.0.1",
|
||||
"coveralls": "^3.0.2",
|
||||
"@sindresorhus/tsconfig": "^0.2.1",
|
||||
"ava": "^1.3.1",
|
||||
"coveralls": "^3.0.3",
|
||||
"execa": "^1.0.0",
|
||||
"flow-bin": "^0.89.0",
|
||||
"flow-bin": "^0.94.0",
|
||||
"import-fresh": "^3.0.0",
|
||||
"matcha": "^0.7.0",
|
||||
"nyc": "^13.1.0",
|
||||
"nyc": "^13.3.0",
|
||||
"resolve-from": "^4.0.0",
|
||||
"tsd-check": "^0.3.0",
|
||||
"xo": "^0.23.0"
|
||||
"xo": "^0.24.0"
|
||||
},
|
||||
"types": "index.d.ts",
|
||||
"xo": {
|
||||
|
|
|
|||
|
|
@ -140,12 +140,12 @@ Multiple arguments will be separated by space.
|
|||
|
||||
Color support is automatically detected, as is the level (see `chalk.level`). However, if you'd like to simply enable/disable Chalk, you can do so via the `.enabled` property. When `chalk.enabled` is `true`, `chalk.level` must *also* be greater than `0` for colored output to be produced.
|
||||
|
||||
Chalk is enabled by default unless explicitly disabled via the constructor or `chalk.level` is `0`.
|
||||
Chalk is enabled by default unless explicitly disabled via `new chalk.Instance()` or `chalk.level` is `0`.
|
||||
|
||||
If you need to change this in a reusable module, create a new instance:
|
||||
|
||||
```js
|
||||
const ctx = new chalk.constructor({enabled: false});
|
||||
const ctx = new chalk.Instance({enabled: false});
|
||||
```
|
||||
|
||||
### chalk.level
|
||||
|
|
@ -155,7 +155,7 @@ Color support is automatically detected, but you can override it by setting the
|
|||
If you need to change this in a reusable module, create a new instance:
|
||||
|
||||
```js
|
||||
const ctx = new chalk.constructor({level: 0});
|
||||
const ctx = new chalk.Instance({level: 0});
|
||||
```
|
||||
|
||||
Levels are as follows:
|
||||
|
|
|
|||
21
templates.js
21
templates.js
|
|
@ -25,9 +25,9 @@ function unescape(c) {
|
|||
return ESCAPES.get(c) || c;
|
||||
}
|
||||
|
||||
function parseArguments(name, args) {
|
||||
function parseArguments(name, arguments_) {
|
||||
const results = [];
|
||||
const chunks = args.trim().split(/\s*,\s*/g);
|
||||
const chunks = arguments_.trim().split(/\s*,\s*/g);
|
||||
let matches;
|
||||
|
||||
for (const chunk of chunks) {
|
||||
|
|
@ -51,7 +51,7 @@ function parseStyle(style) {
|
|||
let matches;
|
||||
|
||||
while ((matches = STYLE_REGEX.exec(style)) !== null) {
|
||||
const name = matches[1]; // eslint-disable-line prefer-destructuring
|
||||
const name = matches[1];
|
||||
|
||||
if (matches[2]) {
|
||||
const args = parseArguments(name, matches[2]);
|
||||
|
|
@ -74,9 +74,8 @@ function buildStyle(chalk, styles) {
|
|||
}
|
||||
|
||||
let current = chalk;
|
||||
// TODO: Use `Object.entries` when targeting Node.js 8
|
||||
for (const styleName of Object.keys(enabled)) {
|
||||
if (!Array.isArray(enabled[styleName])) {
|
||||
for (const [styleName, styles] of Object.entries(enabled)) {
|
||||
if (!Array.isArray(styles)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
|
|
@ -84,23 +83,19 @@ function buildStyle(chalk, styles) {
|
|||
throw new Error(`Unknown Chalk style: ${styleName}`);
|
||||
}
|
||||
|
||||
if (enabled[styleName].length > 0) {
|
||||
current = current[styleName](...enabled[styleName]);
|
||||
} else {
|
||||
current = current[styleName];
|
||||
}
|
||||
current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
|
||||
}
|
||||
|
||||
return current;
|
||||
}
|
||||
|
||||
module.exports = (chalk, tmp) => {
|
||||
module.exports = (chalk, temporary) => {
|
||||
const styles = [];
|
||||
const chunks = [];
|
||||
let chunk = [];
|
||||
|
||||
// eslint-disable-next-line max-params
|
||||
tmp.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
|
||||
temporary.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
|
||||
if (escapeCharacter) {
|
||||
chunk.push(unescape(escapeCharacter));
|
||||
} else if (style) {
|
||||
|
|
|
|||
|
|
@ -2,12 +2,12 @@
|
|||
import chalk from '..';
|
||||
|
||||
// $ExpectError (Can't have typo in option name)
|
||||
chalk.constructor({levl: 1});
|
||||
chalk.constructor({level: 1});
|
||||
new chalk.Instance({levl: 1});
|
||||
new chalk.Instance({level: 1});
|
||||
|
||||
// $ExpectError (Option must have proper type)
|
||||
new chalk.constructor({enabled: 'true'});
|
||||
new chalk.constructor({enabled: true});
|
||||
new chalk.Instance({enabled: 'true'});
|
||||
new chalk.Instance({enabled: true});
|
||||
|
||||
// $ExpectError (Can't have typo in chalk method)
|
||||
chalk.rd('foo');
|
||||
|
|
@ -22,8 +22,8 @@ chalk.red.bgBlu.underline('foo');
|
|||
chalk.red.bgBlue.underline('foo');
|
||||
|
||||
// $ExpectError (Level must be 0, 1, 2, or 3)
|
||||
const badCtx = chalk.constructor({level: 4});
|
||||
const ctx = chalk.constructor({level: 3});
|
||||
const badCtx = chalk.Instance({level: 4});
|
||||
const ctx = chalk.Instance({level: 3});
|
||||
|
||||
// $ExpectError (Can't have typo in method name)
|
||||
ctx.gry('foo');
|
||||
|
|
@ -41,7 +41,7 @@ chalk.enabled = true;
|
|||
chalk.level = 10;
|
||||
chalk.level = 1;
|
||||
|
||||
const chalkInstance = new chalk.constructor();
|
||||
const chalkInstance = new chalk.Instance();
|
||||
|
||||
// $ExpectError (Can't have typo in method name)
|
||||
chalkInstance.blu('foo');
|
||||
|
|
|
|||
|
|
@ -83,17 +83,17 @@ test('line breaks should open and close colors', t => {
|
|||
});
|
||||
|
||||
test('properly convert RGB to 16 colors on basic color terminals', t => {
|
||||
t.is(new chalk.constructor({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
|
||||
t.is(new chalk.constructor({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
|
||||
t.is(new chalk.Instance({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
|
||||
t.is(new chalk.Instance({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
|
||||
});
|
||||
|
||||
test('properly convert RGB to 256 colors on basic color terminals', t => {
|
||||
t.is(new chalk.constructor({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
|
||||
t.is(new chalk.constructor({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
|
||||
t.is(new chalk.constructor({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m');
|
||||
t.is(new chalk.Instance({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
|
||||
t.is(new chalk.Instance({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
|
||||
t.is(new chalk.Instance({level: 3}).bgHex('#FF0000')('hello'), '\u001B[48;2;255;0;0mhello\u001B[49m');
|
||||
});
|
||||
|
||||
test('don\'t emit RGB codes if level is 0', t => {
|
||||
t.is(new chalk.constructor({level: 0}).hex('#FF0000')('hello'), 'hello');
|
||||
t.is(new chalk.constructor({level: 0}).bgHex('#FF0000')('hello'), 'hello');
|
||||
t.is(new chalk.Instance({level: 0}).hex('#FF0000')('hello'), 'hello');
|
||||
t.is(new chalk.Instance({level: 0}).bgHex('#FF0000')('hello'), 'hello');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -1,34 +1,15 @@
|
|||
import test from 'ava';
|
||||
|
||||
// Spoof supports-color
|
||||
require('./_supports-color')(__dirname);
|
||||
|
||||
const chalk = require('..');
|
||||
|
||||
test('create an isolated context where colors can be disabled (by level)', t => {
|
||||
const instance = new chalk.constructor({level: 0, enabled: true});
|
||||
t.is(instance.red('foo'), 'foo');
|
||||
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
instance.level = 2;
|
||||
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
});
|
||||
test('Chalk.constructor should throw an expected error', t => {
|
||||
const expectedError = t.throws(() => {
|
||||
chalk.constructor();
|
||||
});
|
||||
|
||||
test('create an isolated context where colors can be disabled (by enabled flag)', t => {
|
||||
const instance = new chalk.constructor({enabled: false});
|
||||
t.is(instance.red('foo'), 'foo');
|
||||
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
instance.enabled = true;
|
||||
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
});
|
||||
|
||||
test('the `level` option should be a number from 0 to 3', t => {
|
||||
/* eslint-disable no-new */
|
||||
t.throws(() => {
|
||||
new chalk.constructor({level: 10});
|
||||
}, /should be an integer from 0 to 3/);
|
||||
t.is(expectedError.message, '`chalk.constructor()` is deprecated. Use `new chalk.Instance()` instead.');
|
||||
|
||||
t.throws(() => {
|
||||
new chalk.constructor({level: -1});
|
||||
}, /should be an integer from 0 to 3/);
|
||||
/* eslint-enable no-new */
|
||||
new chalk.constructor(); // eslint-disable-line no-new
|
||||
});
|
||||
});
|
||||
|
|
|
|||
34
test/instance.js
Normal file
34
test/instance.js
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import test from 'ava';
|
||||
|
||||
// Spoof supports-color
|
||||
require('./_supports-color')(__dirname);
|
||||
|
||||
const chalk = require('..');
|
||||
|
||||
test('create an isolated context where colors can be disabled (by level)', t => {
|
||||
const instance = new chalk.Instance({level: 0, enabled: true});
|
||||
t.is(instance.red('foo'), 'foo');
|
||||
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
instance.level = 2;
|
||||
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
});
|
||||
|
||||
test('create an isolated context where colors can be disabled (by enabled flag)', t => {
|
||||
const instance = new chalk.Instance({enabled: false});
|
||||
t.is(instance.red('foo'), 'foo');
|
||||
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
instance.enabled = true;
|
||||
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
});
|
||||
|
||||
test('the `level` option should be a number from 0 to 3', t => {
|
||||
/* eslint-disable no-new */
|
||||
t.throws(() => {
|
||||
new chalk.Instance({level: 10});
|
||||
}, /should be an integer from 0 to 3/);
|
||||
|
||||
t.throws(() => {
|
||||
new chalk.Instance({level: -1});
|
||||
}, /should be an integer from 0 to 3/);
|
||||
/* eslint-enable no-new */
|
||||
});
|
||||
|
|
@ -7,23 +7,23 @@ require('./_supports-color')(__dirname);
|
|||
const chalk = require('..');
|
||||
|
||||
test('return an empty string for an empty literal', t => {
|
||||
const instance = chalk.constructor();
|
||||
const instance = new chalk.Instance();
|
||||
t.is(instance``, '');
|
||||
});
|
||||
|
||||
test('return a regular string for a literal with no templates', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`hello`, 'hello');
|
||||
});
|
||||
|
||||
test('correctly perform template parsing', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`{bold Hello, {cyan World!} This is a} test. {green Woo!}`,
|
||||
instance.bold('Hello,', instance.cyan('World!'), 'This is a') + ' test. ' + instance.green('Woo!'));
|
||||
});
|
||||
|
||||
test('correctly perform template substitutions', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
const name = 'Sindre';
|
||||
const exclamation = 'Neat';
|
||||
t.is(instance`{bold Hello, {cyan.inverse ${name}!} This is a} test. {green ${exclamation}!}`,
|
||||
|
|
@ -31,7 +31,7 @@ test('correctly perform template substitutions', t => {
|
|||
});
|
||||
|
||||
test('correctly parse and evaluate color-convert functions', t => {
|
||||
const instance = chalk.constructor({level: 3});
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
t.is(instance`{bold.rgb(144,10,178).inverse Hello, {~inverse there!}}`,
|
||||
'\u001B[1m\u001B[38;2;144;10;178m\u001B[7mHello, ' +
|
||||
'\u001B[27m\u001B[39m\u001B[22m\u001B[1m' +
|
||||
|
|
@ -44,13 +44,13 @@ test('correctly parse and evaluate color-convert functions', t => {
|
|||
});
|
||||
|
||||
test('properly handle escapes', t => {
|
||||
const instance = chalk.constructor({level: 3});
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
t.is(instance`{bold hello \{in brackets\}}`,
|
||||
'\u001B[1mhello {in brackets}\u001B[22m');
|
||||
});
|
||||
|
||||
test('throw if there is an unclosed block', t => {
|
||||
const instance = chalk.constructor({level: 3});
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
try {
|
||||
console.log(instance`{bold this shouldn't appear ever\}`);
|
||||
t.fail();
|
||||
|
|
@ -67,7 +67,7 @@ test('throw if there is an unclosed block', t => {
|
|||
});
|
||||
|
||||
test('throw if there is an invalid style', t => {
|
||||
const instance = chalk.constructor({level: 3});
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
try {
|
||||
console.log(instance`{abadstylethatdoesntexist this shouldn't appear ever}`);
|
||||
t.fail();
|
||||
|
|
@ -77,7 +77,7 @@ test('throw if there is an invalid style', t => {
|
|||
});
|
||||
|
||||
test('properly style multiline color blocks', t => {
|
||||
const instance = chalk.constructor({level: 3});
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
t.is(
|
||||
instance`{bold
|
||||
Hello! This is a
|
||||
|
|
@ -97,49 +97,49 @@ test('properly style multiline color blocks', t => {
|
|||
});
|
||||
|
||||
test('escape interpolated values', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`Hello {bold hi}`, 'Hello hi');
|
||||
t.is(instance`Hello ${'{bold hi}'}`, 'Hello {bold hi}');
|
||||
});
|
||||
|
||||
test('allow custom colors (themes) on custom contexts', t => {
|
||||
const instance = chalk.constructor({level: 3});
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
instance.rose = instance.hex('#F6D9D9');
|
||||
t.is(instance`Hello, {rose Rose}.`, 'Hello, \u001B[38;2;246;217;217mRose\u001B[39m.');
|
||||
});
|
||||
|
||||
test('correctly parse newline literals (bug #184)', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`Hello
|
||||
{red there}`, 'Hello\nthere');
|
||||
});
|
||||
|
||||
test('correctly parse newline escapes (bug #177)', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`Hello\nthere!`, 'Hello\nthere!');
|
||||
});
|
||||
|
||||
test('correctly parse escape in parameters (bug #177 comment 318622809)', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
const str = '\\';
|
||||
t.is(instance`{blue ${str}}`, '\\');
|
||||
});
|
||||
|
||||
test('correctly parses unicode/hex escapes', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`\u0078ylophones are fo\x78y! {magenta.inverse \u0078ylophones are fo\x78y!}`,
|
||||
'xylophones are foxy! xylophones are foxy!');
|
||||
});
|
||||
|
||||
test('correctly parses string arguments', t => {
|
||||
const instance = chalk.constructor({level: 3});
|
||||
const instance = new chalk.Instance({level: 3});
|
||||
t.is(instance`{keyword('black').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
t.is(instance`{keyword('blac\x6B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
t.is(instance`{keyword('blac\u006B').bold can haz cheezburger}`, '\u001B[38;2;0;0;0m\u001B[1mcan haz cheezburger\u001B[22m\u001B[39m');
|
||||
});
|
||||
|
||||
test('throws if a bad argument is encountered', t => {
|
||||
const instance = chalk.constructor({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
|
||||
const instance = new chalk.Instance({level: 3}); // Keep level at least 1 in case we optimize for disabled chalk instances
|
||||
try {
|
||||
console.log(instance`{keyword(????) hi}`);
|
||||
t.fail();
|
||||
|
|
@ -149,7 +149,7 @@ test('throws if a bad argument is encountered', t => {
|
|||
});
|
||||
|
||||
test('throws if an extra unescaped } is found', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
try {
|
||||
console.log(instance`{red hi!}}`);
|
||||
t.fail();
|
||||
|
|
@ -159,12 +159,12 @@ test('throws if an extra unescaped } is found', t => {
|
|||
});
|
||||
|
||||
test('should not parse upper-case escapes', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`\N\n\T\t\X07\x07\U000A\u000A\U000a\u000a`, 'N\nT\tX07\x07U000A\u000AU000a\u000A');
|
||||
});
|
||||
|
||||
test('should properly handle undefined template interpolated values', t => {
|
||||
const instance = chalk.constructor({level: 0});
|
||||
const instance = new chalk.Instance({level: 0});
|
||||
t.is(instance`hello ${undefined}`, 'hello undefined');
|
||||
t.is(instance`hello ${null}`, 'hello null');
|
||||
});
|
||||
|
|
|
|||
|
|
@ -6,25 +6,25 @@ require('./_supports-color')(__dirname);
|
|||
const chalk = require('..');
|
||||
|
||||
test('visible: normal output when enabled', t => {
|
||||
const instance = new chalk.constructor({level: 3, enabled: true});
|
||||
const instance = new chalk.Instance({level: 3, enabled: true});
|
||||
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
});
|
||||
|
||||
test('visible: no output when disabled', t => {
|
||||
const instance = new chalk.constructor({level: 3, enabled: false});
|
||||
const instance = new chalk.Instance({level: 3, enabled: false});
|
||||
t.is(instance.red.visible('foo'), '');
|
||||
t.is(instance.visible.red('foo'), '');
|
||||
});
|
||||
|
||||
test('visible: no output when level is too low', t => {
|
||||
const instance = new chalk.constructor({level: 0, enabled: true});
|
||||
const instance = new chalk.Instance({level: 0, enabled: true});
|
||||
t.is(instance.visible.red('foo'), '');
|
||||
t.is(instance.red.visible('foo'), '');
|
||||
});
|
||||
|
||||
test('test switching back and forth between enabled and disabled', t => {
|
||||
const instance = new chalk.constructor({level: 3, enabled: true});
|
||||
const instance = new chalk.Instance({level: 3, enabled: true});
|
||||
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
t.is(instance.visible.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
t.is(instance.red.visible('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue