diff --git a/.editorconfig b/.editorconfig index 1c6314a..8311fe1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -1,3 +1,4 @@ +# editorconfig.org root = true [*] @@ -7,6 +8,9 @@ charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true -[*.yml] +[package.json] indent_style = space indent_size = 2 + +[*.md] +trim_trailing_whitespace = false diff --git a/.gitattributes b/.gitattributes index 6313b56..176a458 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* text=auto eol=lf +* text=auto diff --git a/.github/funding.yml b/.github/funding.yml deleted file mode 100644 index 1cd069f..0000000 --- a/.github/funding.yml +++ /dev/null @@ -1,4 +0,0 @@ -github: [sindresorhus, Qix-] -open_collective: sindresorhus -tidelift: npm/chalk -custom: https://sindresorhus.com/donate diff --git a/.github/security.md b/.github/security.md deleted file mode 100644 index 5358dc5..0000000 --- a/.github/security.md +++ /dev/null @@ -1,3 +0,0 @@ -# Security Policy - -To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index d588995..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,26 +0,0 @@ -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 diff --git a/.gitignore b/.gitignore index 6bff314..3c3629e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1 @@ node_modules -yarn.lock -coverage -.nyc_output diff --git a/.jshintrc b/.jshintrc new file mode 100644 index 0000000..3e1760c --- /dev/null +++ b/.jshintrc @@ -0,0 +1,17 @@ +{ + "node": true, + "esnext": true, + "bitwise": true, + "camelcase": true, + "curly": true, + "eqeqeq": true, + "immed": true, + "indent": 4, + "newcap": true, + "noarg": true, + "proto": true, + "quotmark": "single", + "undef": true, + "unused": "vars", + "strict": true +} diff --git a/.npmrc b/.npmrc deleted file mode 100644 index 43c97e7..0000000 --- a/.npmrc +++ /dev/null @@ -1 +0,0 @@ -package-lock=false diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..244b7e8 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,3 @@ +language: node_js +node_js: + - '0.10' diff --git a/benchmark.js b/benchmark.js index c5c6e27..ce3f2c4 100644 --- a/benchmark.js +++ b/benchmark.js @@ -1,57 +1,22 @@ -/* globals suite, bench */ -import chalk from './index.js'; +var chalk = require('./'); -suite('chalk', () => { - const chalkRed = chalk.red; - const chalkBgRed = chalk.bgRed; - const chalkBlueBgRed = chalk.blue.bgRed; - const chalkBlueBgRedBold = chalk.blue.bgRed.bold; +suite('chalk', function () { + set('iterations', 100000); - const blueStyledString = 'the fox jumps' + chalk.blue('over the lazy dog') + '!'; - - bench('1 style', () => { + bench('single style', function () { chalk.red('the fox jumps over the lazy dog'); }); - bench('2 styles', () => { - chalk.blue.bgRed('the fox jumps over the lazy dog'); - }); - - bench('3 styles', () => { + bench('several styles', function () { chalk.blue.bgRed.bold('the fox jumps over the lazy dog'); }); - bench('cached: 1 style', () => { - chalkRed('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: 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`; + bench('nested styles', function () { + chalk.red('the fox jumps', chalk.underline.bgBlue('over the lazy dog') + '!'); }); }); diff --git a/code-of-conduct.md b/code-of-conduct.md deleted file mode 100644 index a7c7db8..0000000 --- a/code-of-conduct.md +++ /dev/null @@ -1,74 +0,0 @@ -# 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/ diff --git a/contributing.md b/contributing.md deleted file mode 100644 index 28bfd4b..0000000 --- a/contributing.md +++ /dev/null @@ -1,3 +0,0 @@ -# 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. diff --git a/examples/rainbow.js b/examples/rainbow.js deleted file mode 100644 index 5b2b1d2..0000000 --- a/examples/rainbow.js +++ /dev/null @@ -1,38 +0,0 @@ -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(); diff --git a/examples/screenshot.js b/examples/screenshot.js deleted file mode 100644 index 9ad164b..0000000 --- a/examples/screenshot.js +++ /dev/null @@ -1,27 +0,0 @@ -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) + ' '); -} diff --git a/index.js b/index.js new file mode 100644 index 0000000..ac1f168 --- /dev/null +++ b/index.js @@ -0,0 +1,95 @@ +'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 chalk = module.exports; + +function build(_styles) { + var builder = function builder() { + return applyStyle.apply(builder, arguments); + }; + builder._styles = _styles; + // __proto__ is used because we must return a function, but there is + // no way to create a function with a different prototype. + builder.__proto__ = proto; + return builder; +} + +var styles = (function () { + var ret = {}; + + ansiStyles.grey = ansiStyles.gray; + + Object.keys(ansiStyles).forEach(function (key) { + ansiStyles[key].closeRe = new RegExp(escapeStringRegexp(ansiStyles[key].close), 'g'); + + ret[key] = { + get: function () { + return build(this._styles.concat(key)); + } + }; + }); + + return ret; +})(); + +var proto = defineProps(function chalk() {}, styles); + +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 (!chalk.enabled || !str) { + return str; + } + + /*jshint validthis: true*/ + var nestedStyles = this._styles; + + for (var i = 0; i < nestedStyles.length; 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; + } + + return str; +} + +function init() { + var ret = {}; + + Object.keys(styles).forEach(function (name) { + ret[name] = { + get: function () { + return build([name]); + } + }; + }); + + return ret; +} + +defineProps(chalk, init()); + +chalk.styles = ansiStyles; +chalk.hasColor = hasAnsi; +chalk.stripColor = stripAnsi; +chalk.supportsColor = supportsColor; + +// detect mode if not set manually +if (chalk.enabled === undefined) { + chalk.enabled = chalk.supportsColor; +} diff --git a/license b/license index fa7ceba..654d0bf 100644 --- a/license +++ b/license @@ -1,9 +1,21 @@ -MIT License +The MIT License (MIT) -Copyright (c) Sindre Sorhus (https://sindresorhus.com) +Copyright (c) Sindre Sorhus (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. diff --git a/logo.png b/logo.png new file mode 100644 index 0000000..1228ccf Binary files /dev/null and b/logo.png differ diff --git a/logo.svg b/logo.svg new file mode 100644 index 0000000..28d1881 --- /dev/null +++ b/logo.svg @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/media/logo.png b/media/logo.png deleted file mode 100644 index dad5674..0000000 Binary files a/media/logo.png and /dev/null differ diff --git a/media/logo.svg b/media/logo.svg deleted file mode 100644 index fcc3ea1..0000000 --- a/media/logo.svg +++ /dev/null @@ -1 +0,0 @@ - \ No newline at end of file diff --git a/media/screenshot.png b/media/screenshot.png deleted file mode 100644 index da9d89b..0000000 Binary files a/media/screenshot.png and /dev/null differ diff --git a/package.json b/package.json index c9e0dc5..e58fe48 100644 --- a/package.json +++ b/package.json @@ -1,83 +1,53 @@ { - "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" - ] - } + "name": "chalk", + "version": "0.5.1", + "description": "Terminal string styling done right. Created because the `colors` module does some really horrible things.", + "license": "MIT", + "repository": "sindresorhus/chalk", + "maintainers": [ + "Sindre Sorhus (http://sindresorhus.com)", + "Joshua Appelman " + ], + "engines": { + "node": ">=0.10.0" + }, + "scripts": { + "test": "mocha", + "bench": "matcha benchmark.js" + }, + "files": [ + "index.js" + ], + "keywords": [ + "color", + "colour", + "colors", + "terminal", + "console", + "cli", + "string", + "ansi", + "styles", + "tty", + "formatting", + "rgb", + "256", + "shell", + "xterm", + "log", + "logging", + "command-line", + "text" + ], + "dependencies": { + "ansi-styles": "^1.1.0", + "escape-string-regexp": "^1.0.0", + "has-ansi": "^0.1.0", + "strip-ansi": "^0.3.0", + "supports-color": "^0.2.0" + }, + "devDependencies": { + "matcha": "^0.5.0", + "mocha": "*" + } } diff --git a/readme.md b/readme.md index ce1f3f3..239c791 100644 --- a/readme.md +++ b/readme.md @@ -1,189 +1,152 @@ -

-
-
- Chalk -
-
-
-

+# chalk > Terminal string styling done right -[![Coverage Status](https://codecov.io/gh/chalk/chalk/branch/main/graph/badge.svg)](https://codecov.io/gh/chalk/chalk) -[![npm dependents](https://badgen.net/npm/dependents/chalk)](https://www.npmjs.com/package/chalk?activeTab=dependents) -[![Downloads](https://badgen.net/npm/dt/chalk)](https://www.npmjs.com/package/chalk) +[![Build Status](https://travis-ci.org/sindresorhus/chalk.svg?branch=master)](https://travis-ci.org/sindresorhus/chalk) +![](http://img.shields.io/badge/unicorn-approved-ff69b4.svg) -![](media/screenshot.png) +[colors.js](https://github.com/Marak/colors.js) is currently 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 +![screenshot](https://github.com/sindresorhus/ansi-styles/raw/master/screenshot.png) -## Highlights -- Expressive API +## Why + - Highly performant -- No dependencies +- Doesn't extend String.prototype +- Expressive API - Ability to nest styles -- [256/Truecolor color support](#256-and-truecolor-color-support) -- Auto-detects color support -- Doesn't extend `String.prototype` - Clean and focused +- Auto-detects color support - Actively maintained -- [Used by ~115,000 packages](https://www.npmjs.com/browse/depended/chalk) as of July 4, 2024 +- [Used by 1000+ modules](https://npmjs.org/browse/depended/chalk) + ## Install ```sh -npm install chalk +$ npm install --save 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 -import chalk from 'chalk'; +var chalk = require('chalk'); -const log = console.log; +// style a string +console.log( chalk.blue('Hello world!') ); -// Combine styled and normal strings -log(chalk.blue('Hello') + ' World' + chalk.red('!')); +// combine styled and normal strings +console.log( chalk.blue('Hello'), 'World' + chalk.red('!') ); -// Compose multiple styles using the chainable API -log(chalk.blue.bgRed.bold('Hello world!')); +// compose multiple styles using the chainable API +console.log( chalk.blue.bgRed.bold('Hello world!') ); -// Pass in multiple arguments -log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz')); +// pass in multiple arguments +console.log( chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz') ); -// Nest styles -log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!')); +// nest styles +console.log( chalk.red('Hello', chalk.underline.bgBlue('world') + '!') ); -// 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!')); +// nest styles of the same type even (color, underline, background) +console.log( chalk.green('I am a green line ' + chalk.blue('with a blue substring') + ' that becomes green again!') ); ``` -Easily define your own themes: +Easily define your own themes. ```js -import chalk from 'chalk'; - -const error = chalk.bold.red; -const warning = chalk.hex('#FFA500'); // Orange color - +var chalk = require('chalk'); +var error = chalk.bold.red; console.log(error('Error!')); -console.log(warning('Warning!')); ``` -Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args): +Take advantage of console.log [string substitution](http://nodejs.org/docs/latest/api/console.html#console_console_log_data). ```js -import chalk from 'chalk'; - -const name = 'Sindre'; +var name = 'Sindre'; console.log(chalk.green('Hello %s'), name); -//=> 'Hello Sindre' +//=> Hello Sindre ``` + ## API ### chalk.`