diff --git a/.github/funding.yml b/.github/funding.yml index 1cd069f..baa12cf 100644 --- a/.github/funding.yml +++ b/.github/funding.yml @@ -1,4 +1,5 @@ -github: [sindresorhus, Qix-] +github: sindresorhus open_collective: sindresorhus +patreon: sindresorhus tidelift: npm/chalk custom: https://sindresorhus.com/donate 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/.travis.yml b/.travis.yml new file mode 100644 index 0000000..1492647 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,7 @@ +language: node_js +node_js: + - '12' + - '10' + - '8' +after_success: + - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' diff --git a/benchmark.js b/benchmark.js index c5c6e27..dc24696 100644 --- a/benchmark.js +++ b/benchmark.js @@ -1,7 +1,10 @@ -/* globals suite, bench */ -import chalk from './index.js'; +/* globals suite, set, bench */ +'use strict'; +const chalk = require('.'); suite('chalk', () => { + set('iterations', 1000000); + const chalkRed = chalk.red; const chalkBgRed = chalk.bgRed; const chalkBlueBgRed = chalk.blue.bgRed; @@ -44,14 +47,4 @@ suite('chalk', () => { 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`; - }); }); diff --git a/examples/rainbow.js b/examples/rainbow.js index 5b2b1d2..813e026 100644 --- a/examples/rainbow.js +++ b/examples/rainbow.js @@ -1,24 +1,24 @@ -import {setTimeout as delay} from 'node:timers/promises'; -import convertColor from 'color-convert'; -import updateLog from 'log-update'; -import chalk from '../source/index.js'; +'use strict'; +const chalk = require('..'); const ignoreChars = /[^!-~]/g; +const delay = milliseconds => new Promise(resolve => setTimeout(resolve, milliseconds)); + function rainbow(string, offset) { if (!string || string.length === 0) { return string; } - const hueStep = 360 / string.replaceAll(ignoreChars, '').length; + const hueStep = 360 / string.replace(ignoreChars, '').length; let hue = offset % 360; const characters = []; for (const character of string) { - if (ignoreChars.test(character)) { + if (character.match(ignoreChars)) { characters.push(character); } else { - characters.push(chalk.hex(convertColor.hsl.hex(hue, 100, 50))(character)); + characters.push(chalk.hsl(hue, 100, 50)(character)); hue = (hue + hueStep) % 360; } } @@ -27,12 +27,15 @@ function rainbow(string, offset) { } async function animateString(string) { - for (let index = 0; index < 360 * 5; index++) { - updateLog(rainbow(string, index)); + console.log(); + for (let i = 0; i < 360 * 5; i++) { + console.log('\u001B[1F\u001B[G', rainbow(string, i)); await delay(2); // eslint-disable-line no-await-in-loop } } -console.log(); -await animateString('We hope you enjoy Chalk! <3'); -console.log(); +(async () => { + console.log(); + await animateString('We hope you enjoy Chalk! <3'); + console.log(); +})(); diff --git a/examples/screenshot.js b/examples/screenshot.js index 9ad164b..37f5850 100644 --- a/examples/screenshot.js +++ b/examples/screenshot.js @@ -1,27 +1,18 @@ -import process from 'node:process'; -import styles from 'ansi-styles'; -import chalk from '../source/index.js'; +'use strict'; +const styles = require('ansi-styles'); +const chalk = require('..'); // Generates screenshot for (const key of Object.keys(styles)) { - let returnValue = key; + let ret = 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') - ) { + if (key === 'reset' || key === 'hidden' || key === 'grey') { continue; } if (/^bg[^B]/.test(key)) { - returnValue = chalk.black(returnValue); + ret = chalk.black(ret); } - process.stdout.write(chalk[key](returnValue) + ' '); + process.stdout.write(chalk[key](ret) + ' '); } diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..52dd5ae --- /dev/null +++ b/index.d.ts @@ -0,0 +1,384 @@ +declare const enum LevelEnum { + /** + All colors disabled. + */ + None = 0, + + /** + Basic 16 colors support. + */ + Basic = 1, + + /** + ANSI 256 colors support. + */ + Ansi256 = 2, + + /** + Truecolor 16 million colors support. + */ + TrueColor = 3 +} + +/** +Basic foreground colors. + +[More colors here.](https://github.com/chalk/chalk/blob/master/readme.md#256-and-truecolor-color-support) +*/ +declare type ForegroundColor = + | 'black' + | 'red' + | 'green' + | 'yellow' + | 'blue' + | 'magenta' + | 'cyan' + | 'white' + | 'gray' + | 'grey' + | 'blackBright' + | 'redBright' + | 'greenBright' + | 'yellowBright' + | 'blueBright' + | 'magentaBright' + | 'cyanBright' + | 'whiteBright'; + +/** +Basic background colors. + +[More colors here.](https://github.com/chalk/chalk/blob/master/readme.md#256-and-truecolor-color-support) +*/ +declare type BackgroundColor = + | 'bgBlack' + | 'bgRed' + | 'bgGreen' + | 'bgYellow' + | 'bgBlue' + | 'bgMagenta' + | 'bgCyan' + | 'bgWhite' + | 'bgGray' + | 'bgGrey' + | 'bgBlackBright' + | 'bgRedBright' + | 'bgGreenBright' + | 'bgYellowBright' + | 'bgBlueBright' + | 'bgMagentaBright' + | 'bgCyanBright' + | 'bgWhiteBright'; + +/** +Basic colors. + +[More colors here.](https://github.com/chalk/chalk/blob/master/readme.md#256-and-truecolor-color-support) +*/ +declare type Color = ForegroundColor | BackgroundColor; + +declare type Modifiers = + | 'reset' + | 'bold' + | 'dim' + | 'italic' + | 'underline' + | 'inverse' + | 'hidden' + | 'strikethrough' + | 'visible'; + +declare namespace chalk { + type Level = LevelEnum; + + interface Options { + /** + Specify the color support for Chalk. + By default, color support is automatically detected based on the environment. + */ + level?: Level; + } + + interface Instance { + /** + Return a new Chalk instance. + */ + new (options?: Options): Chalk; + } + + /** + Detect whether the terminal supports color. + */ + interface ColorSupport { + /** + The color level used by Chalk. + */ + level: Level; + + /** + Return whether Chalk supports basic 16 colors. + */ + hasBasic: boolean; + + /** + Return whether Chalk supports ANSI 256 colors. + */ + has256: boolean; + + /** + Return whether Chalk supports Truecolor 16 million colors. + */ + has16m: boolean; + } + + interface ChalkFunction { + /** + Use a template string. + + @remarks Template literals are unsupported for nested calls (see [issue #341](https://github.com/chalk/chalk/issues/341)) + + @example + ``` + import chalk = require('chalk'); + + log(chalk` + CPU: {red ${cpu.totalPercent}%} + RAM: {green ${ram.used / ram.total * 100}%} + DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%} + `); + ``` + */ + (text: TemplateStringsArray, ...placeholders: unknown[]): string; + + (...text: unknown[]): string; + } + + interface Chalk extends ChalkFunction { + /** + Return a new Chalk instance. + */ + Instance: Instance; + + /** + The color support for Chalk. + By default, color support is automatically detected based on the environment. + */ + level: Level; + + /** + Use HEX value to set text color. + + @param color - Hexadecimal value representing the desired color. + + @example + ``` + import chalk = require('chalk'); + + chalk.hex('#DEADED'); + ``` + */ + hex(color: string): Chalk; + + /** + Use keyword color value to set text color. + + @param color - Keyword value representing the desired color. + + @example + ``` + import chalk = require('chalk'); + + chalk.keyword('orange'); + ``` + */ + keyword(color: string): Chalk; + + /** + Use RGB values to set text color. + */ + rgb(red: number, green: number, blue: number): Chalk; + + /** + Use HSL values to set text color. + */ + hsl(hue: number, saturation: number, lightness: number): Chalk; + + /** + Use HSV values to set text color. + */ + hsv(hue: number, saturation: number, value: number): Chalk; + + /** + Use HWB values to set text color. + */ + hwb(hue: number, whiteness: number, blackness: number): Chalk; + + /** + Use HEX value to set background color. + + @param color - Hexadecimal value representing the desired color. + + @example + ``` + import chalk = require('chalk'); + + chalk.bgHex('#DEADED'); + ``` + */ + bgHex(color: string): Chalk; + + /** + Use keyword color value to set background color. + + @param color - Keyword value representing the desired color. + + @example + ``` + import chalk = require('chalk'); + + chalk.bgKeyword('orange'); + ``` + */ + bgKeyword(color: string): Chalk; + + /** + Use RGB values to set background color. + */ + bgRgb(red: number, green: number, blue: number): Chalk; + + /** + Use HSL values to set background color. + */ + bgHsl(hue: number, saturation: number, lightness: number): Chalk; + + /** + Use HSV values to set background color. + */ + bgHsv(hue: number, saturation: number, value: number): Chalk; + + /** + Use HWB values to set background color. + */ + bgHwb(hue: number, whiteness: number, blackness: number): Chalk; + + /** + Modifier: Resets the current color chain. + */ + readonly reset: Chalk; + + /** + Modifier: Make text bold. + */ + readonly bold: Chalk; + + /** + Modifier: Emitting only a small amount of light. + */ + readonly dim: Chalk; + + /** + Modifier: Make text italic. (Not widely supported) + */ + readonly italic: Chalk; + + /** + Modifier: Make text underline. (Not widely supported) + */ + readonly underline: Chalk; + + /** + Modifier: Inverse background and foreground colors. + */ + readonly inverse: Chalk; + + /** + Modifier: Prints the text, but makes it invisible. + */ + readonly hidden: Chalk; + + /** + Modifier: Puts a horizontal line through the center of the text. (Not widely supported) + */ + readonly strikethrough: Chalk; + + /** + Modifier: Prints the text only when Chalk has a color support level > 0. + Can be useful for things that are purely cosmetic. + */ + readonly visible: Chalk; + + readonly black: Chalk; + readonly red: Chalk; + readonly green: Chalk; + readonly yellow: Chalk; + readonly blue: Chalk; + readonly magenta: Chalk; + readonly cyan: Chalk; + readonly white: Chalk; + + /* + Alias for `blackBright`. + */ + readonly gray: Chalk; + + /* + Alias for `blackBright`. + */ + readonly grey: Chalk; + + readonly blackBright: Chalk; + readonly redBright: Chalk; + readonly greenBright: Chalk; + readonly yellowBright: Chalk; + readonly blueBright: Chalk; + readonly magentaBright: Chalk; + readonly cyanBright: Chalk; + readonly whiteBright: Chalk; + + readonly bgBlack: Chalk; + readonly bgRed: Chalk; + readonly bgGreen: Chalk; + readonly bgYellow: Chalk; + readonly bgBlue: Chalk; + readonly bgMagenta: Chalk; + readonly bgCyan: Chalk; + readonly bgWhite: Chalk; + + /* + Alias for `bgBlackBright`. + */ + readonly bgGray: Chalk; + + /* + Alias for `bgBlackBright`. + */ + readonly bgGrey: Chalk; + + readonly bgBlackBright: Chalk; + readonly bgRedBright: Chalk; + readonly bgGreenBright: Chalk; + readonly bgYellowBright: Chalk; + readonly bgBlueBright: Chalk; + readonly bgMagentaBright: Chalk; + readonly bgCyanBright: Chalk; + readonly bgWhiteBright: Chalk; + } +} + +/** +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: chalk.Chalk & chalk.ChalkFunction & { + supportsColor: chalk.ColorSupport | false; + Level: typeof LevelEnum; + Color: Color; + ForegroundColor: ForegroundColor; + BackgroundColor: BackgroundColor; + Modifiers: Modifiers; + stderr: chalk.Chalk & {supportsColor: chalk.ColorSupport | false}; +}; + +export = chalk; diff --git a/source/index.test-d.ts b/index.test-d.ts similarity index 64% rename from source/index.test-d.ts rename to index.test-d.ts index 92da39d..bc3d17a 100644 --- a/source/index.test-d.ts +++ b/index.test-d.ts @@ -1,64 +1,60 @@ -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'; +import {expectType, expectError} from 'tsd'; +import chalk = require('.'); + +// - Helpers - +type colorReturn = chalk.Chalk & {supportsColor?: never}; + +// - Level - +expectType(chalk.Level.None); +expectType(chalk.Level.Basic); +expectType(chalk.Level.Ansi256); +expectType(chalk.Level.TrueColor); // - supportsColor - -expectType(supportsColor); -if (supportsColor) { - expectType(supportsColor); - expectType(supportsColor.level); - expectType(supportsColor.hasBasic); - expectType(supportsColor.has256); - expectType(supportsColor.has16m); -} +expectType(chalk.supportsColor); +expectType((chalk.supportsColor as chalk.ColorSupport).hasBasic); +expectType((chalk.supportsColor as chalk.ColorSupport).has256); +expectType((chalk.supportsColor as chalk.ColorSupport).has16m); // - stderr - -expectAssignable(chalkStderr); -expectType(supportsColorStderr); -if (supportsColorStderr) { - expectType(supportsColorStderr.hasBasic); - expectType(supportsColorStderr.has256); - expectType(supportsColorStderr.has16m); -} +expectType(chalk.stderr); +expectType(chalk.stderr.supportsColor); +expectType((chalk.stderr.supportsColor as chalk.ColorSupport).hasBasic); +expectType((chalk.stderr.supportsColor as chalk.ColorSupport).has256); +expectType((chalk.stderr.supportsColor as chalk.ColorSupport).has16m); -// -- `supportsColorStderr` is not a member of the Chalk interface -- -expectError(chalk.reset.supportsColorStderr); +// -- `stderr` is not a member of the Chalk interface -- +expectError(chalk.reset.stderr); // -- `supportsColor` is not a member of the Chalk interface -- expectError(chalk.reset.supportsColor); // - Chalk - // -- Instance -- -expectType(new Chalk({level: 1})); +expectType(new chalk.Instance({level: 1})); // -- Properties -- -expectType(chalk.level); +expectType(chalk.level); + +// -- Template literal -- +expectType(chalk``); +const name = 'John'; +expectType(chalk`Hello {bold.red ${name}}`); +expectType(chalk`Works with numbers {bold.red ${1}}`); // -- Color methods -- -expectType(chalk.rgb(0, 0, 0)); -expectType(chalk.hex('#DEADED')); -expectType(chalk.ansi256(0)); -expectType(chalk.bgRgb(0, 0, 0)); -expectType(chalk.bgHex('#DEADED')); -expectType(chalk.bgAnsi256(0)); +expectType(chalk.hex('#DEADED')); +expectType(chalk.keyword('orange')); +expectType(chalk.rgb(0, 0, 0)); +expectType(chalk.hsl(0, 0, 0)); +expectType(chalk.hsv(0, 0, 0)); +expectType(chalk.hwb(0, 0, 0)); +expectType(chalk.bgHex('#DEADED')); +expectType(chalk.bgKeyword('orange')); +expectType(chalk.bgRgb(0, 0, 0)); +expectType(chalk.bgHsl(0, 0, 0)); +expectType(chalk.bgHsv(0, 0, 0)); +expectType(chalk.bgHwb(0, 0, 0)); // -- Modifiers -- expectType(chalk.reset('foo')); @@ -66,7 +62,6 @@ expectType(chalk.bold('foo')); expectType(chalk.dim('foo')); expectType(chalk.italic('foo')); expectType(chalk.underline('foo')); -expectType(chalk.overline('foo')); expectType(chalk.inverse('foo')); expectType(chalk.hidden('foo')); expectType(chalk.strikethrough('foo')); @@ -155,25 +150,6 @@ expectType(chalk.bgWhiteBright`foo`); expectType(chalk.red.bgGreen.underline('foo')); expectType(chalk.underline.red.bgGreen('foo')); -// -- Complex template literal -- -expectType(chalk.underline``); -expectType(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`); -expectType(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`); - -// -- Modifiers types -expectAssignable('strikethrough'); -expectError('delete'); - -// -- Foreground types -expectAssignable('red'); -expectError('pink'); - -// -- Background types -expectAssignable('bgRed'); -expectError('bgPink'); - -// -- Color types -- -expectAssignable('red'); -expectAssignable('bgRed'); -expectError('hotpink'); -expectError('bgHotpink'); +// -- Color types == +expectType('red'); +expectError('hotpink'); diff --git a/license b/license index fa7ceba..e7af2f7 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -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: 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..32ea3bd 100644 --- a/package.json +++ b/package.json @@ -1,32 +1,20 @@ { "name": "chalk", - "version": "5.6.2", + "version": "3.0.0-beta.1", "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, + "main": "source", "engines": { - "node": "^12.17.0 || ^14.13 || >=16.0.0" + "node": ">=8" }, "scripts": { - "test": "xo && c8 ava && tsd", + "test": "xo && nyc ava && tsd", "bench": "matcha benchmark.js" }, "files": [ "source", - "!source/index.test-d.ts" + "index.d.ts" ], "keywords": [ "color", @@ -36,6 +24,7 @@ "console", "cli", "string", + "str", "ansi", "style", "styles", @@ -50,34 +39,25 @@ "command-line", "text" ], + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, "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", + "ava": "^2.4.0", + "coveralls": "^3.0.5", + "execa": "^2.0.3", + "import-fresh": "^3.1.0", "matcha": "^0.7.0", - "tsd": "^0.19.0", - "xo": "^0.57.0", - "yoctodelay": "^2.0.0" + "nyc": "^14.1.1", + "resolve-from": "^5.0.0", + "tsd": "^0.7.4", + "xo": "^0.25.3" }, "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" + "unicorn/prefer-includes": "off" } - }, - "c8": { - "reporter": [ - "text", - "lcov" - ], - "exclude": [ - "source/vendor" - ] } } diff --git a/readme.md b/readme.md index ce1f3f3..4a3b646 100644 --- a/readme.md +++ b/readme.md @@ -9,42 +9,37 @@ > 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/chalk/chalk.svg?branch=master)](https://travis-ci.org/chalk/chalk) [![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=master)](https://coveralls.io/github/chalk/chalk?branch=master) [![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) [![](https://img.shields.io/badge/unicorn-approved-ff69b4.svg)](https://www.youtube.com/watch?v=9auOCbH5Ns4) [![XO code style](https://img.shields.io/badge/code_style-XO-5ed9c7.svg)](https://github.com/xojs/xo) ![TypeScript-ready](https://img.shields.io/npm/types/chalk.svg) -![](media/screenshot.png) + -## Info +**This readme reflects the next major version that is currently in development. You probably want [the v2 readme](https://www.npmjs.com/package/chalk).** -- [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 - Expressive API - Highly performant -- No dependencies - 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 - Actively maintained -- [Used by ~115,000 packages](https://www.npmjs.com/browse/depended/chalk) as of July 4, 2024 +- [Used by ~46,000 packages](https://www.npmjs.com/browse/depended/chalk) as of October 1, 2019 + ## Install -```sh -npm install chalk +```console +$ 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'; +const chalk = require('chalk'); console.log(chalk.blue('Hello world!')); ``` @@ -52,8 +47,7 @@ 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'; - +const chalk = require('chalk'); const log = console.log; // Combine styled and normal strings @@ -82,7 +76,15 @@ RAM: ${chalk.green('40%')} DISK: ${chalk.yellow('70%')} `); +// ES2015 tagged template literal +log(chalk` +CPU: {red ${cpu.totalPercent}%} +RAM: {green ${ram.used / ram.total * 100}%} +DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%} +`); + // Use RGB colors in terminal emulators that support it. +log(chalk.keyword('orange')('Yay for orange colored text!')); log(chalk.rgb(123, 45, 67).underline('Underlined reddish color')); log(chalk.hex('#DEADED').bold('Bold gray!')); ``` @@ -90,10 +92,10 @@ log(chalk.hex('#DEADED').bold('Bold gray!')); Easily define your own themes: ```js -import chalk from 'chalk'; +const chalk = require('chalk'); const error = chalk.bold.red; -const warning = chalk.hex('#FFA500'); // Orange color +const warning = chalk.keyword('orange'); console.log(error('Error!')); console.log(warning('Warning!')); @@ -102,13 +104,12 @@ console.log(warning('Warning!')); Take advantage of console.log [string substitution](https://nodejs.org/docs/latest/api/console.html#console_console_log_data_args): ```js -import chalk from 'chalk'; - const name = 'Sindre'; console.log(chalk.green('Hello %s'), name); //=> 'Hello Sindre' ``` + ## API ### chalk.`