forked from orbit-oss/chalk
Require Node.js 12 and move to ESM
This commit is contained in:
parent
4dab5e1fb6
commit
fa16f4ec37
22 changed files with 362 additions and 412 deletions
3
.github/workflows/main.yml
vendored
3
.github/workflows/main.yml
vendored
|
|
@ -12,10 +12,9 @@ jobs:
|
||||||
node-version:
|
node-version:
|
||||||
- 14
|
- 14
|
||||||
- 12
|
- 12
|
||||||
- 10
|
|
||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v2
|
- uses: actions/checkout@v2
|
||||||
- uses: actions/setup-node@v1
|
- uses: actions/setup-node@v2
|
||||||
with:
|
with:
|
||||||
node-version: ${{ matrix.node-version }}
|
node-version: ${{ matrix.node-version }}
|
||||||
- run: npm install
|
- run: npm install
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
/* globals suite, bench */
|
/* globals suite, bench */
|
||||||
'use strict';
|
import chalk from './index.js';
|
||||||
const chalk = require('.');
|
|
||||||
|
|
||||||
suite('chalk', () => {
|
suite('chalk', () => {
|
||||||
const chalkRed = chalk.red;
|
const chalkRed = chalk.red;
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,4 @@
|
||||||
'use strict';
|
import chalk from '../index.js';
|
||||||
const chalk = require('..');
|
|
||||||
|
|
||||||
const ignoreChars = /[^!-~]/g;
|
const ignoreChars = /[^!-~]/g;
|
||||||
|
|
||||||
|
|
@ -17,7 +16,7 @@ function rainbow(string, offset) {
|
||||||
let hue = offset % 360;
|
let hue = offset % 360;
|
||||||
const characters = [];
|
const characters = [];
|
||||||
for (const character of string) {
|
for (const character of string) {
|
||||||
if (character.match(ignoreChars)) {
|
if (ignoreChars.test(character)) {
|
||||||
characters.push(character);
|
characters.push(character);
|
||||||
} else {
|
} else {
|
||||||
characters.push(chalk.hsl(hue, 100, 50)(character));
|
characters.push(chalk.hsl(hue, 100, 50)(character));
|
||||||
|
|
@ -30,8 +29,8 @@ function rainbow(string, offset) {
|
||||||
|
|
||||||
async function animateString(string) {
|
async function animateString(string) {
|
||||||
console.log();
|
console.log();
|
||||||
for (let i = 0; i < 360 * 5; i++) {
|
for (let index = 0; index < 360 * 5; index++) {
|
||||||
console.log('\u001B[1F\u001B[G', rainbow(string, i));
|
console.log('\u001B[1F\u001B[G', rainbow(string, index));
|
||||||
await delay(2); // eslint-disable-line no-await-in-loop
|
await delay(2); // eslint-disable-line no-await-in-loop
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
'use strict';
|
import styles from 'ansi-styles';
|
||||||
const styles = require('ansi-styles');
|
import chalk from '../index.js';
|
||||||
const chalk = require('..');
|
|
||||||
|
|
||||||
// Generates screenshot
|
// Generates screenshot
|
||||||
for (const key of Object.keys(styles)) {
|
for (const key of Object.keys(styles)) {
|
||||||
|
|
|
||||||
56
index.d.ts
vendored
56
index.d.ts
vendored
|
|
@ -3,7 +3,7 @@ Basic foreground colors.
|
||||||
|
|
||||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||||
*/
|
*/
|
||||||
declare type ForegroundColor =
|
export type ForegroundColor =
|
||||||
| 'black'
|
| 'black'
|
||||||
| 'red'
|
| 'red'
|
||||||
| 'green'
|
| 'green'
|
||||||
|
|
@ -28,7 +28,7 @@ Basic background colors.
|
||||||
|
|
||||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||||
*/
|
*/
|
||||||
declare type BackgroundColor =
|
export type BackgroundColor =
|
||||||
| 'bgBlack'
|
| 'bgBlack'
|
||||||
| 'bgRed'
|
| 'bgRed'
|
||||||
| 'bgGreen'
|
| 'bgGreen'
|
||||||
|
|
@ -53,9 +53,9 @@ Basic colors.
|
||||||
|
|
||||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||||
*/
|
*/
|
||||||
declare type Color = ForegroundColor | BackgroundColor;
|
export type Color = ForegroundColor | BackgroundColor;
|
||||||
|
|
||||||
declare type Modifiers =
|
export type Modifiers =
|
||||||
| 'reset'
|
| 'reset'
|
||||||
| 'bold'
|
| 'bold'
|
||||||
| 'dim'
|
| 'dim'
|
||||||
|
|
@ -66,7 +66,6 @@ declare type Modifiers =
|
||||||
| 'strikethrough'
|
| 'strikethrough'
|
||||||
| 'visible';
|
| 'visible';
|
||||||
|
|
||||||
declare namespace chalk {
|
|
||||||
/**
|
/**
|
||||||
Levels:
|
Levels:
|
||||||
- `0` - All colors disabled.
|
- `0` - All colors disabled.
|
||||||
|
|
@ -74,9 +73,9 @@ declare namespace chalk {
|
||||||
- `2` - ANSI 256 colors support.
|
- `2` - ANSI 256 colors support.
|
||||||
- `3` - Truecolor 16 million colors support.
|
- `3` - Truecolor 16 million colors support.
|
||||||
*/
|
*/
|
||||||
type Level = 0 | 1 | 2 | 3;
|
export type ColorSupportLevel = 0 | 1 | 2 | 3;
|
||||||
|
|
||||||
interface Options {
|
export interface Options {
|
||||||
/**
|
/**
|
||||||
Specify the color support for Chalk.
|
Specify the color support for Chalk.
|
||||||
|
|
||||||
|
|
@ -88,22 +87,22 @@ declare namespace chalk {
|
||||||
- `2` - ANSI 256 colors support.
|
- `2` - ANSI 256 colors support.
|
||||||
- `3` - Truecolor 16 million colors support.
|
- `3` - Truecolor 16 million colors support.
|
||||||
*/
|
*/
|
||||||
level?: Level;
|
readonly level?: ColorSupportLevel;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Return a new Chalk instance.
|
Return a new Chalk instance.
|
||||||
*/
|
*/
|
||||||
type Instance = new (options?: Options) => Chalk;
|
export type ChalkInstance = new (options?: Options) => Chalk;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Detect whether the terminal supports color.
|
Detect whether the terminal supports color.
|
||||||
*/
|
*/
|
||||||
interface ColorSupport {
|
export interface ColorSupport {
|
||||||
/**
|
/**
|
||||||
The color level used by Chalk.
|
The color level used by Chalk.
|
||||||
*/
|
*/
|
||||||
level: Level;
|
level: ColorSupportLevel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Return whether Chalk supports basic 16 colors.
|
Return whether Chalk supports basic 16 colors.
|
||||||
|
|
@ -129,7 +128,7 @@ declare namespace chalk {
|
||||||
|
|
||||||
@example
|
@example
|
||||||
```
|
```
|
||||||
import chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
log(chalk`
|
log(chalk`
|
||||||
CPU: {red ${cpu.totalPercent}%}
|
CPU: {red ${cpu.totalPercent}%}
|
||||||
|
|
@ -140,7 +139,7 @@ declare namespace chalk {
|
||||||
|
|
||||||
@example
|
@example
|
||||||
```
|
```
|
||||||
import chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
log(chalk.red.bgBlack`2 + 3 = {bold ${2 + 3}}`)
|
log(chalk.red.bgBlack`2 + 3 = {bold ${2 + 3}}`)
|
||||||
```
|
```
|
||||||
|
|
@ -150,11 +149,11 @@ declare namespace chalk {
|
||||||
(...text: unknown[]): string;
|
(...text: unknown[]): string;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Chalk extends ChalkFunction {
|
export interface Chalk extends ChalkFunction {
|
||||||
/**
|
/**
|
||||||
Return a new Chalk instance.
|
Return a new Chalk instance.
|
||||||
*/
|
*/
|
||||||
Instance: Instance;
|
Instance: ChalkInstance;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
The color support for Chalk.
|
The color support for Chalk.
|
||||||
|
|
@ -167,7 +166,7 @@ declare namespace chalk {
|
||||||
- `2` - ANSI 256 colors support.
|
- `2` - ANSI 256 colors support.
|
||||||
- `3` - Truecolor 16 million colors support.
|
- `3` - Truecolor 16 million colors support.
|
||||||
*/
|
*/
|
||||||
level: Level;
|
level: ColorSupportLevel;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Use HEX value to set text color.
|
Use HEX value to set text color.
|
||||||
|
|
@ -176,7 +175,7 @@ declare namespace chalk {
|
||||||
|
|
||||||
@example
|
@example
|
||||||
```
|
```
|
||||||
import chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
chalk.hex('#DEADED');
|
chalk.hex('#DEADED');
|
||||||
```
|
```
|
||||||
|
|
@ -190,7 +189,7 @@ declare namespace chalk {
|
||||||
|
|
||||||
@example
|
@example
|
||||||
```
|
```
|
||||||
import chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
chalk.keyword('orange');
|
chalk.keyword('orange');
|
||||||
```
|
```
|
||||||
|
|
@ -237,7 +236,7 @@ declare namespace chalk {
|
||||||
|
|
||||||
@example
|
@example
|
||||||
```
|
```
|
||||||
import chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
chalk.bgHex('#DEADED');
|
chalk.bgHex('#DEADED');
|
||||||
```
|
```
|
||||||
|
|
@ -251,7 +250,7 @@ declare namespace chalk {
|
||||||
|
|
||||||
@example
|
@example
|
||||||
```
|
```
|
||||||
import chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
chalk.bgKeyword('orange');
|
chalk.bgKeyword('orange');
|
||||||
```
|
```
|
||||||
|
|
@ -394,22 +393,19 @@ declare namespace chalk {
|
||||||
readonly bgCyanBright: Chalk;
|
readonly bgCyanBright: Chalk;
|
||||||
readonly bgWhiteBright: Chalk;
|
readonly bgWhiteBright: Chalk;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Main Chalk object that allows to chain styles together.
|
Main Chalk object that allows to chain styles together.
|
||||||
|
|
||||||
Call the last one as a method with a string argument.
|
Call the last one as a method with a string argument.
|
||||||
|
|
||||||
Order doesn't matter, and later styles take precedent in case of a conflict.
|
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`.
|
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
|
||||||
*/
|
*/
|
||||||
declare const chalk: chalk.Chalk & chalk.ChalkFunction & {
|
declare const chalk: Chalk & ChalkFunction & {
|
||||||
supportsColor: chalk.ColorSupport | false;
|
supportsColor: ColorSupport | false;
|
||||||
Level: chalk.Level;
|
stderr: Chalk & {supportsColor: ColorSupport | false};
|
||||||
Color: Color;
|
|
||||||
ForegroundColor: ForegroundColor;
|
|
||||||
BackgroundColor: BackgroundColor;
|
|
||||||
Modifiers: Modifiers;
|
|
||||||
stderr: chalk.Chalk & {supportsColor: chalk.ColorSupport | false};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export = chalk;
|
export default chalk;
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,11 @@
|
||||||
import {expectType, expectAssignable, expectError} from 'tsd';
|
import {expectType, expectAssignable, expectError} from 'tsd';
|
||||||
import chalk = require('.');
|
import chalk, {Chalk, Color, ColorSupport, ColorSupportLevel} from './index.js';
|
||||||
|
|
||||||
// - Helpers -
|
// - Helpers -
|
||||||
type colorReturn = chalk.Chalk & {supportsColor?: never};
|
type colorReturn = Chalk & {supportsColor?: never};
|
||||||
|
|
||||||
// - supportsColor -
|
// - supportsColor -
|
||||||
expectType<chalk.ColorSupport | false>(chalk.supportsColor);
|
expectType<ColorSupport | false>(chalk.supportsColor);
|
||||||
if (chalk.supportsColor) {
|
if (chalk.supportsColor) {
|
||||||
expectType<boolean>(chalk.supportsColor.hasBasic);
|
expectType<boolean>(chalk.supportsColor.hasBasic);
|
||||||
expectType<boolean>(chalk.supportsColor.has256);
|
expectType<boolean>(chalk.supportsColor.has256);
|
||||||
|
|
@ -13,8 +13,8 @@ if (chalk.supportsColor) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// - stderr -
|
// - stderr -
|
||||||
expectAssignable<chalk.Chalk>(chalk.stderr);
|
expectAssignable<Chalk>(chalk.stderr);
|
||||||
expectType<chalk.ColorSupport | false>(chalk.stderr.supportsColor);
|
expectType<ColorSupport | false>(chalk.stderr.supportsColor);
|
||||||
if (chalk.stderr.supportsColor) {
|
if (chalk.stderr.supportsColor) {
|
||||||
expectType<boolean>(chalk.stderr.supportsColor.hasBasic);
|
expectType<boolean>(chalk.stderr.supportsColor.hasBasic);
|
||||||
expectType<boolean>(chalk.stderr.supportsColor.has256);
|
expectType<boolean>(chalk.stderr.supportsColor.has256);
|
||||||
|
|
@ -29,10 +29,10 @@ expectError(chalk.reset.supportsColor);
|
||||||
|
|
||||||
// - Chalk -
|
// - Chalk -
|
||||||
// -- Instance --
|
// -- Instance --
|
||||||
expectType<chalk.Chalk>(new chalk.Instance({level: 1}));
|
expectType<Chalk>(new chalk.Instance({level: 1}));
|
||||||
|
|
||||||
// -- Properties --
|
// -- Properties --
|
||||||
expectType<chalk.Level>(chalk.level);
|
expectType<ColorSupportLevel>(chalk.level);
|
||||||
|
|
||||||
// -- Template literal --
|
// -- Template literal --
|
||||||
expectType<string>(chalk``);
|
expectType<string>(chalk``);
|
||||||
|
|
@ -158,5 +158,5 @@ expectType<string>(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`);
|
||||||
expectType<string>(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`);
|
expectType<string>(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`);
|
||||||
|
|
||||||
// -- Color types ==
|
// -- Color types ==
|
||||||
expectAssignable<typeof chalk.Color>('red');
|
expectAssignable<Color>('red');
|
||||||
expectError<typeof chalk.Color>('hotpink');
|
expectError<Color>('hotpink');
|
||||||
|
|
|
||||||
2
license
2
license
|
|
@ -1,6 +1,6 @@
|
||||||
MIT License
|
MIT License
|
||||||
|
|
||||||
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
|
Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (https://sindresorhus.com)
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
||||||
|
|
||||||
|
|
|
||||||
21
package.json
21
package.json
|
|
@ -5,9 +5,10 @@
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": "chalk/chalk",
|
"repository": "chalk/chalk",
|
||||||
"funding": "https://github.com/chalk/chalk?sponsor=1",
|
"funding": "https://github.com/chalk/chalk?sponsor=1",
|
||||||
"main": "source",
|
"type": "module",
|
||||||
|
"exports": "./source/index.js",
|
||||||
"engines": {
|
"engines": {
|
||||||
"node": ">=10"
|
"node": ">=12"
|
||||||
},
|
},
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"test": "xo && nyc ava && tsd",
|
"test": "xo && nyc ava && tsd",
|
||||||
|
|
@ -42,26 +43,20 @@
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ansi-styles": "^4.1.0",
|
"ansi-styles": "^4.1.0",
|
||||||
"supports-color": "^7.1.0"
|
"supports-color": "^9.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"ava": "^2.4.0",
|
"ava": "^3.15.0",
|
||||||
"coveralls": "^3.1.0",
|
"coveralls": "^3.1.0",
|
||||||
"execa": "^4.0.3",
|
"execa": "^5.0.0",
|
||||||
"import-fresh": "^3.2.1",
|
|
||||||
"matcha": "^0.7.0",
|
"matcha": "^0.7.0",
|
||||||
"nyc": "^15.1.0",
|
"nyc": "^15.1.0",
|
||||||
"resolve-from": "^5.0.0",
|
"tsd": "^0.14.0",
|
||||||
"tsd": "^0.13.1",
|
"xo": "^0.38.2"
|
||||||
"xo": "^0.33.1"
|
|
||||||
},
|
},
|
||||||
"xo": {
|
"xo": {
|
||||||
"rules": {
|
"rules": {
|
||||||
"unicorn/prefer-string-slice": "off",
|
"unicorn/prefer-string-slice": "off",
|
||||||
"unicorn/prefer-includes": "off",
|
|
||||||
"@typescript-eslint/member-ordering": "off",
|
|
||||||
"no-redeclare": "off",
|
|
||||||
"unicorn/string-content": "off",
|
|
||||||
"unicorn/better-regex": "off"
|
"unicorn/better-regex": "off"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
17
readme.md
17
readme.md
|
|
@ -79,7 +79,7 @@ $ npm install chalk
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
console.log(chalk.blue('Hello world!'));
|
console.log(chalk.blue('Hello world!'));
|
||||||
```
|
```
|
||||||
|
|
@ -87,7 +87,8 @@ console.log(chalk.blue('Hello world!'));
|
||||||
Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
|
Chalk comes with an easy to use composable API where you just chain and nest the styles you want.
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
const log = console.log;
|
const log = console.log;
|
||||||
|
|
||||||
// Combine styled and normal strings
|
// Combine styled and normal strings
|
||||||
|
|
@ -132,7 +133,7 @@ log(chalk.hex('#DEADED').bold('Bold gray!'));
|
||||||
Easily define your own themes:
|
Easily define your own themes:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
const error = chalk.bold.red;
|
const error = chalk.bold.red;
|
||||||
const warning = chalk.keyword('orange');
|
const warning = chalk.keyword('orange');
|
||||||
|
|
@ -144,6 +145,8 @@ console.log(warning('Warning!'));
|
||||||
Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
|
Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import chalk from 'chalk';
|
||||||
|
|
||||||
const name = 'Sindre';
|
const name = 'Sindre';
|
||||||
console.log(chalk.green('Hello %s'), name);
|
console.log(chalk.green('Hello %s'), name);
|
||||||
//=> 'Hello Sindre'
|
//=> 'Hello Sindre'
|
||||||
|
|
@ -168,7 +171,9 @@ 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:
|
If you need to change this in a reusable module, create a new instance:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const ctx = new chalk.Instance({level: 0});
|
import chalk from 'chalk';
|
||||||
|
|
||||||
|
const customChalk = new chalk.Instance({level: 0});
|
||||||
```
|
```
|
||||||
|
|
||||||
| Level | Description |
|
| Level | Description |
|
||||||
|
|
@ -247,7 +252,7 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
|
||||||
Chalk can be used as a [tagged template literal](https://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
|
Chalk can be used as a [tagged template literal](https://exploringjs.com/es6/ch_template-literals.html#_tagged-template-literals).
|
||||||
|
|
||||||
```js
|
```js
|
||||||
const chalk = require('chalk');
|
import chalk from 'chalk';
|
||||||
|
|
||||||
const miles = 18;
|
const miles = 18;
|
||||||
const calculateFeet = miles => miles * 5280;
|
const calculateFeet = miles => miles * 5280;
|
||||||
|
|
@ -263,6 +268,8 @@ Blocks are delimited by an opening curly brace (`{`), a style, some content, and
|
||||||
Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent:
|
Template styles are chained exactly like normal Chalk styles. The following three statements are equivalent:
|
||||||
|
|
||||||
```js
|
```js
|
||||||
|
import chalk from 'chalk';
|
||||||
|
|
||||||
console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
|
console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
|
||||||
console.log(chalk.bold.rgb(10, 100, 200)`Hello!`);
|
console.log(chalk.bold.rgb(10, 100, 200)`Hello!`);
|
||||||
console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
|
console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
'use strict';
|
import ansiStyles from 'ansi-styles';
|
||||||
const ansiStyles = require('ansi-styles');
|
import supportsColor from 'supports-color';
|
||||||
const {stdout: stdoutColor, stderr: stderrColor} = require('supports-color');
|
import {
|
||||||
const {
|
|
||||||
stringReplaceAll,
|
stringReplaceAll,
|
||||||
stringEncaseCRLFWithFirstIndex
|
stringEncaseCRLFWithFirstIndex
|
||||||
} = require('./util');
|
} from './util.js';
|
||||||
|
import template from './templates.js';
|
||||||
|
|
||||||
|
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
|
||||||
const {isArray} = Array;
|
const {isArray} = Array;
|
||||||
|
|
||||||
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
||||||
|
|
@ -168,7 +169,7 @@ const applyStyle = (self, string) => {
|
||||||
}
|
}
|
||||||
|
|
||||||
const {openAll, closeAll} = styler;
|
const {openAll, closeAll} = styler;
|
||||||
if (string.indexOf('\u001B') !== -1) {
|
if (string.includes('\u001B')) {
|
||||||
while (styler !== undefined) {
|
while (styler !== undefined) {
|
||||||
// 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
|
||||||
|
|
@ -190,7 +191,6 @@ const applyStyle = (self, string) => {
|
||||||
return openAll + string + closeAll;
|
return openAll + string + closeAll;
|
||||||
};
|
};
|
||||||
|
|
||||||
let template;
|
|
||||||
const chalkTag = (chalk, ...strings) => {
|
const chalkTag = (chalk, ...strings) => {
|
||||||
const [firstString] = strings;
|
const [firstString] = strings;
|
||||||
|
|
||||||
|
|
@ -210,10 +210,6 @@ const chalkTag = (chalk, ...strings) => {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (template === undefined) {
|
|
||||||
template = require('./templates');
|
|
||||||
}
|
|
||||||
|
|
||||||
return template(chalk, parts.join(''));
|
return template(chalk, parts.join(''));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -224,4 +220,4 @@ chalk.supportsColor = stdoutColor;
|
||||||
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
|
chalk.stderr = Chalk({level: stderrColor ? stderrColor.level : 0}); // eslint-disable-line new-cap
|
||||||
chalk.stderr.supportsColor = stderrColor;
|
chalk.stderr.supportsColor = stderrColor;
|
||||||
|
|
||||||
module.exports = chalk;
|
export default chalk;
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
'use strict';
|
|
||||||
const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
|
const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
|
||||||
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
|
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
|
||||||
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
|
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
|
||||||
|
|
@ -62,7 +61,7 @@ function parseStyle(style) {
|
||||||
|
|
||||||
if (matches[2]) {
|
if (matches[2]) {
|
||||||
const args = parseArguments(name, matches[2]);
|
const args = parseArguments(name, matches[2]);
|
||||||
results.push([name].concat(args));
|
results.push([name, ...args]);
|
||||||
} else {
|
} else {
|
||||||
results.push([name]);
|
results.push([name]);
|
||||||
}
|
}
|
||||||
|
|
@ -96,7 +95,7 @@ function buildStyle(chalk, styles) {
|
||||||
return current;
|
return current;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = (chalk, temporary) => {
|
export default function template(chalk, temporary) {
|
||||||
const styles = [];
|
const styles = [];
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
let chunk = [];
|
let chunk = [];
|
||||||
|
|
@ -126,9 +125,9 @@ module.exports = (chalk, temporary) => {
|
||||||
chunks.push(chunk.join(''));
|
chunks.push(chunk.join(''));
|
||||||
|
|
||||||
if (styles.length > 0) {
|
if (styles.length > 0) {
|
||||||
const errMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
|
const errorMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
|
||||||
throw new Error(errMessage);
|
throw new Error(errorMessage);
|
||||||
}
|
}
|
||||||
|
|
||||||
return chunks.join('');
|
return chunks.join('');
|
||||||
};
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,4 @@
|
||||||
'use strict';
|
export function stringReplaceAll(string, substring, replacer) {
|
||||||
|
|
||||||
const stringReplaceAll = (string, substring, replacer) => {
|
|
||||||
let index = string.indexOf(substring);
|
let index = string.indexOf(substring);
|
||||||
if (index === -1) {
|
if (index === -1) {
|
||||||
return string;
|
return string;
|
||||||
|
|
@ -15,11 +13,11 @@ const stringReplaceAll = (string, substring, replacer) => {
|
||||||
index = string.indexOf(substring, endIndex);
|
index = string.indexOf(substring, endIndex);
|
||||||
} while (index !== -1);
|
} while (index !== -1);
|
||||||
|
|
||||||
returnValue += string.substr(endIndex);
|
returnValue += string.slice(endIndex);
|
||||||
return returnValue;
|
return returnValue;
|
||||||
};
|
}
|
||||||
|
|
||||||
const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
|
export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
|
||||||
let endIndex = 0;
|
let endIndex = 0;
|
||||||
let returnValue = '';
|
let returnValue = '';
|
||||||
do {
|
do {
|
||||||
|
|
@ -29,11 +27,6 @@ const stringEncaseCRLFWithFirstIndex = (string, prefix, postfix, index) => {
|
||||||
index = string.indexOf('\n', endIndex);
|
index = string.indexOf('\n', endIndex);
|
||||||
} while (index !== -1);
|
} while (index !== -1);
|
||||||
|
|
||||||
returnValue += string.substr(endIndex);
|
returnValue += string.slice(endIndex);
|
||||||
return returnValue;
|
return returnValue;
|
||||||
};
|
}
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
stringReplaceAll,
|
|
||||||
stringEncaseCRLFWithFirstIndex
|
|
||||||
};
|
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,3 @@
|
||||||
'use strict';
|
import chalk from '../source/index.js';
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
console.log(`${chalk.hex('#ff6159')('testout')} ${chalk.stderr.hex('#ff6159')('testerr')}`);
|
console.log(`${chalk.hex('#ff6159')('testout')} ${chalk.stderr.hex('#ff6159')('testerr')}`);
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
'use strict';
|
|
||||||
const resolveFrom = require('resolve-from');
|
|
||||||
|
|
||||||
const DEFAULT = {
|
|
||||||
stdout: {
|
|
||||||
level: 3,
|
|
||||||
hasBasic: true,
|
|
||||||
has256: true,
|
|
||||||
has16m: true
|
|
||||||
},
|
|
||||||
stderr: {
|
|
||||||
level: 3,
|
|
||||||
hasBasic: true,
|
|
||||||
has256: true,
|
|
||||||
has16m: true
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
module.exports = (dir, override) => {
|
|
||||||
require.cache[resolveFrom(dir, 'supports-color')] = {exports: override || DEFAULT};
|
|
||||||
};
|
|
||||||
|
|
@ -1,9 +1,8 @@
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
import chalk from '../source/index.js';
|
||||||
|
|
||||||
// Spoof supports-color
|
chalk.level = 3;
|
||||||
require('./_supports-color')(__dirname);
|
chalk.stderr.level = 3;
|
||||||
|
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
console.log('TERM:', process.env.TERM || '[none]');
|
console.log('TERM:', process.env.TERM || '[none]');
|
||||||
console.log('platform:', process.platform || '[unknown]');
|
console.log('platform:', process.platform || '[unknown]');
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
import chalk from '../source/index.js';
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
test('Chalk.constructor should throw an expected error', t => {
|
test('Chalk.constructor should throw an expected error', t => {
|
||||||
const expectedError = t.throws(() => {
|
const expectedError = t.throws(() => {
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
import chalk from '../source/index.js';
|
||||||
|
|
||||||
// Spoof supports-color
|
chalk.level = 1;
|
||||||
require('./_supports-color')(__dirname);
|
|
||||||
|
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
test('create an isolated context where colors can be disabled (by level)', t => {
|
test('create an isolated context where colors can be disabled (by level)', t => {
|
||||||
const instance = new chalk.Instance({level: 0});
|
const instance = new chalk.Instance({level: 0});
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,9 @@
|
||||||
import path from 'path';
|
import {fileURLToPath} from 'url';
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
import execa from 'execa';
|
import execa from 'execa';
|
||||||
|
import chalk from '../source/index.js';
|
||||||
|
|
||||||
// Spoof supports-color
|
chalk.level = 1;
|
||||||
require('./_supports-color')(__dirname);
|
|
||||||
|
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
test('don\'t output colors when manually disabled', t => {
|
test('don\'t output colors when manually disabled', t => {
|
||||||
const oldLevel = chalk.level;
|
const oldLevel = chalk.level;
|
||||||
|
|
@ -40,6 +38,6 @@ test('propagate enable/disable changes from child colors', t => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test('disable colors if they are not supported', async t => {
|
test('disable colors if they are not supported', async t => {
|
||||||
const {stdout} = await execa.node(path.join(__dirname, '_fixture'));
|
const {stdout} = await execa.node(fileURLToPath(new URL('./_fixture.js', import.meta.url)));
|
||||||
t.is(stdout, 'testout testerr');
|
t.is(stdout, 'testout testerr');
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,22 @@
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
import chalk from '../source/index.js';
|
||||||
|
|
||||||
|
// TODO: Do this when ESM supports loader hooks
|
||||||
// Spoof supports-color
|
// Spoof supports-color
|
||||||
require('./_supports-color')(__dirname, {
|
// require('./_supports-color')(__dirname, {
|
||||||
stdout: {
|
// stdout: {
|
||||||
level: 0,
|
// level: 0,
|
||||||
hasBasic: false,
|
// hasBasic: false,
|
||||||
has256: false,
|
// has256: false,
|
||||||
has16m: false
|
// has16m: false
|
||||||
},
|
// },
|
||||||
stderr: {
|
// stderr: {
|
||||||
level: 0,
|
// level: 0,
|
||||||
hasBasic: false,
|
// hasBasic: false,
|
||||||
has256: false,
|
// has256: false,
|
||||||
has16m: false
|
// has16m: false
|
||||||
}
|
// }
|
||||||
});
|
// });
|
||||||
|
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
test('colors can be forced by using chalk.level', t => {
|
test('colors can be forced by using chalk.level', t => {
|
||||||
chalk.level = 1;
|
chalk.level = 1;
|
||||||
|
|
|
||||||
|
|
@ -1,10 +1,8 @@
|
||||||
/* eslint-disable unicorn/no-hex-escape */
|
/* eslint-disable unicorn/no-hex-escape */
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
import chalk from '../source/index.js';
|
||||||
|
|
||||||
// Spoof supports-color
|
chalk.level = 1;
|
||||||
require('./_supports-color')(__dirname);
|
|
||||||
|
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
test('return an empty string for an empty literal', t => {
|
test('return an empty string for an empty literal', t => {
|
||||||
const instance = new chalk.Instance();
|
const instance = new chalk.Instance();
|
||||||
|
|
|
||||||
|
|
@ -1,9 +1,7 @@
|
||||||
import test from 'ava';
|
import test from 'ava';
|
||||||
|
import chalk from '../source/index.js';
|
||||||
|
|
||||||
// Spoof supports-color
|
chalk.level = 1;
|
||||||
require('./_supports-color')(__dirname);
|
|
||||||
|
|
||||||
const chalk = require('../source');
|
|
||||||
|
|
||||||
test('visible: normal output when level > 0', t => {
|
test('visible: normal output when level > 0', t => {
|
||||||
const instance = new chalk.Instance({level: 3});
|
const instance = new chalk.Instance({level: 3});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue