Compare commits
251 commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
aa06bb5ac3 | ||
|
|
51557784b8 | ||
|
|
5c91505e18 | ||
|
|
a8f5bf7a67 | ||
|
|
67db246ae0 | ||
|
|
79ee2d386c | ||
|
|
5dbc1e2633 | ||
|
|
4ebb62d1ca | ||
|
|
fc809b686d | ||
|
|
83acfcf8cb | ||
|
|
f8381204fb | ||
|
|
4a10354857 | ||
|
|
77126fba13 | ||
|
|
795273f17a | ||
|
|
386909ee0b | ||
|
|
2db3c7701e | ||
|
|
f7b29ae8ef | ||
|
|
f399cd0ff6 | ||
|
|
72c742d471 | ||
|
|
5aafc0af74 | ||
|
|
df0e69df51 | ||
|
|
29b856959f | ||
|
|
a370f468a4 | ||
|
|
7443e9faa0 | ||
|
|
a027e3c1e0 | ||
|
|
79c56dfc1e | ||
|
|
c3d14f9ea8 | ||
|
|
158bf4429e | ||
|
|
a34bcf6327 | ||
|
|
1b4cd21fb1 | ||
|
|
6e0df055f4 | ||
|
|
92c55db46f | ||
|
|
d7d75717b3 | ||
|
|
96f44c976b | ||
|
|
c2311622c3 | ||
|
|
ba5c385ecf | ||
|
|
503b9d35cd | ||
|
|
b17d862944 | ||
|
|
d28690e66b | ||
|
|
bccde97f8a | ||
|
|
85f7e96f75 | ||
|
|
4d5c4795ad | ||
|
|
7a6893551d | ||
|
|
04fdbd6d8d | ||
|
|
d7c4aac07e | ||
|
|
09fd5c4ba8 | ||
|
|
c987c61486 | ||
|
|
f478655c3c | ||
|
|
3761e455a1 | ||
|
|
95217429be | ||
|
|
625a285772 | ||
|
|
48d25d156a | ||
|
|
b68c8b5102 | ||
|
|
f8a3642a81 | ||
|
|
4cf2e40e07 | ||
|
|
0fba91b037 | ||
|
|
d798222a5a | ||
|
|
fa16f4ec37 | ||
|
|
4dab5e1fb6 | ||
|
|
9bf298571e | ||
|
|
2a9abb18f4 | ||
|
|
97a0861848 | ||
|
|
0433fcff2a | ||
|
|
d5fb377019 | ||
|
|
ca330532ec | ||
|
|
e304e8c230 | ||
|
|
02abeebac3 | ||
|
|
72299f99a0 | ||
|
|
c0d8e7ed8a | ||
|
|
b31d6fb48a | ||
|
|
445092f9af | ||
|
|
4c3df88472 | ||
|
|
09ddbadcb5 | ||
|
|
55816cdd4d | ||
|
|
31fa942080 | ||
|
|
61999a4e77 | ||
|
|
f0f4638a92 | ||
|
|
7f21f209da | ||
|
|
63469d30e4 | ||
|
|
797461ee32 | ||
|
|
0e6fecc7f7 | ||
|
|
20002d8bd1 | ||
|
|
b3930189a5 | ||
|
|
18c280da2e | ||
|
|
628b5954cf | ||
|
|
4de1841129 | ||
|
|
fb8e85ab87 | ||
|
|
eef8c8c191 | ||
|
|
48905d0805 | ||
|
|
1953816afd | ||
|
|
61aca7cb76 | ||
|
|
4e65299e7b | ||
|
|
2a53389d72 | ||
|
|
6b4d20683f | ||
|
|
1f77953f1a | ||
|
|
87156ce8e2 | ||
|
|
c25c32a25f | ||
|
|
655653bb0c | ||
|
|
af4a078245 | ||
|
|
c08417e88c | ||
|
|
983094883c | ||
|
|
e2dd171597 | ||
|
|
d82b2a6638 | ||
|
|
4ab4665994 | ||
|
|
4990a3578a | ||
|
|
b3e9b91405 | ||
|
|
d3be9c65b1 | ||
|
|
98628d9f08 | ||
|
|
7b9211be50 | ||
|
|
cd5de7a2f6 | ||
|
|
2ca015c4c5 | ||
|
|
3ef170b457 | ||
|
|
de2f4cd606 | ||
|
|
60959e05cf | ||
|
|
7f6e5630b0 | ||
|
|
587a5fbcbb | ||
|
|
076f0c9eb6 | ||
|
|
1284415013 | ||
|
|
cf6615647a | ||
|
|
4726b1bfda | ||
|
|
90c7a92ff1 | ||
|
|
f590a65489 | ||
|
|
70bb378e8e | ||
|
|
b827cb0817 | ||
|
|
1542c85f1b | ||
|
|
8a090d7885 | ||
|
|
b9c95dbfd2 | ||
|
|
6a14c58e54 | ||
|
|
2a678789b1 | ||
|
|
0307f263cb | ||
|
|
70f22d87ba | ||
|
|
a2b5fa22dc | ||
|
|
a6ad945452 | ||
|
|
48ba5b0b9b | ||
|
|
01cfb5c888 | ||
|
|
fc9a7e369b | ||
|
|
af8b3657e9 | ||
|
|
7c6f83f719 | ||
|
|
58483b5ab1 | ||
|
|
0dae4c28e1 | ||
|
|
84f27d4bd8 | ||
|
|
245dfa5c6f | ||
|
|
e80f02e2dd | ||
|
|
925397a0f5 | ||
|
|
52823001a5 | ||
|
|
12d1276b36 | ||
|
|
576d8d2171 | ||
|
|
ae8a03f2c5 | ||
|
|
b55dd79f8b | ||
|
|
678152cf19 | ||
|
|
011dd0421e | ||
|
|
6554030550 | ||
|
|
bc3dd75329 | ||
|
|
a8c60e37fa | ||
|
|
5392313414 | ||
|
|
14e0aa9772 | ||
|
|
7be154c074 | ||
|
|
e1177ec362 | ||
|
|
e2a4aa4275 | ||
|
|
ede310303b | ||
|
|
6adf5794a3 | ||
|
|
dc092b4a5f | ||
|
|
4372d27f7e | ||
|
|
d86db88e77 | ||
|
|
f0533f655d | ||
|
|
f653b061d6 | ||
|
|
5e6d5fda44 | ||
|
|
7898eda561 | ||
|
|
37db75e1fd | ||
|
|
a9f0c77142 | ||
|
|
38f641a222 | ||
|
|
69ac663f29 | ||
|
|
106f086aaf | ||
|
|
f0c0178d12 | ||
|
|
0827d3b632 | ||
|
|
fbd17e7a69 | ||
|
|
7a75399b72 | ||
|
|
7dbd8c3993 | ||
|
|
4c4ba2df3d | ||
|
|
4c4eb1fd0a | ||
|
|
e8d28f3041 | ||
|
|
5cdd9eddf8 | ||
|
|
23092eefd2 | ||
|
|
8d26c1401c | ||
|
|
5ec90cb354 | ||
|
|
3d10f8fad7 | ||
|
|
5827081719 | ||
|
|
caeb7d8dca | ||
|
|
3fca6150e2 | ||
|
|
f66271e01a | ||
|
|
23ef1c7ca2 | ||
|
|
c0155688cf | ||
|
|
09fb2d8606 | ||
|
|
608242a4fc | ||
|
|
18f2e7c122 | ||
|
|
523b998e64 | ||
|
|
54975fb20d | ||
|
|
1d73b21111 | ||
|
|
6f4d6b30fb | ||
|
|
870249689a | ||
|
|
0412cdf07b | ||
|
|
249b9ac7e7 | ||
|
|
cb3f2308e1 | ||
|
|
dbae68d623 | ||
|
|
9b60021fa6 | ||
|
|
0d2144904b | ||
|
|
5a69476142 | ||
|
|
492f11fb59 | ||
|
|
4ce73b632b | ||
|
|
7c02cf45f8 | ||
|
|
835ca3d950 | ||
|
|
74c087db82 | ||
|
|
d939a3f9bc | ||
|
|
a125eb0e72 | ||
|
|
6c3e57351b | ||
|
|
fe2e4b093e | ||
|
|
2653218312 | ||
|
|
ff2c3ea9a5 | ||
|
|
426fc485bd | ||
|
|
9001b3933a | ||
|
|
308f6b6a1e | ||
|
|
18c64ad7ba | ||
|
|
05f87e25e1 | ||
|
|
139f8b68cc | ||
|
|
51b8f329e8 | ||
|
|
8417f175ab | ||
|
|
a301cbf3d1 | ||
|
|
8ec46cfb35 | ||
|
|
337f9c7485 | ||
|
|
5679ec0c04 | ||
|
|
b8e7da6367 | ||
|
|
7c40059a03 | ||
|
|
c2ef5b8c13 | ||
|
|
04cae226cc | ||
|
|
83615f2ed6 | ||
|
|
ed6e7d56da | ||
|
|
8d599972ee | ||
|
|
262274af86 | ||
|
|
60b0a74adc | ||
|
|
641ae571b4 | ||
|
|
14603cd0f5 | ||
|
|
8b554e254e | ||
|
|
409f95eef5 | ||
|
|
fb6332df4f | ||
|
|
5d2cefc243 | ||
|
|
4006cc0b41 | ||
|
|
6142553bb5 | ||
|
|
789a0118c6 | ||
|
|
388e21aed7 | ||
|
|
d7537f37df | ||
|
|
632bd4a088 |
36 changed files with 2247 additions and 478 deletions
|
|
@ -7,9 +7,6 @@ charset = utf-8
|
|||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[package.json]
|
||||
[*.yml]
|
||||
indent_style = space
|
||||
indent_size = 2
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
|
|
|
|||
2
.gitattributes
vendored
2
.gitattributes
vendored
|
|
@ -1 +1 @@
|
|||
* text=auto
|
||||
* text=auto eol=lf
|
||||
|
|
|
|||
4
.github/funding.yml
vendored
Normal file
4
.github/funding.yml
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
github: [sindresorhus, Qix-]
|
||||
open_collective: sindresorhus
|
||||
tidelift: npm/chalk
|
||||
custom: https://sindresorhus.com/donate
|
||||
3
.github/security.md
vendored
Normal file
3
.github/security.md
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Security Policy
|
||||
|
||||
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure.
|
||||
26
.github/workflows/main.yml
vendored
Normal file
26
.github/workflows/main.yml
vendored
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
name: CI
|
||||
on:
|
||||
- push
|
||||
- pull_request
|
||||
jobs:
|
||||
test:
|
||||
name: Node.js ${{ matrix.node-version }}
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
fail-fast: false
|
||||
matrix:
|
||||
node-version:
|
||||
- 18
|
||||
- 16
|
||||
- 14
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: ${{ matrix.node-version }}
|
||||
- run: npm install
|
||||
- run: npm test
|
||||
- uses: codecov/codecov-action@v2
|
||||
if: matrix.node-version == 16
|
||||
with:
|
||||
fail_ci_if_error: true
|
||||
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -1,3 +1,4 @@
|
|||
node_modules
|
||||
yarn.lock
|
||||
coverage
|
||||
.nyc_output
|
||||
|
|
|
|||
13
.jshintrc
13
.jshintrc
|
|
@ -1,13 +0,0 @@
|
|||
{
|
||||
"node": true,
|
||||
"esnext": true,
|
||||
"bitwise": true,
|
||||
"camelcase": true,
|
||||
"curly": true,
|
||||
"immed": true,
|
||||
"newcap": true,
|
||||
"noarg": true,
|
||||
"undef": true,
|
||||
"unused": "vars",
|
||||
"strict": true
|
||||
}
|
||||
1
.npmrc
Normal file
1
.npmrc
Normal file
|
|
@ -0,0 +1 @@
|
|||
package-lock=false
|
||||
|
|
@ -1,7 +0,0 @@
|
|||
sudo: false
|
||||
language: node_js
|
||||
node_js:
|
||||
- 'iojs'
|
||||
- '0.12'
|
||||
- '0.10'
|
||||
after_success: npm run coveralls
|
||||
55
benchmark.js
55
benchmark.js
|
|
@ -1,22 +1,57 @@
|
|||
var chalk = require('./');
|
||||
/* globals suite, bench */
|
||||
import chalk from './index.js';
|
||||
|
||||
suite('chalk', function () {
|
||||
set('iterations', 100000);
|
||||
suite('chalk', () => {
|
||||
const chalkRed = chalk.red;
|
||||
const chalkBgRed = chalk.bgRed;
|
||||
const chalkBlueBgRed = chalk.blue.bgRed;
|
||||
const chalkBlueBgRedBold = chalk.blue.bgRed.bold;
|
||||
|
||||
bench('single style', function () {
|
||||
const blueStyledString = 'the fox jumps' + chalk.blue('over the lazy dog') + '!';
|
||||
|
||||
bench('1 style', () => {
|
||||
chalk.red('the fox jumps over the lazy dog');
|
||||
});
|
||||
|
||||
bench('several styles', function () {
|
||||
bench('2 styles', () => {
|
||||
chalk.blue.bgRed('the fox jumps over the lazy dog');
|
||||
});
|
||||
|
||||
bench('3 styles', () => {
|
||||
chalk.blue.bgRed.bold('the fox jumps over the lazy dog');
|
||||
});
|
||||
|
||||
var cached = chalk.blue.bgRed.bold;
|
||||
bench('cached styles', function () {
|
||||
cached('the fox jumps over the lazy dog');
|
||||
bench('cached: 1 style', () => {
|
||||
chalkRed('the fox jumps over the lazy dog');
|
||||
});
|
||||
|
||||
bench('nested styles', function () {
|
||||
chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!');
|
||||
bench('cached: 2 styles', () => {
|
||||
chalkBlueBgRed('the fox jumps over the lazy dog');
|
||||
});
|
||||
|
||||
bench('cached: 3 styles', () => {
|
||||
chalkBlueBgRedBold('the fox jumps over the lazy dog');
|
||||
});
|
||||
|
||||
bench('cached: 1 style with newline', () => {
|
||||
chalkRed('the fox jumps\nover the lazy dog');
|
||||
});
|
||||
|
||||
bench('cached: 1 style nested intersecting', () => {
|
||||
chalkRed(blueStyledString);
|
||||
});
|
||||
|
||||
bench('cached: 1 style nested non-intersecting', () => {
|
||||
chalkBgRed(blueStyledString);
|
||||
});
|
||||
|
||||
bench('cached: 1 style template literal', () => {
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
chalkRed`the fox jumps over the lazy dog`;
|
||||
});
|
||||
|
||||
bench('cached: nested styles template literal', () => {
|
||||
// eslint-disable-next-line no-unused-expressions
|
||||
chalkRed`the fox {bold jumps} over the {underline lazy} dog`;
|
||||
});
|
||||
});
|
||||
|
|
|
|||
74
code-of-conduct.md
Normal file
74
code-of-conduct.md
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as
|
||||
contributors and maintainers pledge to making participation in our project and
|
||||
our community a harassment-free experience for everyone, regardless of age, body
|
||||
size, disability, ethnicity, gender identity and expression, level of experience,
|
||||
nationality, personal appearance, race, religion, or sexual identity and
|
||||
orientation.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment
|
||||
include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or
|
||||
advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic
|
||||
address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a
|
||||
professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable
|
||||
behavior and are expected to take appropriate and fair corrective action in
|
||||
response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or
|
||||
reject comments, commits, code, wiki edits, issues, and other contributions
|
||||
that are not aligned to this Code of Conduct, or to ban temporarily or
|
||||
permanently any contributor for other behaviors that they deem inappropriate,
|
||||
threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces
|
||||
when an individual is representing the project or its community. Examples of
|
||||
representing a project or community include using an official project e-mail
|
||||
address, posting via an official social media account, or acting as an appointed
|
||||
representative at an online or offline event. Representation of a project may be
|
||||
further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
||||
reported by contacting the project team at sindresorhus@gmail.com. All
|
||||
complaints will be reviewed and investigated and will result in a response that
|
||||
is deemed necessary and appropriate to the circumstances. The project team is
|
||||
obligated to maintain confidentiality with regard to the reporter of an incident.
|
||||
Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good
|
||||
faith may face temporary or permanent repercussions as determined by other
|
||||
members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
||||
available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
||||
3
contributing.md
Normal file
3
contributing.md
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
# Contributing to Chalk
|
||||
|
||||
Please note that this project is released with a [Contributor Code of Conduct](code-of-conduct.md). By participating in this project you agree to abide by its terms.
|
||||
38
examples/rainbow.js
Normal file
38
examples/rainbow.js
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import {setTimeout as delay} from 'node:timers/promises';
|
||||
import convertColor from 'color-convert';
|
||||
import updateLog from 'log-update';
|
||||
import chalk from '../source/index.js';
|
||||
|
||||
const ignoreChars = /[^!-~]/g;
|
||||
|
||||
function rainbow(string, offset) {
|
||||
if (!string || string.length === 0) {
|
||||
return string;
|
||||
}
|
||||
|
||||
const hueStep = 360 / string.replaceAll(ignoreChars, '').length;
|
||||
|
||||
let hue = offset % 360;
|
||||
const characters = [];
|
||||
for (const character of string) {
|
||||
if (ignoreChars.test(character)) {
|
||||
characters.push(character);
|
||||
} else {
|
||||
characters.push(chalk.hex(convertColor.hsl.hex(hue, 100, 50))(character));
|
||||
hue = (hue + hueStep) % 360;
|
||||
}
|
||||
}
|
||||
|
||||
return characters.join('');
|
||||
}
|
||||
|
||||
async function animateString(string) {
|
||||
for (let index = 0; index < 360 * 5; index++) {
|
||||
updateLog(rainbow(string, index));
|
||||
await delay(2); // eslint-disable-line no-await-in-loop
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
await animateString('We hope you enjoy Chalk! <3');
|
||||
console.log();
|
||||
27
examples/screenshot.js
Normal file
27
examples/screenshot.js
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
import process from 'node:process';
|
||||
import styles from 'ansi-styles';
|
||||
import chalk from '../source/index.js';
|
||||
|
||||
// Generates screenshot
|
||||
for (const key of Object.keys(styles)) {
|
||||
let returnValue = key;
|
||||
|
||||
// We skip `overline` as almost no terminal supports it so we cannot show it off.
|
||||
if (
|
||||
key === 'reset'
|
||||
|| key === 'hidden'
|
||||
|| key === 'grey'
|
||||
|| key === 'bgGray'
|
||||
|| key === 'bgGrey'
|
||||
|| key === 'overline'
|
||||
|| key.endsWith('Bright')
|
||||
) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (/^bg[^B]/.test(key)) {
|
||||
returnValue = chalk.black(returnValue);
|
||||
}
|
||||
|
||||
process.stdout.write(chalk[key](returnValue) + ' ');
|
||||
}
|
||||
116
index.js
116
index.js
|
|
@ -1,116 +0,0 @@
|
|||
'use strict';
|
||||
var escapeStringRegexp = require('escape-string-regexp');
|
||||
var ansiStyles = require('ansi-styles');
|
||||
var stripAnsi = require('strip-ansi');
|
||||
var hasAnsi = require('has-ansi');
|
||||
var supportsColor = require('supports-color');
|
||||
var defineProps = Object.defineProperties;
|
||||
var isSimpleWindowsTerm = process.platform === 'win32' && !/^xterm/i.test(process.env.TERM);
|
||||
|
||||
function Chalk(options) {
|
||||
// detect mode if not set manually
|
||||
this.enabled = !options || options.enabled === undefined ? supportsColor : options.enabled;
|
||||
}
|
||||
|
||||
// use bright blue on Windows as the normal blue color is illegible
|
||||
if (isSimpleWindowsTerm) {
|
||||
ansiStyles.blue.open = '\u001b[94m';
|
||||
}
|
||||
|
||||
var styles = (function () {
|
||||
var ret = {};
|
||||
|
||||
Object.keys(ansiStyles).forEach(function (key) {
|
||||
ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g');
|
||||
|
||||
ret[key] = {
|
||||
get: function () {
|
||||
return build.call(this, this._styles.concat(key));
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return ret;
|
||||
})();
|
||||
|
||||
var proto = defineProps(function chalk() {}, styles);
|
||||
|
||||
function build(_styles) {
|
||||
var builder = function builder() {
|
||||
return applyStyle.apply(builder, arguments);
|
||||
};
|
||||
|
||||
builder._styles = _styles;
|
||||
builder.enabled = this.enabled;
|
||||
// __proto__ is used because we must return a function, but there is
|
||||
// no way to create a function with a different prototype.
|
||||
/*eslint no-proto: 0 */
|
||||
builder.__proto__ = proto;
|
||||
|
||||
return builder;
|
||||
}
|
||||
|
||||
function applyStyle() {
|
||||
// support varags, but simply cast to string in case there's only one arg
|
||||
var args = arguments;
|
||||
var argsLen = args.length;
|
||||
var str = argsLen !== 0 && String(arguments[0]);
|
||||
|
||||
if (argsLen > 1) {
|
||||
// don't slice `arguments`, it prevents v8 optimizations
|
||||
for (var a = 1; a < argsLen; a++) {
|
||||
str += ' ' + args[a];
|
||||
}
|
||||
}
|
||||
|
||||
if (!this.enabled || !str) {
|
||||
return str;
|
||||
}
|
||||
|
||||
var nestedStyles = this._styles;
|
||||
var i = nestedStyles.length;
|
||||
|
||||
// Turns out that on Windows dimmed gray text becomes invisible in cmd.exe,
|
||||
// see https://github.com/chalk/chalk/issues/58
|
||||
// If we're on Windows and we're dealing with a gray color, temporarily make 'dim' a noop.
|
||||
var originalDim = ansiStyles.dim.open;
|
||||
if (isSimpleWindowsTerm && (nestedStyles.indexOf('gray') !== -1 || nestedStyles.indexOf('grey') !== -1)) {
|
||||
ansiStyles.dim.open = '';
|
||||
}
|
||||
|
||||
while (i--) {
|
||||
var code = ansiStyles[nestedStyles[i]];
|
||||
|
||||
// Replace any instances already present with a re-opening code
|
||||
// otherwise only the part of the string until said closing code
|
||||
// will be colored, and the rest will simply be 'plain'.
|
||||
str = code.open + str.replace(code.closeRe, code.open) + code.close;
|
||||
}
|
||||
|
||||
// Reset the original 'dim' if we changed it to work around the Windows dimmed gray issue.
|
||||
ansiStyles.dim.open = originalDim;
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
function init() {
|
||||
var ret = {};
|
||||
|
||||
Object.keys(styles).forEach(function (name) {
|
||||
ret[name] = {
|
||||
get: function () {
|
||||
return build.call(this, [name]);
|
||||
}
|
||||
};
|
||||
});
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
defineProps(Chalk.prototype, init());
|
||||
|
||||
module.exports = new Chalk();
|
||||
module.exports.styles = ansiStyles;
|
||||
module.exports.hasColor = hasAnsi;
|
||||
module.exports.stripColor = stripAnsi;
|
||||
module.exports.supportsColor = supportsColor;
|
||||
22
license
22
license
|
|
@ -1,21 +1,9 @@
|
|||
The MIT License (MIT)
|
||||
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:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
|
|
|||
BIN
media/screenshot.png
Normal file
BIN
media/screenshot.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 235 KiB |
141
package.json
141
package.json
|
|
@ -1,62 +1,83 @@
|
|||
{
|
||||
"name": "chalk",
|
||||
"version": "1.1.0",
|
||||
"description": "Terminal string styling done right. Much color.",
|
||||
"license": "MIT",
|
||||
"repository": "chalk/chalk",
|
||||
"maintainers": [
|
||||
"Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)",
|
||||
"Joshua Appelman <jappelman@xebia.com> (jbnicolai.com)"
|
||||
],
|
||||
"engines": {
|
||||
"node": ">=0.10.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "mocha",
|
||||
"bench": "matcha benchmark.js",
|
||||
"coverage": "nyc npm test && nyc report",
|
||||
"coveralls": "nyc npm test && nyc report --reporter=text-lcov | coveralls"
|
||||
},
|
||||
"files": [
|
||||
"index.js"
|
||||
],
|
||||
"keywords": [
|
||||
"color",
|
||||
"colour",
|
||||
"colors",
|
||||
"terminal",
|
||||
"console",
|
||||
"cli",
|
||||
"string",
|
||||
"str",
|
||||
"ansi",
|
||||
"style",
|
||||
"styles",
|
||||
"tty",
|
||||
"formatting",
|
||||
"rgb",
|
||||
"256",
|
||||
"shell",
|
||||
"xterm",
|
||||
"log",
|
||||
"logging",
|
||||
"command-line",
|
||||
"text"
|
||||
],
|
||||
"dependencies": {
|
||||
"ansi-styles": "^2.1.0",
|
||||
"escape-string-regexp": "^1.0.2",
|
||||
"has-ansi": "^2.0.0",
|
||||
"strip-ansi": "^3.0.0",
|
||||
"supports-color": "^2.0.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"coveralls": "^2.11.2",
|
||||
"matcha": "^0.6.0",
|
||||
"mocha": "*",
|
||||
"nyc": "^3.0.0",
|
||||
"require-uncached": "^1.0.2",
|
||||
"resolve-from": "^1.0.0",
|
||||
"semver": "^4.3.3"
|
||||
}
|
||||
"name": "chalk",
|
||||
"version": "5.6.2",
|
||||
"description": "Terminal string styling done right",
|
||||
"license": "MIT",
|
||||
"repository": "chalk/chalk",
|
||||
"funding": "https://github.com/chalk/chalk?sponsor=1",
|
||||
"type": "module",
|
||||
"main": "./source/index.js",
|
||||
"exports": "./source/index.js",
|
||||
"imports": {
|
||||
"#ansi-styles": "./source/vendor/ansi-styles/index.js",
|
||||
"#supports-color": {
|
||||
"node": "./source/vendor/supports-color/index.js",
|
||||
"default": "./source/vendor/supports-color/browser.js"
|
||||
}
|
||||
},
|
||||
"types": "./source/index.d.ts",
|
||||
"sideEffects": false,
|
||||
"engines": {
|
||||
"node": "^12.17.0 || ^14.13 || >=16.0.0"
|
||||
},
|
||||
"scripts": {
|
||||
"test": "xo && c8 ava && tsd",
|
||||
"bench": "matcha benchmark.js"
|
||||
},
|
||||
"files": [
|
||||
"source",
|
||||
"!source/index.test-d.ts"
|
||||
],
|
||||
"keywords": [
|
||||
"color",
|
||||
"colour",
|
||||
"colors",
|
||||
"terminal",
|
||||
"console",
|
||||
"cli",
|
||||
"string",
|
||||
"ansi",
|
||||
"style",
|
||||
"styles",
|
||||
"tty",
|
||||
"formatting",
|
||||
"rgb",
|
||||
"256",
|
||||
"shell",
|
||||
"xterm",
|
||||
"log",
|
||||
"logging",
|
||||
"command-line",
|
||||
"text"
|
||||
],
|
||||
"devDependencies": {
|
||||
"@types/node": "^16.11.10",
|
||||
"ava": "^3.15.0",
|
||||
"c8": "^7.10.0",
|
||||
"color-convert": "^2.0.1",
|
||||
"execa": "^6.0.0",
|
||||
"log-update": "^5.0.0",
|
||||
"matcha": "^0.7.0",
|
||||
"tsd": "^0.19.0",
|
||||
"xo": "^0.57.0",
|
||||
"yoctodelay": "^2.0.0"
|
||||
},
|
||||
"xo": {
|
||||
"rules": {
|
||||
"unicorn/prefer-string-slice": "off",
|
||||
"@typescript-eslint/consistent-type-imports": "off",
|
||||
"@typescript-eslint/consistent-type-exports": "off",
|
||||
"@typescript-eslint/consistent-type-definitions": "off",
|
||||
"unicorn/expiring-todo-comments": "off"
|
||||
}
|
||||
},
|
||||
"c8": {
|
||||
"reporter": [
|
||||
"text",
|
||||
"lcov"
|
||||
],
|
||||
"exclude": [
|
||||
"source/vendor"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
|
|
|||
269
readme.md
269
readme.md
|
|
@ -1,7 +1,7 @@
|
|||
<h1 align="center">
|
||||
<br>
|
||||
<br>
|
||||
<img width="360" src="https://cdn.rawgit.com/chalk/chalk/19935d6484811c5e468817f846b7b3d417d7bf4a/logo.svg" alt="chalk">
|
||||
<img width="320" src="media/logo.svg" alt="Chalk">
|
||||
<br>
|
||||
<br>
|
||||
<br>
|
||||
|
|
@ -9,83 +9,105 @@
|
|||
|
||||
> Terminal string styling done right
|
||||
|
||||
[](https://travis-ci.org/chalk/chalk)
|
||||
[](https://coveralls.io/r/chalk/chalk?branch=master)
|
||||
[](https://www.youtube.com/watch?v=9auOCbH5Ns4)
|
||||
[](https://codecov.io/gh/chalk/chalk)
|
||||
[](https://www.npmjs.com/package/chalk?activeTab=dependents)
|
||||
[](https://www.npmjs.com/package/chalk)
|
||||
|
||||

|
||||
|
||||
[colors.js](https://github.com/Marak/colors.js) used to be the most popular string styling module, but it has serious deficiencies like extending `String.prototype` which causes all kinds of [problems](https://github.com/yeoman/yo/issues/68). Although there are other ones, they either do too much or not enough.
|
||||
## Info
|
||||
|
||||
**Chalk is a clean and focused alternative.**
|
||||
- [Why not switch to a smaller coloring package?](https://github.com/chalk/chalk?tab=readme-ov-file#why-not-switch-to-a-smaller-coloring-package)
|
||||
- See [yoctocolors](https://github.com/sindresorhus/yoctocolors) for a smaller alternative
|
||||
|
||||

|
||||
## Highlights
|
||||
|
||||
|
||||
## Why
|
||||
|
||||
- Highly performant
|
||||
- Doesn't extend `String.prototype`
|
||||
- Expressive API
|
||||
- Highly performant
|
||||
- No dependencies
|
||||
- Ability to nest styles
|
||||
- Clean and focused
|
||||
- [256/Truecolor color support](#256-and-truecolor-color-support)
|
||||
- Auto-detects color support
|
||||
- Doesn't extend `String.prototype`
|
||||
- Clean and focused
|
||||
- Actively maintained
|
||||
- [Used by ~4000 modules](https://www.npmjs.com/browse/depended/chalk) as of May 24, 2015
|
||||
|
||||
- [Used by ~115,000 packages](https://www.npmjs.com/browse/depended/chalk) as of July 4, 2024
|
||||
|
||||
## Install
|
||||
|
||||
```
|
||||
$ npm install --save chalk
|
||||
```sh
|
||||
npm install chalk
|
||||
```
|
||||
|
||||
**IMPORTANT:** Chalk 5 is ESM. If you want to use Chalk with TypeScript or a build tool, you will probably want to use Chalk 4 for now. [Read more.](https://github.com/chalk/chalk/releases/tag/v5.0.0)
|
||||
|
||||
## Usage
|
||||
|
||||
```js
|
||||
import chalk from 'chalk';
|
||||
|
||||
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.
|
||||
|
||||
```js
|
||||
var chalk = require('chalk');
|
||||
import chalk from 'chalk';
|
||||
|
||||
// style a string
|
||||
chalk.blue('Hello world!');
|
||||
const log = console.log;
|
||||
|
||||
// combine styled and normal strings
|
||||
chalk.blue('Hello') + 'World' + chalk.red('!');
|
||||
// Combine styled and normal strings
|
||||
log(chalk.blue('Hello') + ' World' + chalk.red('!'));
|
||||
|
||||
// compose multiple styles using the chainable API
|
||||
chalk.blue.bgRed.bold('Hello world!');
|
||||
// Compose multiple styles using the chainable API
|
||||
log(chalk.blue.bgRed.bold('Hello world!'));
|
||||
|
||||
// pass in multiple arguments
|
||||
chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz');
|
||||
// Pass in multiple arguments
|
||||
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
|
||||
|
||||
// nest styles
|
||||
chalk.red('Hello', chalk.underline.bgBlue('world') + '!');
|
||||
// Nest styles
|
||||
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
|
||||
|
||||
// nest styles of the same type even (color, underline, background)
|
||||
chalk.green(
|
||||
// Nest styles of the same type even (color, underline, background)
|
||||
log(chalk.green(
|
||||
'I am a green line ' +
|
||||
chalk.blue.underline.bold('with a blue substring') +
|
||||
' that becomes green again!'
|
||||
);
|
||||
));
|
||||
|
||||
// ES2015 template literal
|
||||
log(`
|
||||
CPU: ${chalk.red('90%')}
|
||||
RAM: ${chalk.green('40%')}
|
||||
DISK: ${chalk.yellow('70%')}
|
||||
`);
|
||||
|
||||
// Use RGB colors in terminal emulators that support it.
|
||||
log(chalk.rgb(123, 45, 67).underline('Underlined reddish color'));
|
||||
log(chalk.hex('#DEADED').bold('Bold gray!'));
|
||||
```
|
||||
|
||||
Easily define your own themes.
|
||||
Easily define your own themes:
|
||||
|
||||
```js
|
||||
var chalk = require('chalk');
|
||||
var error = chalk.bold.red;
|
||||
import chalk from 'chalk';
|
||||
|
||||
const error = chalk.bold.red;
|
||||
const warning = chalk.hex('#FFA500'); // Orange color
|
||||
|
||||
console.log(error('Error!'));
|
||||
console.log(warning('Warning!'));
|
||||
```
|
||||
|
||||
Take advantage of console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data).
|
||||
Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args):
|
||||
|
||||
```js
|
||||
var name = 'Sindre';
|
||||
console.log(chalk.green('Hello %s'), name);
|
||||
//=> Hello Sindre
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
const name = 'Sindre';
|
||||
console.log(chalk.green('Hello %s'), name);
|
||||
//=> 'Hello Sindre'
|
||||
```
|
||||
|
||||
## API
|
||||
|
||||
|
|
@ -93,75 +115,73 @@ console.log(chalk.green('Hello %s'), name);
|
|||
|
||||
Example: `chalk.red.bold.underline('Hello', 'world');`
|
||||
|
||||
Chain [styles](#styles) and 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. This simply means that `Chalk.red.yellow.green` is equivalent to `Chalk.green`.
|
||||
Chain [styles](#styles) and 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. This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
|
||||
|
||||
Multiple arguments will be separated by space.
|
||||
|
||||
### chalk.enabled
|
||||
### chalk.level
|
||||
|
||||
Color support is automatically detected, but you can override it by setting the `enabled` property. You should however only do this in your own code as it applies globally to all chalk consumers.
|
||||
Specifies the level of color support.
|
||||
|
||||
If you need to change this in a reusable module create a new instance:
|
||||
Color support is automatically detected, but you can override it by setting the `level` property. You should however only do this in your own code as it applies globally to all Chalk consumers.
|
||||
|
||||
If you need to change this in a reusable module, create a new instance:
|
||||
|
||||
```js
|
||||
var ctx = new chalk.constructor({enabled: false});
|
||||
import {Chalk} from 'chalk';
|
||||
|
||||
const customChalk = new Chalk({level: 0});
|
||||
```
|
||||
|
||||
### chalk.supportsColor
|
||||
| Level | Description |
|
||||
| :---: | :--- |
|
||||
| `0` | All colors disabled |
|
||||
| `1` | Basic color support (16 colors) |
|
||||
| `2` | 256 color support |
|
||||
| `3` | Truecolor support (16 million colors) |
|
||||
|
||||
### supportsColor
|
||||
|
||||
Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.
|
||||
|
||||
Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, add an environment variable `FORCE_COLOR` with any value to force color. Trumps `--no-color`.
|
||||
Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, use the environment variable `FORCE_COLOR=1` (level 1), `FORCE_COLOR=2` (level 2), or `FORCE_COLOR=3` (level 3) to forcefully enable color, or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.
|
||||
|
||||
### chalk.styles
|
||||
Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.
|
||||
|
||||
Exposes the styles as [ANSI escape codes](https://github.com/chalk/ansi-styles).
|
||||
### chalkStderr and supportsColorStderr
|
||||
|
||||
Generally not useful, but you might need just the `.open` or `.close` escape code if you're mixing externally styled strings with your own.
|
||||
`chalkStderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `supportsColor` apply to this too. `supportsColorStderr` is exposed for convenience.
|
||||
|
||||
### modifierNames, foregroundColorNames, backgroundColorNames, and colorNames
|
||||
|
||||
All supported style strings are exposed as an array of strings for convenience. `colorNames` is the combination of `foregroundColorNames` and `backgroundColorNames`.
|
||||
|
||||
This can be useful if you wrap Chalk and need to validate input:
|
||||
|
||||
```js
|
||||
var chalk = require('chalk');
|
||||
import {modifierNames, foregroundColorNames} from 'chalk';
|
||||
|
||||
console.log(chalk.styles.red);
|
||||
//=> {open: '\u001b[31m', close: '\u001b[39m'}
|
||||
console.log(modifierNames.includes('bold'));
|
||||
//=> true
|
||||
|
||||
console.log(chalk.styles.red.open + 'Hello' + chalk.styles.red.close);
|
||||
console.log(foregroundColorNames.includes('pink'));
|
||||
//=> false
|
||||
```
|
||||
|
||||
### chalk.hasColor(string)
|
||||
|
||||
Check whether a string [has color](https://github.com/chalk/has-ansi).
|
||||
|
||||
### chalk.stripColor(string)
|
||||
|
||||
[Strip color](https://github.com/chalk/strip-ansi) from a string.
|
||||
|
||||
Can be useful in combination with `.supportsColor` to strip color on externally styled text when it's not supported.
|
||||
|
||||
Example:
|
||||
|
||||
```js
|
||||
var chalk = require('chalk');
|
||||
var styledString = getText();
|
||||
|
||||
if (!chalk.supportsColor) {
|
||||
styledString = chalk.stripColor(styledString);
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
## Styles
|
||||
|
||||
### Modifiers
|
||||
|
||||
- `reset`
|
||||
- `bold`
|
||||
- `dim`
|
||||
- `italic` *(not widely supported)*
|
||||
- `underline`
|
||||
- `inverse`
|
||||
- `hidden`
|
||||
- `strikethrough` *(not widely supported)*
|
||||
- `reset` - Reset the current style.
|
||||
- `bold` - Make the text bold.
|
||||
- `dim` - Make the text have lower opacity.
|
||||
- `italic` - Make the text italic. *(Not widely supported)*
|
||||
- `underline` - Put a horizontal line below the text. *(Not widely supported)*
|
||||
- `overline` - Put a horizontal line above the text. *(Not widely supported)*
|
||||
- `inverse` - Invert background and foreground colors.
|
||||
- `hidden` - Print the text but make it invisible.
|
||||
- `strikethrough` - Puts a horizontal line through the center of the text. *(Not widely supported)*
|
||||
- `visible` - Print the text only when Chalk has a color level above zero. Can be useful for things that are purely cosmetic.
|
||||
|
||||
### Colors
|
||||
|
||||
|
|
@ -169,11 +189,18 @@ if (!chalk.supportsColor) {
|
|||
- `red`
|
||||
- `green`
|
||||
- `yellow`
|
||||
- `blue` *(on Windows the bright version is used as normal blue is illegible)*
|
||||
- `blue`
|
||||
- `magenta`
|
||||
- `cyan`
|
||||
- `white`
|
||||
- `gray`
|
||||
- `blackBright` (alias: `gray`, `grey`)
|
||||
- `redBright`
|
||||
- `greenBright`
|
||||
- `yellowBright`
|
||||
- `blueBright`
|
||||
- `magentaBright`
|
||||
- `cyanBright`
|
||||
- `whiteBright`
|
||||
|
||||
### Background colors
|
||||
|
||||
|
|
@ -185,28 +212,86 @@ if (!chalk.supportsColor) {
|
|||
- `bgMagenta`
|
||||
- `bgCyan`
|
||||
- `bgWhite`
|
||||
- `bgBlackBright` (alias: `bgGray`, `bgGrey`)
|
||||
- `bgRedBright`
|
||||
- `bgGreenBright`
|
||||
- `bgYellowBright`
|
||||
- `bgBlueBright`
|
||||
- `bgMagentaBright`
|
||||
- `bgCyanBright`
|
||||
- `bgWhiteBright`
|
||||
|
||||
## 256 and Truecolor color support
|
||||
|
||||
## 256-colors
|
||||
Chalk supports 256 colors and [Truecolor](https://github.com/termstandard/colors) (16 million colors) on supported terminal apps.
|
||||
|
||||
Chalk does not support support anything other than the base eight colors, which guarantees it will work on all terminals and systems. Some terminals, specifically `xterm` compliant ones, will support the full range of 8-bit colors. For this the lower level [ansi-256-colors](https://github.com/jbnicolai/ansi-256-colors) package can be used.
|
||||
Colors are downsampled from 16 million RGB values to an ANSI color format that is supported by the terminal emulator (or by specifying `{level: n}` as a Chalk option). For example, Chalk configured to run at level 1 (basic color support) will downsample an RGB value of #FF0000 (red) to 31 (ANSI escape for red).
|
||||
|
||||
Examples:
|
||||
|
||||
- `chalk.hex('#DEADED').underline('Hello, world!')`
|
||||
- `chalk.rgb(15, 100, 204).inverse('Hello!')`
|
||||
|
||||
Background versions of these models are prefixed with `bg` and the first level of the module capitalized (e.g. `hex` for foreground colors and `bgHex` for background colors).
|
||||
|
||||
- `chalk.bgHex('#DEADED').underline('Hello, world!')`
|
||||
- `chalk.bgRgb(15, 100, 204).inverse('Hello!')`
|
||||
|
||||
The following color models can be used:
|
||||
|
||||
- [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
|
||||
- [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
|
||||
- [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
|
||||
|
||||
## Browser support
|
||||
|
||||
Since Chrome 69, ANSI escape codes are natively supported in the developer console.
|
||||
|
||||
## Windows
|
||||
|
||||
If you're on Windows, do yourself a favor and use [`cmder`](http://bliker.github.io/cmder/) instead of `cmd.exe`.
|
||||
If you're on Windows, do yourself a favor and use [Windows Terminal](https://github.com/microsoft/terminal) instead of `cmd.exe`.
|
||||
|
||||
## FAQ
|
||||
|
||||
### Why not switch to a smaller coloring package?
|
||||
|
||||
Chalk may be larger, but there is a reason for that. It offers a more user-friendly API, well-documented types, supports millions of colors, and covers edge cases that smaller alternatives miss. Chalk is mature, reliable, and built to last.
|
||||
|
||||
But beyond the technical aspects, there's something more critical: trust and long-term maintenance. I have been active in open source for over a decade, and I'm committed to keeping Chalk maintained. Smaller packages might seem appealing now, but there's no guarantee they will be around for the long term, or that they won't become malicious over time.
|
||||
|
||||
Chalk is also likely already in your dependency tree (since 100K+ packages depend on it), so switching won’t save space—in fact, it might increase it. npm deduplicates dependencies, so multiple Chalk instances turn into one, but adding another package alongside it will increase your overall size.
|
||||
|
||||
If the goal is to clean up the ecosystem, switching away from Chalk won’t even make a dent. The real problem lies with packages that have very deep dependency trees (for example, those including a lot of polyfills). Chalk has no dependencies. It's better to focus on impactful changes rather than minor optimizations.
|
||||
|
||||
If absolute package size is important to you, I also maintain [yoctocolors](https://github.com/sindresorhus/yoctocolors), one of the smallest color packages out there.
|
||||
|
||||
*\- [Sindre](https://github.com/sindresorhus)*
|
||||
|
||||
### But the smaller coloring package has benchmarks showing it is faster
|
||||
|
||||
[Micro-benchmarks are flawed](https://sindresorhus.com/blog/micro-benchmark-fallacy) because they measure performance in unrealistic, isolated scenarios, often giving a distorted view of real-world performance. Don't believe marketing fluff. All the coloring packages are more than fast enough.
|
||||
|
||||
## Related
|
||||
|
||||
- [chalk-template](https://github.com/chalk/chalk-template) - [Tagged template literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#tagged_templates) support for this module
|
||||
- [chalk-cli](https://github.com/chalk/chalk-cli) - CLI for this module
|
||||
- [ansi-styles](https://github.com/chalk/ansi-styles/) - ANSI escape codes for styling strings in the terminal
|
||||
- [supports-color](https://github.com/chalk/supports-color/) - Detect whether a terminal supports color
|
||||
- [ansi-styles](https://github.com/chalk/ansi-styles) - ANSI escape codes for styling strings in the terminal
|
||||
- [supports-color](https://github.com/chalk/supports-color) - Detect whether a terminal supports color
|
||||
- [strip-ansi](https://github.com/chalk/strip-ansi) - Strip ANSI escape codes
|
||||
- [strip-ansi-stream](https://github.com/chalk/strip-ansi-stream) - Strip ANSI escape codes from a stream
|
||||
- [has-ansi](https://github.com/chalk/has-ansi) - Check if a string has ANSI escape codes
|
||||
- [ansi-regex](https://github.com/chalk/ansi-regex) - Regular expression for matching ANSI escape codes
|
||||
- [wrap-ansi](https://github.com/chalk/wrap-ansi) - Wordwrap a string with ANSI escape codes
|
||||
- [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes
|
||||
- [color-convert](https://github.com/qix-/color-convert) - Converts colors between different models
|
||||
- [chalk-animation](https://github.com/bokub/chalk-animation) - Animate strings in the terminal
|
||||
- [gradient-string](https://github.com/bokub/gradient-string) - Apply color gradients to strings
|
||||
- [chalk-pipe](https://github.com/LitoMore/chalk-pipe) - Create chalk style schemes with simpler style strings
|
||||
- [terminal-link](https://github.com/sindresorhus/terminal-link) - Create clickable links in the terminal
|
||||
|
||||
*(Not accepting additional entries)*
|
||||
|
||||
## License
|
||||
## Maintainers
|
||||
|
||||
MIT © [Sindre Sorhus](http://sindresorhus.com)
|
||||
- [Sindre Sorhus](https://github.com/sindresorhus)
|
||||
- [Josh Junon](https://github.com/qix-)
|
||||
|
|
|
|||
325
source/index.d.ts
vendored
Normal file
325
source/index.d.ts
vendored
Normal file
|
|
@ -0,0 +1,325 @@
|
|||
// TODO: Make it this when TS suports that.
|
||||
// import {ModifierName, ForegroundColor, BackgroundColor, ColorName} from '#ansi-styles';
|
||||
// import {ColorInfo, ColorSupportLevel} from '#supports-color';
|
||||
import {
|
||||
ModifierName,
|
||||
ForegroundColorName,
|
||||
BackgroundColorName,
|
||||
ColorName,
|
||||
} from './vendor/ansi-styles/index.js';
|
||||
import {ColorInfo, ColorSupportLevel} from './vendor/supports-color/index.js';
|
||||
|
||||
export interface Options {
|
||||
/**
|
||||
Specify the color support for Chalk.
|
||||
|
||||
By default, color support is automatically detected based on the environment.
|
||||
|
||||
Levels:
|
||||
- `0` - All colors disabled.
|
||||
- `1` - Basic 16 colors support.
|
||||
- `2` - ANSI 256 colors support.
|
||||
- `3` - Truecolor 16 million colors support.
|
||||
*/
|
||||
readonly level?: ColorSupportLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
Return a new Chalk instance.
|
||||
*/
|
||||
export const Chalk: new (options?: Options) => ChalkInstance; // eslint-disable-line @typescript-eslint/naming-convention
|
||||
|
||||
export interface ChalkInstance {
|
||||
(...text: unknown[]): string;
|
||||
|
||||
/**
|
||||
The color support for Chalk.
|
||||
|
||||
By default, color support is automatically detected based on the environment.
|
||||
|
||||
Levels:
|
||||
- `0` - All colors disabled.
|
||||
- `1` - Basic 16 colors support.
|
||||
- `2` - ANSI 256 colors support.
|
||||
- `3` - Truecolor 16 million colors support.
|
||||
*/
|
||||
level: ColorSupportLevel;
|
||||
|
||||
/**
|
||||
Use RGB values to set text color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.rgb(222, 173, 237);
|
||||
```
|
||||
*/
|
||||
rgb: (red: number, green: number, blue: number) => this;
|
||||
|
||||
/**
|
||||
Use HEX value to set text color.
|
||||
|
||||
@param color - Hexadecimal value representing the desired color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.hex('#DEADED');
|
||||
```
|
||||
*/
|
||||
hex: (color: string) => this;
|
||||
|
||||
/**
|
||||
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.ansi256(201);
|
||||
```
|
||||
*/
|
||||
ansi256: (index: number) => this;
|
||||
|
||||
/**
|
||||
Use RGB values to set background color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.bgRgb(222, 173, 237);
|
||||
```
|
||||
*/
|
||||
bgRgb: (red: number, green: number, blue: number) => this;
|
||||
|
||||
/**
|
||||
Use HEX value to set background color.
|
||||
|
||||
@param color - Hexadecimal value representing the desired color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.bgHex('#DEADED');
|
||||
```
|
||||
*/
|
||||
bgHex: (color: string) => this;
|
||||
|
||||
/**
|
||||
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.bgAnsi256(201);
|
||||
```
|
||||
*/
|
||||
bgAnsi256: (index: number) => this;
|
||||
|
||||
/**
|
||||
Modifier: Reset the current style.
|
||||
*/
|
||||
readonly reset: this;
|
||||
|
||||
/**
|
||||
Modifier: Make the text bold.
|
||||
*/
|
||||
readonly bold: this;
|
||||
|
||||
/**
|
||||
Modifier: Make the text have lower opacity.
|
||||
*/
|
||||
readonly dim: this;
|
||||
|
||||
/**
|
||||
Modifier: Make the text italic. *(Not widely supported)*
|
||||
*/
|
||||
readonly italic: this;
|
||||
|
||||
/**
|
||||
Modifier: Put a horizontal line below the text. *(Not widely supported)*
|
||||
*/
|
||||
readonly underline: this;
|
||||
|
||||
/**
|
||||
Modifier: Put a horizontal line above the text. *(Not widely supported)*
|
||||
*/
|
||||
readonly overline: this;
|
||||
|
||||
/**
|
||||
Modifier: Invert background and foreground colors.
|
||||
*/
|
||||
readonly inverse: this;
|
||||
|
||||
/**
|
||||
Modifier: Print the text but make it invisible.
|
||||
*/
|
||||
readonly hidden: this;
|
||||
|
||||
/**
|
||||
Modifier: Puts a horizontal line through the center of the text. *(Not widely supported)*
|
||||
*/
|
||||
readonly strikethrough: this;
|
||||
|
||||
/**
|
||||
Modifier: Print the text only when Chalk has a color level above zero.
|
||||
|
||||
Can be useful for things that are purely cosmetic.
|
||||
*/
|
||||
readonly visible: this;
|
||||
|
||||
readonly black: this;
|
||||
readonly red: this;
|
||||
readonly green: this;
|
||||
readonly yellow: this;
|
||||
readonly blue: this;
|
||||
readonly magenta: this;
|
||||
readonly cyan: this;
|
||||
readonly white: this;
|
||||
|
||||
/*
|
||||
Alias for `blackBright`.
|
||||
*/
|
||||
readonly gray: this;
|
||||
|
||||
/*
|
||||
Alias for `blackBright`.
|
||||
*/
|
||||
readonly grey: this;
|
||||
|
||||
readonly blackBright: this;
|
||||
readonly redBright: this;
|
||||
readonly greenBright: this;
|
||||
readonly yellowBright: this;
|
||||
readonly blueBright: this;
|
||||
readonly magentaBright: this;
|
||||
readonly cyanBright: this;
|
||||
readonly whiteBright: this;
|
||||
|
||||
readonly bgBlack: this;
|
||||
readonly bgRed: this;
|
||||
readonly bgGreen: this;
|
||||
readonly bgYellow: this;
|
||||
readonly bgBlue: this;
|
||||
readonly bgMagenta: this;
|
||||
readonly bgCyan: this;
|
||||
readonly bgWhite: this;
|
||||
|
||||
/*
|
||||
Alias for `bgBlackBright`.
|
||||
*/
|
||||
readonly bgGray: this;
|
||||
|
||||
/*
|
||||
Alias for `bgBlackBright`.
|
||||
*/
|
||||
readonly bgGrey: this;
|
||||
|
||||
readonly bgBlackBright: this;
|
||||
readonly bgRedBright: this;
|
||||
readonly bgGreenBright: this;
|
||||
readonly bgYellowBright: this;
|
||||
readonly bgBlueBright: this;
|
||||
readonly bgMagentaBright: this;
|
||||
readonly bgCyanBright: this;
|
||||
readonly bgWhiteBright: this;
|
||||
}
|
||||
|
||||
/**
|
||||
Main Chalk object that allows to chain styles together.
|
||||
|
||||
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.
|
||||
|
||||
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
|
||||
*/
|
||||
declare const chalk: ChalkInstance;
|
||||
|
||||
export const supportsColor: ColorInfo;
|
||||
|
||||
export const chalkStderr: typeof chalk;
|
||||
export const supportsColorStderr: typeof supportsColor;
|
||||
|
||||
export {
|
||||
ModifierName, ForegroundColorName, BackgroundColorName, ColorName,
|
||||
modifierNames, foregroundColorNames, backgroundColorNames, colorNames,
|
||||
// } from '#ansi-styles';
|
||||
} from './vendor/ansi-styles/index.js';
|
||||
|
||||
export {
|
||||
ColorInfo,
|
||||
ColorSupport,
|
||||
ColorSupportLevel,
|
||||
// } from '#supports-color';
|
||||
} from './vendor/supports-color/index.js';
|
||||
|
||||
// TODO: Remove these aliases in the next major version
|
||||
/**
|
||||
@deprecated Use `ModifierName` instead.
|
||||
|
||||
Basic modifier names.
|
||||
*/
|
||||
export type Modifiers = ModifierName;
|
||||
|
||||
/**
|
||||
@deprecated Use `ForegroundColorName` instead.
|
||||
|
||||
Basic foreground color names.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type ForegroundColor = ForegroundColorName;
|
||||
|
||||
/**
|
||||
@deprecated Use `BackgroundColorName` instead.
|
||||
|
||||
Basic background color names.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type BackgroundColor = BackgroundColorName;
|
||||
|
||||
/**
|
||||
@deprecated Use `ColorName` instead.
|
||||
|
||||
Basic color names. The combination of foreground and background color names.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type Color = ColorName;
|
||||
|
||||
/**
|
||||
@deprecated Use `modifierNames` instead.
|
||||
|
||||
Basic modifier names.
|
||||
*/
|
||||
export const modifiers: readonly Modifiers[];
|
||||
|
||||
/**
|
||||
@deprecated Use `foregroundColorNames` instead.
|
||||
|
||||
Basic foreground color names.
|
||||
*/
|
||||
export const foregroundColors: readonly ForegroundColor[];
|
||||
|
||||
/**
|
||||
@deprecated Use `backgroundColorNames` instead.
|
||||
|
||||
Basic background color names.
|
||||
*/
|
||||
export const backgroundColors: readonly BackgroundColor[];
|
||||
|
||||
/**
|
||||
@deprecated Use `colorNames` instead.
|
||||
|
||||
Basic color names. The combination of foreground and background color names.
|
||||
*/
|
||||
export const colors: readonly Color[];
|
||||
|
||||
export default chalk;
|
||||
225
source/index.js
Normal file
225
source/index.js
Normal file
|
|
@ -0,0 +1,225 @@
|
|||
import ansiStyles from '#ansi-styles';
|
||||
import supportsColor from '#supports-color';
|
||||
import { // eslint-disable-line import/order
|
||||
stringReplaceAll,
|
||||
stringEncaseCRLFWithFirstIndex,
|
||||
} from './utilities.js';
|
||||
|
||||
const {stdout: stdoutColor, stderr: stderrColor} = supportsColor;
|
||||
|
||||
const GENERATOR = Symbol('GENERATOR');
|
||||
const STYLER = Symbol('STYLER');
|
||||
const IS_EMPTY = Symbol('IS_EMPTY');
|
||||
|
||||
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
||||
const levelMapping = [
|
||||
'ansi',
|
||||
'ansi',
|
||||
'ansi256',
|
||||
'ansi16m',
|
||||
];
|
||||
|
||||
const styles = Object.create(null);
|
||||
|
||||
const applyOptions = (object, options = {}) => {
|
||||
if (options.level && !(Number.isInteger(options.level) && options.level >= 0 && options.level <= 3)) {
|
||||
throw new Error('The `level` option should be an integer from 0 to 3');
|
||||
}
|
||||
|
||||
// Detect level if not set manually
|
||||
const colorLevel = stdoutColor ? stdoutColor.level : 0;
|
||||
object.level = options.level === undefined ? colorLevel : options.level;
|
||||
};
|
||||
|
||||
export class Chalk {
|
||||
constructor(options) {
|
||||
// eslint-disable-next-line no-constructor-return
|
||||
return chalkFactory(options);
|
||||
}
|
||||
}
|
||||
|
||||
const chalkFactory = options => {
|
||||
const chalk = (...strings) => strings.join(' ');
|
||||
applyOptions(chalk, options);
|
||||
|
||||
Object.setPrototypeOf(chalk, createChalk.prototype);
|
||||
|
||||
return chalk;
|
||||
};
|
||||
|
||||
function createChalk(options) {
|
||||
return chalkFactory(options);
|
||||
}
|
||||
|
||||
Object.setPrototypeOf(createChalk.prototype, Function.prototype);
|
||||
|
||||
for (const [styleName, style] of Object.entries(ansiStyles)) {
|
||||
styles[styleName] = {
|
||||
get() {
|
||||
const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
|
||||
Object.defineProperty(this, styleName, {value: builder});
|
||||
return builder;
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
styles.visible = {
|
||||
get() {
|
||||
const builder = createBuilder(this, this[STYLER], true);
|
||||
Object.defineProperty(this, 'visible', {value: builder});
|
||||
return builder;
|
||||
},
|
||||
};
|
||||
|
||||
const getModelAnsi = (model, level, type, ...arguments_) => {
|
||||
if (model === 'rgb') {
|
||||
if (level === 'ansi16m') {
|
||||
return ansiStyles[type].ansi16m(...arguments_);
|
||||
}
|
||||
|
||||
if (level === 'ansi256') {
|
||||
return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
|
||||
}
|
||||
|
||||
return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_));
|
||||
}
|
||||
|
||||
if (model === 'hex') {
|
||||
return getModelAnsi('rgb', level, type, ...ansiStyles.hexToRgb(...arguments_));
|
||||
}
|
||||
|
||||
return ansiStyles[type][model](...arguments_);
|
||||
};
|
||||
|
||||
const usedModels = ['rgb', 'hex', 'ansi256'];
|
||||
|
||||
for (const model of usedModels) {
|
||||
styles[model] = {
|
||||
get() {
|
||||
const {level} = this;
|
||||
return function (...arguments_) {
|
||||
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
|
||||
return createBuilder(this, styler, this[IS_EMPTY]);
|
||||
};
|
||||
},
|
||||
};
|
||||
|
||||
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
||||
styles[bgModel] = {
|
||||
get() {
|
||||
const {level} = this;
|
||||
return function (...arguments_) {
|
||||
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
|
||||
return createBuilder(this, styler, this[IS_EMPTY]);
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const proto = Object.defineProperties(() => {}, {
|
||||
...styles,
|
||||
level: {
|
||||
enumerable: true,
|
||||
get() {
|
||||
return this[GENERATOR].level;
|
||||
},
|
||||
set(level) {
|
||||
this[GENERATOR].level = level;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const createStyler = (open, close, parent) => {
|
||||
let openAll;
|
||||
let closeAll;
|
||||
if (parent === undefined) {
|
||||
openAll = open;
|
||||
closeAll = close;
|
||||
} else {
|
||||
openAll = parent.openAll + open;
|
||||
closeAll = close + parent.closeAll;
|
||||
}
|
||||
|
||||
return {
|
||||
open,
|
||||
close,
|
||||
openAll,
|
||||
closeAll,
|
||||
parent,
|
||||
};
|
||||
};
|
||||
|
||||
const createBuilder = (self, _styler, _isEmpty) => {
|
||||
// Single argument is hot path, implicit coercion is faster than anything
|
||||
// eslint-disable-next-line no-implicit-coercion
|
||||
const builder = (...arguments_) => applyStyle(builder, (arguments_.length === 1) ? ('' + arguments_[0]) : arguments_.join(' '));
|
||||
|
||||
// We alter the prototype because we must return a function, but there is
|
||||
// no way to create a function with a different prototype
|
||||
Object.setPrototypeOf(builder, proto);
|
||||
|
||||
builder[GENERATOR] = self;
|
||||
builder[STYLER] = _styler;
|
||||
builder[IS_EMPTY] = _isEmpty;
|
||||
|
||||
return builder;
|
||||
};
|
||||
|
||||
const applyStyle = (self, string) => {
|
||||
if (self.level <= 0 || !string) {
|
||||
return self[IS_EMPTY] ? '' : string;
|
||||
}
|
||||
|
||||
let styler = self[STYLER];
|
||||
|
||||
if (styler === undefined) {
|
||||
return string;
|
||||
}
|
||||
|
||||
const {openAll, closeAll} = styler;
|
||||
if (string.includes('\u001B')) {
|
||||
while (styler !== undefined) {
|
||||
// Replace any instances already present with a re-opening code
|
||||
// otherwise only the part of the string until said closing code
|
||||
// will be colored, and the rest will simply be 'plain'.
|
||||
string = stringReplaceAll(string, styler.close, styler.open);
|
||||
|
||||
styler = styler.parent;
|
||||
}
|
||||
}
|
||||
|
||||
// We can move both next actions out of loop, because remaining actions in loop won't have
|
||||
// any/visible effect on parts we add here. Close the styling before a linebreak and reopen
|
||||
// after next line to fix a bleed issue on macOS: https://github.com/chalk/chalk/pull/92
|
||||
const lfIndex = string.indexOf('\n');
|
||||
if (lfIndex !== -1) {
|
||||
string = stringEncaseCRLFWithFirstIndex(string, closeAll, openAll, lfIndex);
|
||||
}
|
||||
|
||||
return openAll + string + closeAll;
|
||||
};
|
||||
|
||||
Object.defineProperties(createChalk.prototype, styles);
|
||||
|
||||
const chalk = createChalk();
|
||||
export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0});
|
||||
|
||||
export {
|
||||
modifierNames,
|
||||
foregroundColorNames,
|
||||
backgroundColorNames,
|
||||
colorNames,
|
||||
|
||||
// TODO: Remove these aliases in the next major version
|
||||
modifierNames as modifiers,
|
||||
foregroundColorNames as foregroundColors,
|
||||
backgroundColorNames as backgroundColors,
|
||||
colorNames as colors,
|
||||
} from './vendor/ansi-styles/index.js';
|
||||
|
||||
export {
|
||||
stdoutColor as supportsColor,
|
||||
stderrColor as supportsColorStderr,
|
||||
};
|
||||
|
||||
export default chalk;
|
||||
179
source/index.test-d.ts
Normal file
179
source/index.test-d.ts
Normal file
|
|
@ -0,0 +1,179 @@
|
|||
import {
|
||||
expectType,
|
||||
expectAssignable,
|
||||
expectError,
|
||||
expectDeprecated,
|
||||
} from 'tsd';
|
||||
import chalk, {
|
||||
Chalk,
|
||||
ChalkInstance,
|
||||
ColorInfo,
|
||||
ColorSupport,
|
||||
ColorSupportLevel,
|
||||
chalkStderr,
|
||||
supportsColor,
|
||||
supportsColorStderr,
|
||||
ModifierName,
|
||||
ForegroundColorName,
|
||||
BackgroundColorName,
|
||||
ColorName,
|
||||
Modifiers,
|
||||
} from './index.js';
|
||||
|
||||
// - supportsColor -
|
||||
expectType<ColorInfo>(supportsColor);
|
||||
if (supportsColor) {
|
||||
expectType<ColorSupport>(supportsColor);
|
||||
expectType<ColorSupportLevel>(supportsColor.level);
|
||||
expectType<boolean>(supportsColor.hasBasic);
|
||||
expectType<boolean>(supportsColor.has256);
|
||||
expectType<boolean>(supportsColor.has16m);
|
||||
}
|
||||
|
||||
// - stderr -
|
||||
expectAssignable<ChalkInstance>(chalkStderr);
|
||||
expectType<ColorInfo>(supportsColorStderr);
|
||||
if (supportsColorStderr) {
|
||||
expectType<boolean>(supportsColorStderr.hasBasic);
|
||||
expectType<boolean>(supportsColorStderr.has256);
|
||||
expectType<boolean>(supportsColorStderr.has16m);
|
||||
}
|
||||
|
||||
// -- `supportsColorStderr` is not a member of the Chalk interface --
|
||||
expectError(chalk.reset.supportsColorStderr);
|
||||
|
||||
// -- `supportsColor` is not a member of the Chalk interface --
|
||||
expectError(chalk.reset.supportsColor);
|
||||
|
||||
// - Chalk -
|
||||
// -- Instance --
|
||||
expectType<ChalkInstance>(new Chalk({level: 1}));
|
||||
|
||||
// -- Properties --
|
||||
expectType<ColorSupportLevel>(chalk.level);
|
||||
|
||||
// -- Color methods --
|
||||
expectType<ChalkInstance>(chalk.rgb(0, 0, 0));
|
||||
expectType<ChalkInstance>(chalk.hex('#DEADED'));
|
||||
expectType<ChalkInstance>(chalk.ansi256(0));
|
||||
expectType<ChalkInstance>(chalk.bgRgb(0, 0, 0));
|
||||
expectType<ChalkInstance>(chalk.bgHex('#DEADED'));
|
||||
expectType<ChalkInstance>(chalk.bgAnsi256(0));
|
||||
|
||||
// -- Modifiers --
|
||||
expectType<string>(chalk.reset('foo'));
|
||||
expectType<string>(chalk.bold('foo'));
|
||||
expectType<string>(chalk.dim('foo'));
|
||||
expectType<string>(chalk.italic('foo'));
|
||||
expectType<string>(chalk.underline('foo'));
|
||||
expectType<string>(chalk.overline('foo'));
|
||||
expectType<string>(chalk.inverse('foo'));
|
||||
expectType<string>(chalk.hidden('foo'));
|
||||
expectType<string>(chalk.strikethrough('foo'));
|
||||
expectType<string>(chalk.visible('foo'));
|
||||
expectType<string>(chalk.reset`foo`);
|
||||
expectType<string>(chalk.bold`foo`);
|
||||
expectType<string>(chalk.dim`foo`);
|
||||
expectType<string>(chalk.italic`foo`);
|
||||
expectType<string>(chalk.underline`foo`);
|
||||
expectType<string>(chalk.inverse`foo`);
|
||||
expectType<string>(chalk.hidden`foo`);
|
||||
expectType<string>(chalk.strikethrough`foo`);
|
||||
expectType<string>(chalk.visible`foo`);
|
||||
|
||||
// -- Colors --
|
||||
expectType<string>(chalk.black('foo'));
|
||||
expectType<string>(chalk.red('foo'));
|
||||
expectType<string>(chalk.green('foo'));
|
||||
expectType<string>(chalk.yellow('foo'));
|
||||
expectType<string>(chalk.blue('foo'));
|
||||
expectType<string>(chalk.magenta('foo'));
|
||||
expectType<string>(chalk.cyan('foo'));
|
||||
expectType<string>(chalk.white('foo'));
|
||||
expectType<string>(chalk.gray('foo'));
|
||||
expectType<string>(chalk.grey('foo'));
|
||||
expectType<string>(chalk.blackBright('foo'));
|
||||
expectType<string>(chalk.redBright('foo'));
|
||||
expectType<string>(chalk.greenBright('foo'));
|
||||
expectType<string>(chalk.yellowBright('foo'));
|
||||
expectType<string>(chalk.blueBright('foo'));
|
||||
expectType<string>(chalk.magentaBright('foo'));
|
||||
expectType<string>(chalk.cyanBright('foo'));
|
||||
expectType<string>(chalk.whiteBright('foo'));
|
||||
expectType<string>(chalk.bgBlack('foo'));
|
||||
expectType<string>(chalk.bgRed('foo'));
|
||||
expectType<string>(chalk.bgGreen('foo'));
|
||||
expectType<string>(chalk.bgYellow('foo'));
|
||||
expectType<string>(chalk.bgBlue('foo'));
|
||||
expectType<string>(chalk.bgMagenta('foo'));
|
||||
expectType<string>(chalk.bgCyan('foo'));
|
||||
expectType<string>(chalk.bgWhite('foo'));
|
||||
expectType<string>(chalk.bgBlackBright('foo'));
|
||||
expectType<string>(chalk.bgRedBright('foo'));
|
||||
expectType<string>(chalk.bgGreenBright('foo'));
|
||||
expectType<string>(chalk.bgYellowBright('foo'));
|
||||
expectType<string>(chalk.bgBlueBright('foo'));
|
||||
expectType<string>(chalk.bgMagentaBright('foo'));
|
||||
expectType<string>(chalk.bgCyanBright('foo'));
|
||||
expectType<string>(chalk.bgWhiteBright('foo'));
|
||||
expectType<string>(chalk.black`foo`);
|
||||
expectType<string>(chalk.red`foo`);
|
||||
expectType<string>(chalk.green`foo`);
|
||||
expectType<string>(chalk.yellow`foo`);
|
||||
expectType<string>(chalk.blue`foo`);
|
||||
expectType<string>(chalk.magenta`foo`);
|
||||
expectType<string>(chalk.cyan`foo`);
|
||||
expectType<string>(chalk.white`foo`);
|
||||
expectType<string>(chalk.gray`foo`);
|
||||
expectType<string>(chalk.grey`foo`);
|
||||
expectType<string>(chalk.blackBright`foo`);
|
||||
expectType<string>(chalk.redBright`foo`);
|
||||
expectType<string>(chalk.greenBright`foo`);
|
||||
expectType<string>(chalk.yellowBright`foo`);
|
||||
expectType<string>(chalk.blueBright`foo`);
|
||||
expectType<string>(chalk.magentaBright`foo`);
|
||||
expectType<string>(chalk.cyanBright`foo`);
|
||||
expectType<string>(chalk.whiteBright`foo`);
|
||||
expectType<string>(chalk.bgBlack`foo`);
|
||||
expectType<string>(chalk.bgRed`foo`);
|
||||
expectType<string>(chalk.bgGreen`foo`);
|
||||
expectType<string>(chalk.bgYellow`foo`);
|
||||
expectType<string>(chalk.bgBlue`foo`);
|
||||
expectType<string>(chalk.bgMagenta`foo`);
|
||||
expectType<string>(chalk.bgCyan`foo`);
|
||||
expectType<string>(chalk.bgWhite`foo`);
|
||||
expectType<string>(chalk.bgBlackBright`foo`);
|
||||
expectType<string>(chalk.bgRedBright`foo`);
|
||||
expectType<string>(chalk.bgGreenBright`foo`);
|
||||
expectType<string>(chalk.bgYellowBright`foo`);
|
||||
expectType<string>(chalk.bgBlueBright`foo`);
|
||||
expectType<string>(chalk.bgMagentaBright`foo`);
|
||||
expectType<string>(chalk.bgCyanBright`foo`);
|
||||
expectType<string>(chalk.bgWhiteBright`foo`);
|
||||
|
||||
// -- Complex --
|
||||
expectType<string>(chalk.red.bgGreen.underline('foo'));
|
||||
expectType<string>(chalk.underline.red.bgGreen('foo'));
|
||||
|
||||
// -- Complex template literal --
|
||||
expectType<string>(chalk.underline``);
|
||||
expectType<string>(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`);
|
||||
expectType<string>(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`);
|
||||
|
||||
// -- Modifiers types
|
||||
expectAssignable<ModifierName>('strikethrough');
|
||||
expectError<ModifierName>('delete');
|
||||
|
||||
// -- Foreground types
|
||||
expectAssignable<ForegroundColorName>('red');
|
||||
expectError<ForegroundColorName>('pink');
|
||||
|
||||
// -- Background types
|
||||
expectAssignable<BackgroundColorName>('bgRed');
|
||||
expectError<BackgroundColorName>('bgPink');
|
||||
|
||||
// -- Color types --
|
||||
expectAssignable<ColorName>('red');
|
||||
expectAssignable<ColorName>('bgRed');
|
||||
expectError<ColorName>('hotpink');
|
||||
expectError<ColorName>('bgHotpink');
|
||||
33
source/utilities.js
Normal file
33
source/utilities.js
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// TODO: When targeting Node.js 16, use `String.prototype.replaceAll`.
|
||||
export function stringReplaceAll(string, substring, replacer) {
|
||||
let index = string.indexOf(substring);
|
||||
if (index === -1) {
|
||||
return string;
|
||||
}
|
||||
|
||||
const substringLength = substring.length;
|
||||
let endIndex = 0;
|
||||
let returnValue = '';
|
||||
do {
|
||||
returnValue += string.slice(endIndex, index) + substring + replacer;
|
||||
endIndex = index + substringLength;
|
||||
index = string.indexOf(substring, endIndex);
|
||||
} while (index !== -1);
|
||||
|
||||
returnValue += string.slice(endIndex);
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
export function stringEncaseCRLFWithFirstIndex(string, prefix, postfix, index) {
|
||||
let endIndex = 0;
|
||||
let returnValue = '';
|
||||
do {
|
||||
const gotCR = string[index - 1] === '\r';
|
||||
returnValue += string.slice(endIndex, (gotCR ? index - 1 : index)) + prefix + (gotCR ? '\r\n' : '\n') + postfix;
|
||||
endIndex = index + 1;
|
||||
index = string.indexOf('\n', endIndex);
|
||||
} while (index !== -1);
|
||||
|
||||
returnValue += string.slice(endIndex);
|
||||
return returnValue;
|
||||
}
|
||||
236
source/vendor/ansi-styles/index.d.ts
vendored
Normal file
236
source/vendor/ansi-styles/index.d.ts
vendored
Normal file
|
|
@ -0,0 +1,236 @@
|
|||
export interface CSPair { // eslint-disable-line @typescript-eslint/naming-convention
|
||||
/**
|
||||
The ANSI terminal control sequence for starting this style.
|
||||
*/
|
||||
readonly open: string;
|
||||
|
||||
/**
|
||||
The ANSI terminal control sequence for ending this style.
|
||||
*/
|
||||
readonly close: string;
|
||||
}
|
||||
|
||||
export interface ColorBase {
|
||||
/**
|
||||
The ANSI terminal control sequence for ending this color.
|
||||
*/
|
||||
readonly close: string;
|
||||
|
||||
ansi(code: number): string;
|
||||
|
||||
ansi256(code: number): string;
|
||||
|
||||
ansi16m(red: number, green: number, blue: number): string;
|
||||
}
|
||||
|
||||
export interface Modifier {
|
||||
/**
|
||||
Resets the current color chain.
|
||||
*/
|
||||
readonly reset: CSPair;
|
||||
|
||||
/**
|
||||
Make text bold.
|
||||
*/
|
||||
readonly bold: CSPair;
|
||||
|
||||
/**
|
||||
Emitting only a small amount of light.
|
||||
*/
|
||||
readonly dim: CSPair;
|
||||
|
||||
/**
|
||||
Make text italic. (Not widely supported)
|
||||
*/
|
||||
readonly italic: CSPair;
|
||||
|
||||
/**
|
||||
Make text underline. (Not widely supported)
|
||||
*/
|
||||
readonly underline: CSPair;
|
||||
|
||||
/**
|
||||
Make text overline.
|
||||
|
||||
Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.
|
||||
*/
|
||||
readonly overline: CSPair;
|
||||
|
||||
/**
|
||||
Inverse background and foreground colors.
|
||||
*/
|
||||
readonly inverse: CSPair;
|
||||
|
||||
/**
|
||||
Prints the text, but makes it invisible.
|
||||
*/
|
||||
readonly hidden: CSPair;
|
||||
|
||||
/**
|
||||
Puts a horizontal line through the center of the text. (Not widely supported)
|
||||
*/
|
||||
readonly strikethrough: CSPair;
|
||||
}
|
||||
|
||||
export interface ForegroundColor {
|
||||
readonly black: CSPair;
|
||||
readonly red: CSPair;
|
||||
readonly green: CSPair;
|
||||
readonly yellow: CSPair;
|
||||
readonly blue: CSPair;
|
||||
readonly cyan: CSPair;
|
||||
readonly magenta: CSPair;
|
||||
readonly white: CSPair;
|
||||
|
||||
/**
|
||||
Alias for `blackBright`.
|
||||
*/
|
||||
readonly gray: CSPair;
|
||||
|
||||
/**
|
||||
Alias for `blackBright`.
|
||||
*/
|
||||
readonly grey: CSPair;
|
||||
|
||||
readonly blackBright: CSPair;
|
||||
readonly redBright: CSPair;
|
||||
readonly greenBright: CSPair;
|
||||
readonly yellowBright: CSPair;
|
||||
readonly blueBright: CSPair;
|
||||
readonly cyanBright: CSPair;
|
||||
readonly magentaBright: CSPair;
|
||||
readonly whiteBright: CSPair;
|
||||
}
|
||||
|
||||
export interface BackgroundColor {
|
||||
readonly bgBlack: CSPair;
|
||||
readonly bgRed: CSPair;
|
||||
readonly bgGreen: CSPair;
|
||||
readonly bgYellow: CSPair;
|
||||
readonly bgBlue: CSPair;
|
||||
readonly bgCyan: CSPair;
|
||||
readonly bgMagenta: CSPair;
|
||||
readonly bgWhite: CSPair;
|
||||
|
||||
/**
|
||||
Alias for `bgBlackBright`.
|
||||
*/
|
||||
readonly bgGray: CSPair;
|
||||
|
||||
/**
|
||||
Alias for `bgBlackBright`.
|
||||
*/
|
||||
readonly bgGrey: CSPair;
|
||||
|
||||
readonly bgBlackBright: CSPair;
|
||||
readonly bgRedBright: CSPair;
|
||||
readonly bgGreenBright: CSPair;
|
||||
readonly bgYellowBright: CSPair;
|
||||
readonly bgBlueBright: CSPair;
|
||||
readonly bgCyanBright: CSPair;
|
||||
readonly bgMagentaBright: CSPair;
|
||||
readonly bgWhiteBright: CSPair;
|
||||
}
|
||||
|
||||
export interface ConvertColor {
|
||||
/**
|
||||
Convert from the RGB color space to the ANSI 256 color space.
|
||||
|
||||
@param red - (`0...255`)
|
||||
@param green - (`0...255`)
|
||||
@param blue - (`0...255`)
|
||||
*/
|
||||
rgbToAnsi256(red: number, green: number, blue: number): number;
|
||||
|
||||
/**
|
||||
Convert from the RGB HEX color space to the RGB color space.
|
||||
|
||||
@param hex - A hexadecimal string containing RGB data.
|
||||
*/
|
||||
hexToRgb(hex: string): [red: number, green: number, blue: number];
|
||||
|
||||
/**
|
||||
Convert from the RGB HEX color space to the ANSI 256 color space.
|
||||
|
||||
@param hex - A hexadecimal string containing RGB data.
|
||||
*/
|
||||
hexToAnsi256(hex: string): number;
|
||||
|
||||
/**
|
||||
Convert from the ANSI 256 color space to the ANSI 16 color space.
|
||||
|
||||
@param code - A number representing the ANSI 256 color.
|
||||
*/
|
||||
ansi256ToAnsi(code: number): number;
|
||||
|
||||
/**
|
||||
Convert from the RGB color space to the ANSI 16 color space.
|
||||
|
||||
@param red - (`0...255`)
|
||||
@param green - (`0...255`)
|
||||
@param blue - (`0...255`)
|
||||
*/
|
||||
rgbToAnsi(red: number, green: number, blue: number): number;
|
||||
|
||||
/**
|
||||
Convert from the RGB HEX color space to the ANSI 16 color space.
|
||||
|
||||
@param hex - A hexadecimal string containing RGB data.
|
||||
*/
|
||||
hexToAnsi(hex: string): number;
|
||||
}
|
||||
|
||||
/**
|
||||
Basic modifier names.
|
||||
*/
|
||||
export type ModifierName = keyof Modifier;
|
||||
|
||||
/**
|
||||
Basic foreground color names.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type ForegroundColorName = keyof ForegroundColor;
|
||||
|
||||
/**
|
||||
Basic background color names.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type BackgroundColorName = keyof BackgroundColor;
|
||||
|
||||
/**
|
||||
Basic color names. The combination of foreground and background color names.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type ColorName = ForegroundColorName | BackgroundColorName;
|
||||
|
||||
/**
|
||||
Basic modifier names.
|
||||
*/
|
||||
export const modifierNames: readonly ModifierName[];
|
||||
|
||||
/**
|
||||
Basic foreground color names.
|
||||
*/
|
||||
export const foregroundColorNames: readonly ForegroundColorName[];
|
||||
|
||||
/**
|
||||
Basic background color names.
|
||||
*/
|
||||
export const backgroundColorNames: readonly BackgroundColorName[];
|
||||
|
||||
/*
|
||||
Basic color names. The combination of foreground and background color names.
|
||||
*/
|
||||
export const colorNames: readonly ColorName[];
|
||||
|
||||
declare const ansiStyles: {
|
||||
readonly modifier: Modifier;
|
||||
readonly color: ColorBase & ForegroundColor;
|
||||
readonly bgColor: ColorBase & BackgroundColor;
|
||||
readonly codes: ReadonlyMap<number, number>;
|
||||
} & ForegroundColor & BackgroundColor & Modifier & ConvertColor;
|
||||
|
||||
export default ansiStyles;
|
||||
223
source/vendor/ansi-styles/index.js
vendored
Normal file
223
source/vendor/ansi-styles/index.js
vendored
Normal file
|
|
@ -0,0 +1,223 @@
|
|||
const ANSI_BACKGROUND_OFFSET = 10;
|
||||
|
||||
const wrapAnsi16 = (offset = 0) => code => `\u001B[${code + offset}m`;
|
||||
|
||||
const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
|
||||
|
||||
const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
|
||||
|
||||
const styles = {
|
||||
modifier: {
|
||||
reset: [0, 0],
|
||||
// 21 isn't widely supported and 22 does the same thing
|
||||
bold: [1, 22],
|
||||
dim: [2, 22],
|
||||
italic: [3, 23],
|
||||
underline: [4, 24],
|
||||
overline: [53, 55],
|
||||
inverse: [7, 27],
|
||||
hidden: [8, 28],
|
||||
strikethrough: [9, 29],
|
||||
},
|
||||
color: {
|
||||
black: [30, 39],
|
||||
red: [31, 39],
|
||||
green: [32, 39],
|
||||
yellow: [33, 39],
|
||||
blue: [34, 39],
|
||||
magenta: [35, 39],
|
||||
cyan: [36, 39],
|
||||
white: [37, 39],
|
||||
|
||||
// Bright color
|
||||
blackBright: [90, 39],
|
||||
gray: [90, 39], // Alias of `blackBright`
|
||||
grey: [90, 39], // Alias of `blackBright`
|
||||
redBright: [91, 39],
|
||||
greenBright: [92, 39],
|
||||
yellowBright: [93, 39],
|
||||
blueBright: [94, 39],
|
||||
magentaBright: [95, 39],
|
||||
cyanBright: [96, 39],
|
||||
whiteBright: [97, 39],
|
||||
},
|
||||
bgColor: {
|
||||
bgBlack: [40, 49],
|
||||
bgRed: [41, 49],
|
||||
bgGreen: [42, 49],
|
||||
bgYellow: [43, 49],
|
||||
bgBlue: [44, 49],
|
||||
bgMagenta: [45, 49],
|
||||
bgCyan: [46, 49],
|
||||
bgWhite: [47, 49],
|
||||
|
||||
// Bright color
|
||||
bgBlackBright: [100, 49],
|
||||
bgGray: [100, 49], // Alias of `bgBlackBright`
|
||||
bgGrey: [100, 49], // Alias of `bgBlackBright`
|
||||
bgRedBright: [101, 49],
|
||||
bgGreenBright: [102, 49],
|
||||
bgYellowBright: [103, 49],
|
||||
bgBlueBright: [104, 49],
|
||||
bgMagentaBright: [105, 49],
|
||||
bgCyanBright: [106, 49],
|
||||
bgWhiteBright: [107, 49],
|
||||
},
|
||||
};
|
||||
|
||||
export const modifierNames = Object.keys(styles.modifier);
|
||||
export const foregroundColorNames = Object.keys(styles.color);
|
||||
export const backgroundColorNames = Object.keys(styles.bgColor);
|
||||
export const colorNames = [...foregroundColorNames, ...backgroundColorNames];
|
||||
|
||||
function assembleStyles() {
|
||||
const codes = new Map();
|
||||
|
||||
for (const [groupName, group] of Object.entries(styles)) {
|
||||
for (const [styleName, style] of Object.entries(group)) {
|
||||
styles[styleName] = {
|
||||
open: `\u001B[${style[0]}m`,
|
||||
close: `\u001B[${style[1]}m`,
|
||||
};
|
||||
|
||||
group[styleName] = styles[styleName];
|
||||
|
||||
codes.set(style[0], style[1]);
|
||||
}
|
||||
|
||||
Object.defineProperty(styles, groupName, {
|
||||
value: group,
|
||||
enumerable: false,
|
||||
});
|
||||
}
|
||||
|
||||
Object.defineProperty(styles, 'codes', {
|
||||
value: codes,
|
||||
enumerable: false,
|
||||
});
|
||||
|
||||
styles.color.close = '\u001B[39m';
|
||||
styles.bgColor.close = '\u001B[49m';
|
||||
|
||||
styles.color.ansi = wrapAnsi16();
|
||||
styles.color.ansi256 = wrapAnsi256();
|
||||
styles.color.ansi16m = wrapAnsi16m();
|
||||
styles.bgColor.ansi = wrapAnsi16(ANSI_BACKGROUND_OFFSET);
|
||||
styles.bgColor.ansi256 = wrapAnsi256(ANSI_BACKGROUND_OFFSET);
|
||||
styles.bgColor.ansi16m = wrapAnsi16m(ANSI_BACKGROUND_OFFSET);
|
||||
|
||||
// From https://github.com/Qix-/color-convert/blob/3f0e0d4e92e235796ccb17f6e85c72094a651f49/conversions.js
|
||||
Object.defineProperties(styles, {
|
||||
rgbToAnsi256: {
|
||||
value(red, green, blue) {
|
||||
// We use the extended greyscale palette here, with the exception of
|
||||
// black and white. normal palette only has 4 greyscale shades.
|
||||
if (red === green && green === blue) {
|
||||
if (red < 8) {
|
||||
return 16;
|
||||
}
|
||||
|
||||
if (red > 248) {
|
||||
return 231;
|
||||
}
|
||||
|
||||
return Math.round(((red - 8) / 247) * 24) + 232;
|
||||
}
|
||||
|
||||
return 16
|
||||
+ (36 * Math.round(red / 255 * 5))
|
||||
+ (6 * Math.round(green / 255 * 5))
|
||||
+ Math.round(blue / 255 * 5);
|
||||
},
|
||||
enumerable: false,
|
||||
},
|
||||
hexToRgb: {
|
||||
value(hex) {
|
||||
const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
|
||||
if (!matches) {
|
||||
return [0, 0, 0];
|
||||
}
|
||||
|
||||
let [colorString] = matches;
|
||||
|
||||
if (colorString.length === 3) {
|
||||
colorString = [...colorString].map(character => character + character).join('');
|
||||
}
|
||||
|
||||
const integer = Number.parseInt(colorString, 16);
|
||||
|
||||
return [
|
||||
/* eslint-disable no-bitwise */
|
||||
(integer >> 16) & 0xFF,
|
||||
(integer >> 8) & 0xFF,
|
||||
integer & 0xFF,
|
||||
/* eslint-enable no-bitwise */
|
||||
];
|
||||
},
|
||||
enumerable: false,
|
||||
},
|
||||
hexToAnsi256: {
|
||||
value: hex => styles.rgbToAnsi256(...styles.hexToRgb(hex)),
|
||||
enumerable: false,
|
||||
},
|
||||
ansi256ToAnsi: {
|
||||
value(code) {
|
||||
if (code < 8) {
|
||||
return 30 + code;
|
||||
}
|
||||
|
||||
if (code < 16) {
|
||||
return 90 + (code - 8);
|
||||
}
|
||||
|
||||
let red;
|
||||
let green;
|
||||
let blue;
|
||||
|
||||
if (code >= 232) {
|
||||
red = (((code - 232) * 10) + 8) / 255;
|
||||
green = red;
|
||||
blue = red;
|
||||
} else {
|
||||
code -= 16;
|
||||
|
||||
const remainder = code % 36;
|
||||
|
||||
red = Math.floor(code / 36) / 5;
|
||||
green = Math.floor(remainder / 6) / 5;
|
||||
blue = (remainder % 6) / 5;
|
||||
}
|
||||
|
||||
const value = Math.max(red, green, blue) * 2;
|
||||
|
||||
if (value === 0) {
|
||||
return 30;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-bitwise
|
||||
let result = 30 + ((Math.round(blue) << 2) | (Math.round(green) << 1) | Math.round(red));
|
||||
|
||||
if (value === 2) {
|
||||
result += 60;
|
||||
}
|
||||
|
||||
return result;
|
||||
},
|
||||
enumerable: false,
|
||||
},
|
||||
rgbToAnsi: {
|
||||
value: (red, green, blue) => styles.ansi256ToAnsi(styles.rgbToAnsi256(red, green, blue)),
|
||||
enumerable: false,
|
||||
},
|
||||
hexToAnsi: {
|
||||
value: hex => styles.ansi256ToAnsi(styles.hexToAnsi256(hex)),
|
||||
enumerable: false,
|
||||
},
|
||||
});
|
||||
|
||||
return styles;
|
||||
}
|
||||
|
||||
const ansiStyles = assembleStyles();
|
||||
|
||||
export default ansiStyles;
|
||||
1
source/vendor/supports-color/browser.d.ts
vendored
Normal file
1
source/vendor/supports-color/browser.d.ts
vendored
Normal file
|
|
@ -0,0 +1 @@
|
|||
export {default} from './index.js';
|
||||
34
source/vendor/supports-color/browser.js
vendored
Normal file
34
source/vendor/supports-color/browser.js
vendored
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
/* eslint-env browser */
|
||||
|
||||
const level = (() => {
|
||||
if (!('navigator' in globalThis)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (globalThis.navigator.userAgentData) {
|
||||
const brand = navigator.userAgentData.brands.find(({brand}) => brand === 'Chromium');
|
||||
if (brand && brand.version > 93) {
|
||||
return 3;
|
||||
}
|
||||
}
|
||||
|
||||
if (/\b(Chrome|Chromium)\//.test(globalThis.navigator.userAgent)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
})();
|
||||
|
||||
const colorSupport = level !== 0 && {
|
||||
level,
|
||||
hasBasic: true,
|
||||
has256: level >= 2,
|
||||
has16m: level >= 3,
|
||||
};
|
||||
|
||||
const supportsColor = {
|
||||
stdout: colorSupport,
|
||||
stderr: colorSupport,
|
||||
};
|
||||
|
||||
export default supportsColor;
|
||||
55
source/vendor/supports-color/index.d.ts
vendored
Normal file
55
source/vendor/supports-color/index.d.ts
vendored
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
import type {WriteStream} from 'node:tty';
|
||||
|
||||
export type Options = {
|
||||
/**
|
||||
Whether `process.argv` should be sniffed for `--color` and `--no-color` flags.
|
||||
|
||||
@default true
|
||||
*/
|
||||
readonly sniffFlags?: boolean;
|
||||
};
|
||||
|
||||
/**
|
||||
Levels:
|
||||
- `0` - All colors disabled.
|
||||
- `1` - Basic 16 colors support.
|
||||
- `2` - ANSI 256 colors support.
|
||||
- `3` - Truecolor 16 million colors support.
|
||||
*/
|
||||
export type ColorSupportLevel = 0 | 1 | 2 | 3;
|
||||
|
||||
/**
|
||||
Detect whether the terminal supports color.
|
||||
*/
|
||||
export type ColorSupport = {
|
||||
/**
|
||||
The color level.
|
||||
*/
|
||||
level: ColorSupportLevel;
|
||||
|
||||
/**
|
||||
Whether basic 16 colors are supported.
|
||||
*/
|
||||
hasBasic: boolean;
|
||||
|
||||
/**
|
||||
Whether ANSI 256 colors are supported.
|
||||
*/
|
||||
has256: boolean;
|
||||
|
||||
/**
|
||||
Whether Truecolor 16 million colors are supported.
|
||||
*/
|
||||
has16m: boolean;
|
||||
};
|
||||
|
||||
export type ColorInfo = ColorSupport | false;
|
||||
|
||||
export function createSupportsColor(stream?: WriteStream, options?: Options): ColorInfo;
|
||||
|
||||
declare const supportsColor: {
|
||||
stdout: ColorInfo;
|
||||
stderr: ColorInfo;
|
||||
};
|
||||
|
||||
export default supportsColor;
|
||||
190
source/vendor/supports-color/index.js
vendored
Normal file
190
source/vendor/supports-color/index.js
vendored
Normal file
|
|
@ -0,0 +1,190 @@
|
|||
import process from 'node:process';
|
||||
import os from 'node:os';
|
||||
import tty from 'node:tty';
|
||||
|
||||
// From: https://github.com/sindresorhus/has-flag/blob/main/index.js
|
||||
/// function hasFlag(flag, argv = globalThis.Deno?.args ?? process.argv) {
|
||||
function hasFlag(flag, argv = globalThis.Deno ? globalThis.Deno.args : process.argv) {
|
||||
const prefix = flag.startsWith('-') ? '' : (flag.length === 1 ? '-' : '--');
|
||||
const position = argv.indexOf(prefix + flag);
|
||||
const terminatorPosition = argv.indexOf('--');
|
||||
return position !== -1 && (terminatorPosition === -1 || position < terminatorPosition);
|
||||
}
|
||||
|
||||
const {env} = process;
|
||||
|
||||
let flagForceColor;
|
||||
if (
|
||||
hasFlag('no-color')
|
||||
|| hasFlag('no-colors')
|
||||
|| hasFlag('color=false')
|
||||
|| hasFlag('color=never')
|
||||
) {
|
||||
flagForceColor = 0;
|
||||
} else if (
|
||||
hasFlag('color')
|
||||
|| hasFlag('colors')
|
||||
|| hasFlag('color=true')
|
||||
|| hasFlag('color=always')
|
||||
) {
|
||||
flagForceColor = 1;
|
||||
}
|
||||
|
||||
function envForceColor() {
|
||||
if ('FORCE_COLOR' in env) {
|
||||
if (env.FORCE_COLOR === 'true') {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (env.FORCE_COLOR === 'false') {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return env.FORCE_COLOR.length === 0 ? 1 : Math.min(Number.parseInt(env.FORCE_COLOR, 10), 3);
|
||||
}
|
||||
}
|
||||
|
||||
function translateLevel(level) {
|
||||
if (level === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return {
|
||||
level,
|
||||
hasBasic: true,
|
||||
has256: level >= 2,
|
||||
has16m: level >= 3,
|
||||
};
|
||||
}
|
||||
|
||||
function _supportsColor(haveStream, {streamIsTTY, sniffFlags = true} = {}) {
|
||||
const noFlagForceColor = envForceColor();
|
||||
if (noFlagForceColor !== undefined) {
|
||||
flagForceColor = noFlagForceColor;
|
||||
}
|
||||
|
||||
const forceColor = sniffFlags ? flagForceColor : noFlagForceColor;
|
||||
|
||||
if (forceColor === 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (sniffFlags) {
|
||||
if (hasFlag('color=16m')
|
||||
|| hasFlag('color=full')
|
||||
|| hasFlag('color=truecolor')) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (hasFlag('color=256')) {
|
||||
return 2;
|
||||
}
|
||||
}
|
||||
|
||||
// Check for Azure DevOps pipelines.
|
||||
// Has to be above the `!streamIsTTY` check.
|
||||
if ('TF_BUILD' in env && 'AGENT_NAME' in env) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if (haveStream && !streamIsTTY && forceColor === undefined) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
const min = forceColor || 0;
|
||||
|
||||
if (env.TERM === 'dumb') {
|
||||
return min;
|
||||
}
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
// Windows 10 build 10586 is the first Windows release that supports 256 colors.
|
||||
// Windows 10 build 14931 is the first release that supports 16m/TrueColor.
|
||||
const osRelease = os.release().split('.');
|
||||
if (
|
||||
Number(osRelease[0]) >= 10
|
||||
&& Number(osRelease[2]) >= 10_586
|
||||
) {
|
||||
return Number(osRelease[2]) >= 14_931 ? 3 : 2;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ('CI' in env) {
|
||||
if (['GITHUB_ACTIONS', 'GITEA_ACTIONS', 'CIRCLECI'].some(key => key in env)) {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (['TRAVIS', 'APPVEYOR', 'GITLAB_CI', 'BUILDKITE', 'DRONE'].some(sign => sign in env) || env.CI_NAME === 'codeship') {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
if ('TEAMCITY_VERSION' in env) {
|
||||
return /^(9\.(0*[1-9]\d*)\.|\d{2,}\.)/.test(env.TEAMCITY_VERSION) ? 1 : 0;
|
||||
}
|
||||
|
||||
if (env.COLORTERM === 'truecolor') {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (env.TERM === 'xterm-kitty') {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (env.TERM === 'xterm-ghostty') {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if (env.TERM === 'wezterm') {
|
||||
return 3;
|
||||
}
|
||||
|
||||
if ('TERM_PROGRAM' in env) {
|
||||
const version = Number.parseInt((env.TERM_PROGRAM_VERSION || '').split('.')[0], 10);
|
||||
|
||||
switch (env.TERM_PROGRAM) {
|
||||
case 'iTerm.app': {
|
||||
return version >= 3 ? 3 : 2;
|
||||
}
|
||||
|
||||
case 'Apple_Terminal': {
|
||||
return 2;
|
||||
}
|
||||
// No default
|
||||
}
|
||||
}
|
||||
|
||||
if (/-256(color)?$/i.test(env.TERM)) {
|
||||
return 2;
|
||||
}
|
||||
|
||||
if (/^screen|^xterm|^vt100|^vt220|^rxvt|color|ansi|cygwin|linux/i.test(env.TERM)) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
if ('COLORTERM' in env) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
return min;
|
||||
}
|
||||
|
||||
export function createSupportsColor(stream, options = {}) {
|
||||
const level = _supportsColor(stream, {
|
||||
streamIsTTY: stream && stream.isTTY,
|
||||
...options,
|
||||
});
|
||||
|
||||
return translateLevel(level);
|
||||
}
|
||||
|
||||
const supportsColor = {
|
||||
stdout: createSupportsColor({isTTY: tty.isatty(1)}),
|
||||
stderr: createSupportsColor({isTTY: tty.isatty(2)}),
|
||||
};
|
||||
|
||||
export default supportsColor;
|
||||
158
test.js
158
test.js
|
|
@ -1,158 +0,0 @@
|
|||
'use strict';
|
||||
var assert = require('assert');
|
||||
var requireUncached = require('require-uncached');
|
||||
var resolveFrom = require('resolve-from');
|
||||
var semver = require('semver');
|
||||
var chalk = require('./');
|
||||
|
||||
describe('chalk', function () {
|
||||
it('should style string', function () {
|
||||
assert.equal(chalk.underline('foo'), '\u001b[4mfoo\u001b[24m');
|
||||
assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m');
|
||||
assert.equal(chalk.bgRed('foo'), '\u001b[41mfoo\u001b[49m');
|
||||
});
|
||||
|
||||
it('should support applying multiple styles at once', function () {
|
||||
assert.equal(chalk.red.bgGreen.underline('foo'), '\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39m');
|
||||
assert.equal(chalk.underline.red.bgGreen('foo'), '\u001b[4m\u001b[31m\u001b[42mfoo\u001b[49m\u001b[39m\u001b[24m');
|
||||
});
|
||||
|
||||
it('should support nesting styles', function () {
|
||||
assert.equal(
|
||||
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
|
||||
'\u001b[31mfoo\u001b[4m\u001b[44mbar\u001b[49m\u001b[24m!\u001b[39m'
|
||||
);
|
||||
});
|
||||
|
||||
it('should support nesting styles of the same type (color, underline, bg)', function () {
|
||||
assert.equal(
|
||||
chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'),
|
||||
'\u001b[31ma\u001b[33mb\u001b[32mc\u001b[33mb\u001b[31mc\u001b[39m'
|
||||
);
|
||||
});
|
||||
|
||||
it('should reset all styles with `.reset()`', function () {
|
||||
assert.equal(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001b[0m\u001b[31m\u001b[42m\u001b[4mfoo\u001b[24m\u001b[49m\u001b[39mfoo\u001b[0m');
|
||||
});
|
||||
|
||||
it('should be able to cache multiple styles', function () {
|
||||
var red = chalk.red;
|
||||
var green = chalk.green;
|
||||
var redBold = red.bold;
|
||||
var greenBold = green.bold;
|
||||
|
||||
assert.notEqual(red('foo'), green('foo'));
|
||||
assert.notEqual(redBold('bar'), greenBold('bar'));
|
||||
assert.notEqual(green('baz'), greenBold('baz'));
|
||||
});
|
||||
|
||||
it('should alias gray to grey', function () {
|
||||
assert.equal(chalk.grey('foo'), '\u001b[90mfoo\u001b[39m');
|
||||
});
|
||||
|
||||
it('should support variable number of arguments', function () {
|
||||
assert.equal(chalk.red('foo', 'bar'), '\u001b[31mfoo bar\u001b[39m');
|
||||
});
|
||||
|
||||
it('should support falsy values', function () {
|
||||
assert.equal(chalk.red(0), '\u001b[31m0\u001b[39m');
|
||||
});
|
||||
|
||||
it('don\'t output escape codes if the input is empty', function () {
|
||||
assert.equal(chalk.red(), '');
|
||||
});
|
||||
});
|
||||
|
||||
describe('chalk on windows', function () {
|
||||
var originalEnv;
|
||||
var originalPlatform;
|
||||
|
||||
// in node versions older than 0.12.x process.platform cannot be overridden
|
||||
if (semver.lt(process.version, '0.12.0')) {
|
||||
return;
|
||||
}
|
||||
|
||||
before(function () {
|
||||
originalEnv = process.env;
|
||||
originalPlatform = process.platform;
|
||||
});
|
||||
|
||||
after(function () {
|
||||
process.env = originalEnv;
|
||||
Object.defineProperty(process, 'platform', {value: originalPlatform});
|
||||
});
|
||||
|
||||
beforeEach(function () {
|
||||
process.env = {};
|
||||
Object.defineProperty(process, 'platform', {value: 'win32'});
|
||||
// since chalk internally modifies ansiStyles.blue.open, ansi-styles needs
|
||||
// to be removed from the require cache for require-uncached to work
|
||||
delete require.cache[resolveFrom(__dirname, 'ansi-styles')];
|
||||
});
|
||||
|
||||
it('should replace blue foreground color in cmd.exe', function () {
|
||||
process.env.TERM = 'dumb';
|
||||
var chalkCtx = requireUncached('./');
|
||||
assert.equal(chalkCtx.blue('foo'), '\u001b[94mfoo\u001b[39m');
|
||||
});
|
||||
|
||||
it('shouldn\'t replace blue foreground color in xterm based terminals', function () {
|
||||
process.env.TERM = 'xterm-256color';
|
||||
var chalkCtx = requireUncached('./');
|
||||
assert.equal(chalkCtx.blue('foo'), '\u001b[34mfoo\u001b[39m');
|
||||
});
|
||||
|
||||
it('should not apply dimmed styling on gray strings, see https://github.com/chalk/chalk/issues/58', function () {
|
||||
process.env.TERM = 'dumb';
|
||||
var chalkCtx = requireUncached('./');
|
||||
assert.equal(chalkCtx.gray.dim('foo'), '\u001b[90mfoo\u001b[22m\u001b[39m');
|
||||
});
|
||||
|
||||
it('should apply dimmed styling on xterm compatible terminals', function () {
|
||||
process.env.TERM = 'xterm';
|
||||
var chalkCtx = requireUncached('./');
|
||||
assert.equal(chalkCtx.gray.dim('foo'), '\u001b[90m\u001b[2mfoo\u001b[22m\u001b[39m');
|
||||
});
|
||||
|
||||
it('should apply dimmed styling on strings of other colors', function () {
|
||||
process.env.TERM = 'dumb';
|
||||
var chalkCtx = requireUncached('./');
|
||||
assert.equal(chalkCtx.blue.dim('foo'), '\u001b[94m\u001b[2mfoo\u001b[22m\u001b[39m');
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('chalk.enabled', function () {
|
||||
it('should not output colors when manually disabled', function () {
|
||||
chalk.enabled = false;
|
||||
assert.equal(chalk.red('foo'), 'foo');
|
||||
chalk.enabled = true;
|
||||
});
|
||||
});
|
||||
|
||||
describe('chalk.constructor', function () {
|
||||
it('should create a isolated context where colors can be disabled', function () {
|
||||
var ctx = new chalk.constructor({enabled: false});
|
||||
assert.equal(ctx.red('foo'), 'foo');
|
||||
assert.equal(chalk.red('foo'), '\u001b[31mfoo\u001b[39m');
|
||||
});
|
||||
});
|
||||
|
||||
describe('chalk.styles', function () {
|
||||
it('should expose the styles as ANSI escape codes', function () {
|
||||
assert.equal(chalk.styles.red.open, '\u001b[31m');
|
||||
});
|
||||
});
|
||||
|
||||
describe('chalk.hasColor()', function () {
|
||||
it('should detect whether a string has color', function () {
|
||||
assert(chalk.hasColor(chalk.green('foo')));
|
||||
assert(!chalk.hasColor(chalk.stripColor(chalk.green('foo'))));
|
||||
});
|
||||
});
|
||||
|
||||
describe('chalk.stripColor()', function () {
|
||||
it('should strip color from string', function () {
|
||||
assert.equal(chalk.stripColor(chalk.underline.red.bgGreen('foo')), 'foo');
|
||||
});
|
||||
});
|
||||
3
test/_fixture.js
Normal file
3
test/_fixture.js
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
import chalk, {chalkStderr} from '../source/index.js';
|
||||
|
||||
console.log(`${chalk.hex('#ff6159')('testout')} ${chalkStderr.hex('#ff6159')('testerr')}`);
|
||||
126
test/chalk.js
Normal file
126
test/chalk.js
Normal file
|
|
@ -0,0 +1,126 @@
|
|||
import process from 'node:process';
|
||||
import test from 'ava';
|
||||
import chalk, {Chalk, chalkStderr} from '../source/index.js';
|
||||
|
||||
chalk.level = 3;
|
||||
chalkStderr.level = 3;
|
||||
|
||||
console.log('TERM:', process.env.TERM || '[none]');
|
||||
console.log('platform:', process.platform || '[unknown]');
|
||||
|
||||
test('don\'t add any styling when called as the base function', t => {
|
||||
t.is(chalk('foo'), 'foo');
|
||||
});
|
||||
|
||||
test('support multiple arguments in base function', t => {
|
||||
t.is(chalk('hello', 'there'), 'hello there');
|
||||
});
|
||||
|
||||
test('support automatic casting to string', t => {
|
||||
t.is(chalk(['hello', 'there']), 'hello,there');
|
||||
t.is(chalk(123), '123');
|
||||
|
||||
t.is(chalk.bold(['foo', 'bar']), '\u001B[1mfoo,bar\u001B[22m');
|
||||
t.is(chalk.green(98_765), '\u001B[32m98765\u001B[39m');
|
||||
});
|
||||
|
||||
test('style string', t => {
|
||||
t.is(chalk.underline('foo'), '\u001B[4mfoo\u001B[24m');
|
||||
t.is(chalk.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
t.is(chalk.bgRed('foo'), '\u001B[41mfoo\u001B[49m');
|
||||
});
|
||||
|
||||
test('support applying multiple styles at once', t => {
|
||||
t.is(chalk.red.bgGreen.underline('foo'), '\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39m');
|
||||
t.is(chalk.underline.red.bgGreen('foo'), '\u001B[4m\u001B[31m\u001B[42mfoo\u001B[49m\u001B[39m\u001B[24m');
|
||||
});
|
||||
|
||||
test('support nesting styles', t => {
|
||||
t.is(
|
||||
chalk.red('foo' + chalk.underline.bgBlue('bar') + '!'),
|
||||
'\u001B[31mfoo\u001B[4m\u001B[44mbar\u001B[49m\u001B[24m!\u001B[39m',
|
||||
);
|
||||
});
|
||||
|
||||
test('support nesting styles of the same type (color, underline, bg)', t => {
|
||||
t.is(
|
||||
chalk.red('a' + chalk.yellow('b' + chalk.green('c') + 'b') + 'c'),
|
||||
'\u001B[31ma\u001B[33mb\u001B[32mc\u001B[39m\u001B[31m\u001B[33mb\u001B[39m\u001B[31mc\u001B[39m',
|
||||
);
|
||||
});
|
||||
|
||||
test('reset all styles with `.reset()`', t => {
|
||||
t.is(chalk.reset(chalk.red.bgGreen.underline('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
|
||||
});
|
||||
|
||||
test('support caching multiple styles', t => {
|
||||
const {red, green} = chalk.red;
|
||||
const redBold = red.bold;
|
||||
const greenBold = green.bold;
|
||||
|
||||
t.not(red('foo'), green('foo'));
|
||||
t.not(redBold('bar'), greenBold('bar'));
|
||||
t.not(green('baz'), greenBold('baz'));
|
||||
});
|
||||
|
||||
test('alias gray to grey', t => {
|
||||
t.is(chalk.grey('foo'), '\u001B[90mfoo\u001B[39m');
|
||||
});
|
||||
|
||||
test('support variable number of arguments', t => {
|
||||
t.is(chalk.red('foo', 'bar'), '\u001B[31mfoo bar\u001B[39m');
|
||||
});
|
||||
|
||||
test('support falsy values', t => {
|
||||
t.is(chalk.red(0), '\u001B[31m0\u001B[39m');
|
||||
});
|
||||
|
||||
test('don\'t output escape codes if the input is empty', t => {
|
||||
t.is(chalk.red(), '');
|
||||
t.is(chalk.red.blue.black(), '');
|
||||
});
|
||||
|
||||
test('keep Function.prototype methods', t => {
|
||||
t.is(Reflect.apply(chalk.grey, null, ['foo']), '\u001B[90mfoo\u001B[39m');
|
||||
t.is(chalk.reset(chalk.red.bgGreen.underline.bind(null)('foo') + 'foo'), '\u001B[0m\u001B[31m\u001B[42m\u001B[4mfoo\u001B[24m\u001B[49m\u001B[39mfoo\u001B[0m');
|
||||
t.is(chalk.red.blue.black.call(null), '');
|
||||
});
|
||||
|
||||
test('line breaks should open and close colors', t => {
|
||||
t.is(chalk.grey('hello\nworld'), '\u001B[90mhello\u001B[39m\n\u001B[90mworld\u001B[39m');
|
||||
});
|
||||
|
||||
test('line breaks should open and close colors with CRLF', t => {
|
||||
t.is(chalk.grey('hello\r\nworld'), '\u001B[90mhello\u001B[39m\r\n\u001B[90mworld\u001B[39m');
|
||||
});
|
||||
|
||||
test('properly convert RGB to 16 colors on basic color terminals', t => {
|
||||
t.is(new Chalk({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
|
||||
t.is(new Chalk({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({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
|
||||
t.is(new Chalk({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
|
||||
t.is(new Chalk({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({level: 0}).hex('#FF0000')('hello'), 'hello');
|
||||
t.is(new Chalk({level: 0}).bgHex('#FF0000')('hello'), 'hello');
|
||||
});
|
||||
|
||||
test('supports blackBright color', t => {
|
||||
t.is(chalk.blackBright('foo'), '\u001B[90mfoo\u001B[39m');
|
||||
});
|
||||
|
||||
test('sets correct level for chalkStderr and respects it', t => {
|
||||
t.is(chalkStderr.level, 3);
|
||||
t.is(chalkStderr.red.bold('foo'), '\u001B[31m\u001B[1mfoo\u001B[22m\u001B[39m');
|
||||
});
|
||||
|
||||
test('keeps function prototype methods', t => {
|
||||
t.is(chalk.apply(chalk, ['foo']), 'foo');
|
||||
t.is(chalk.bind(chalk, 'foo')(), 'foo');
|
||||
t.is(chalk.call(chalk, 'foo'), 'foo');
|
||||
});
|
||||
24
test/instance.js
Normal file
24
test/instance.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import test from 'ava';
|
||||
import chalk, {Chalk} from '../source/index.js';
|
||||
|
||||
chalk.level = 1;
|
||||
|
||||
test('create an isolated context where colors can be disabled (by level)', t => {
|
||||
const instance = new Chalk({level: 0});
|
||||
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('the `level` option should be a number from 0 to 3', t => {
|
||||
/* eslint-disable no-new */
|
||||
t.throws(() => {
|
||||
new Chalk({level: 10});
|
||||
}, {message: /should be an integer from 0 to 3/});
|
||||
|
||||
t.throws(() => {
|
||||
new Chalk({level: -1});
|
||||
}, {message: /should be an integer from 0 to 3/});
|
||||
/* eslint-enable no-new */
|
||||
});
|
||||
43
test/level.js
Normal file
43
test/level.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
import {fileURLToPath} from 'node:url';
|
||||
import test from 'ava';
|
||||
import {execaNode} from 'execa';
|
||||
import chalk from '../source/index.js';
|
||||
|
||||
chalk.level = 1;
|
||||
|
||||
test('don\'t output colors when manually disabled', t => {
|
||||
const oldLevel = chalk.level;
|
||||
chalk.level = 0;
|
||||
t.is(chalk.red('foo'), 'foo');
|
||||
chalk.level = oldLevel;
|
||||
});
|
||||
|
||||
test('enable/disable colors based on overall chalk .level property, not individual instances', t => {
|
||||
const oldLevel = chalk.level;
|
||||
chalk.level = 1;
|
||||
const {red} = chalk;
|
||||
t.is(red.level, 1);
|
||||
chalk.level = 0;
|
||||
t.is(red.level, chalk.level);
|
||||
chalk.level = oldLevel;
|
||||
});
|
||||
|
||||
test('propagate enable/disable changes from child colors', t => {
|
||||
const oldLevel = chalk.level;
|
||||
chalk.level = 1;
|
||||
const {red} = chalk;
|
||||
t.is(red.level, 1);
|
||||
t.is(chalk.level, 1);
|
||||
red.level = 0;
|
||||
t.is(red.level, 0);
|
||||
t.is(chalk.level, 0);
|
||||
chalk.level = 1;
|
||||
t.is(red.level, 1);
|
||||
t.is(chalk.level, 1);
|
||||
chalk.level = oldLevel;
|
||||
});
|
||||
|
||||
test('disable colors if they are not supported', async t => {
|
||||
const {stdout} = await execaNode(fileURLToPath(new URL('_fixture.js', import.meta.url)));
|
||||
t.is(stdout, 'testout testerr');
|
||||
});
|
||||
24
test/no-color-support.js
Normal file
24
test/no-color-support.js
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
import test from 'ava';
|
||||
import chalk from '../source/index.js';
|
||||
|
||||
// TODO: Do this when ESM supports loader hooks
|
||||
// Spoof supports-color
|
||||
// require('./_supports-color')(__dirname, {
|
||||
// stdout: {
|
||||
// level: 0,
|
||||
// hasBasic: false,
|
||||
// has256: false,
|
||||
// has16m: false
|
||||
// },
|
||||
// stderr: {
|
||||
// level: 0,
|
||||
// hasBasic: false,
|
||||
// has256: false,
|
||||
// has16m: false
|
||||
// }
|
||||
// });
|
||||
|
||||
test('colors can be forced by using chalk.level', t => {
|
||||
chalk.level = 1;
|
||||
t.is(chalk.green('hello'), '\u001B[32mhello\u001B[39m');
|
||||
});
|
||||
39
test/visible.js
Normal file
39
test/visible.js
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import test from 'ava';
|
||||
import chalk, {Chalk} from '../source/index.js';
|
||||
|
||||
chalk.level = 1;
|
||||
|
||||
test('visible: normal output when level > 0', t => {
|
||||
const instance = new Chalk({level: 3});
|
||||
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 level is too low', t => {
|
||||
const instance = new Chalk({level: 0});
|
||||
t.is(instance.visible.red('foo'), '');
|
||||
t.is(instance.red.visible('foo'), '');
|
||||
});
|
||||
|
||||
test('test switching back and forth between level == 0 and level > 0', t => {
|
||||
const instance = new Chalk({level: 3});
|
||||
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');
|
||||
t.is(instance.visible('foo'), 'foo');
|
||||
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
|
||||
instance.level = 0;
|
||||
t.is(instance.red('foo'), 'foo');
|
||||
t.is(instance.visible('foo'), '');
|
||||
t.is(instance.visible.red('foo'), '');
|
||||
t.is(instance.red.visible('foo'), '');
|
||||
t.is(instance.red('foo'), 'foo');
|
||||
|
||||
instance.level = 3;
|
||||
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');
|
||||
t.is(instance.visible('foo'), 'foo');
|
||||
t.is(instance.red('foo'), '\u001B[31mfoo\u001B[39m');
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue