介绍
安装
$ npm install chalk
使用
基本用例
const chalk = require('chalk');
console.log(chalk.blue('hello world!'));
const chalk = require('chalk');
const log = console.log;
log(chalk.blue('Hello') + ' World' + chalk.red('!'));
log(chalk.blue.bgRed.bold('Hello world!'));
log(chalk.blue('Hello', 'World!', 'Foo', 'bar', 'biz', 'baz'));
log(chalk.red('Hello', chalk.underline.bgBlue('world') + '!'));
log(chalk.green(
'I am a green line ' +
chalk.blue.underline.bold('with a blue substring') +
' that becomes green again!'
));
log(`
CPU: ${chalk.red('90%')}
RAM: ${chalk.green('40%')}
DISK: ${chalk.yellow('70%')}
`);
log(chalk`
CPU: {red ${cpu.totalPercent}%}
RAM: {green ${ram.used / ram.total * 100}%}
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
`);
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!'));
const chalk = require('chalk');
const error = chalk.bold.red;
const warning = chalk.keyword('orange');
console.log(error('Error!'));
console.log(warning('Warning!'));
const name = 'Sindre';
console.log(chalk.green('Hello %s'), name);
API
chalk.<style>[.<style>...](string, [string...])
chalk.red.bold.underline('Hello', 'world');
chalk.level
const ctx = new chalk.Instance({level: 0});
| Level | Description |
|---|
| 0 | All colors disabled |
| 1 | Basic color support (16 colors) |
| 2 | 256 color support |
| 3 | Truecolor support (16 million colors) |
chalk.supportsColor
- --color and --no-color
- --color=256 and --color=16m
chalk.stderr and chalk.stderr.supportsColor
Styles
Modifiers
- reset - Resets the current color chain.
- bold - Make text bold.
- dim - Emitting only a small amount of light.
- italic - Make text italic. (Not widely supported)
- underline - Make text underline. (Not widely supported)
- inverse- Inverse background and foreground colors.
- hidden - Prints the text, but makes it invisible.
- strikethrough - Puts a horizontal line through the center of the text. (Not widely supported)
- visible- Prints the text only when Chalk has a color level > 0. Can be useful for things that are purely cosmetic.
colors
- black
- red
- green
- yellow
- blue
- magenta
- cyan
- white
- blackBright (alias: gray, grey)
- redBright
- greenBright
- yellowBright
- blueBright
- magentaBright
- cyanBright
- whiteBright
Background colors
- bgBlack
- bgRed
- bgGreen
- bgYellow
- bgBlue
- bgMagenta
- bgCyan
- bgWhite
- bgBlackBright (alias: bgGray, bgGrey)
- bgRedBright
- bgGreenBright
- bgYellowBright
- bgBlueBright
- bgMagentaBright
- bgCyanBright
- bgWhiteBright
Tagged template literal
const chalk = require('chalk');
const miles = 18;
const calculateFeet = miles => miles * 5280;
console.log(chalk`
There are {bold 5280 feet} in a mile.
In {bold ${miles} miles}, there are {green.bold ${calculateFeet(miles)} feet}.
`);
console.log(chalk.bold.rgb(10, 100, 200)('Hello!'));
console.log(chalk.bold.rgb(10, 100, 200)`Hello!`);
console.log(chalk`{bold.rgb(10,100,200) Hello!}`);
256 and Truecolor color support
chalk.hex('#DEADED').underline('Hello, world!')
chalk.keyword('orange')('Some orange text')
chalk.rgb(15, 100, 204).inverse('Hello!')
chalk.bgHex('#DEADED').underline('Hello, world!')
chalk.bgKeyword('orange')('Some orange text')
chalk.bgRgb(15, 100, 204).inverse('Hello!')
- rgb - Example: chalk.rgb(255, 136, 0).bold('Orange!')
- hex - Example: chalk.hex('#FF8800').bold('Orange!')
keyword (CSS keywords) - Example: chalk.keyword('orange').bold('Orange!')
- hsl - Example: chalk.hsl(32, 100, 50).bold('Orange!')
- hsv - Example: chalk.hsv(32, 100, 100).bold('Orange!')
- hwb - Example: chalk.hwb(32, 0, 50).bold('Orange!')
- ansi - Example: chalk.ansi(31).bgAnsi(93)('red on yellowBright')
- ansi256 - Example: chalk.bgAnsi256(194)('Honeydew, more or less')
related
- chalk-cli - CLI for this module
- ansi-styles - ANSI escape codes for styling strings in the terminal
- supports-color - Detect whether a terminal supports color
- strip-ansi - Strip ANSI escape codes
- strip-ansi-stream - Strip ANSI escape codes from a stream
- has-ansi - Check if a string has ANSI escape codes
- ansi-regex - Regular expression for matching ANSI escape codes
- wrap-ansi - Wordwrap a string with ANSI escape codes
- slice-ansi - Slice a string with ANSI escape codes
- color-convert - Converts colors between different models
- chalk-animation - Animate strings in the terminal
- gradient-string - Apply color gradients to strings
- chalk-pipe - Create chalk style schemes with simpler style strings
- terminal-link - Create clickable links in the terminal