diff --git a/readme.md b/readme.md index 6d92165..78f69c9 100644 --- a/readme.md +++ b/readme.md @@ -20,6 +20,10 @@ --- +[English](./readme.md) | [中文文档](./readme_zh.md) + +--- +

diff --git a/readme_zh.md b/readme_zh.md new file mode 100644 index 0000000..af040c3 --- /dev/null +++ b/readme_zh.md @@ -0,0 +1,347 @@ +

+
+
+ Chalk +
+
+
+

+ +> 终端字符串的正确样式 + +[![Coverage Status](https://coveralls.io/repos/github/chalk/chalk/badge.svg?branch=main)](https://coveralls.io/github/chalk/chalk?branch=main) +[![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) +[![run on repl.it](https://repl.it/badge/github/chalk/chalk)](https://repl.it/github/chalk/chalk) +[![Support Chalk on DEV](https://badge.devprotocol.xyz/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15/descriptive)](https://stakes.social/0x44d871aebF0126Bf646753E2C976Aa7e68A66c15) + + + +
+ +--- + +[English](./readme.md) | [中文文档](./readme_zh.md) + +--- + +
+

+

+ + Sindre Sorhus的开源工作得到了GitHub SponsorsDev的支持 + +

+ 特别鸣谢: +
+
+ + + +
+
+ + + +
+
+ +
+ Doppler +
+ All your environment variables, in one place +
+ Stop struggling with scattered API keys, hacking together home-brewed tools, +
+ and avoiding access controls. Keep your team and servers in sync with Doppler. +
+
+

+
+ +--- + +
+ +## 重点介绍 + +- 富有表现力的 API +- 高性能 +- 能够嵌套样式 +- [支持 256/Truecolor 颜色](#256-and-truecolor-color-support) +- 自动检测颜色支持 +- 不扩展`String.prototype` +- 干净而专注 +- 积极维护 +- 截至 2020 年 1 月 1 日,[约有 50,000 个软件包使用](https://www.npmjs.com/browse/depended/chalk) + +## 安装 + +```console +$ npm install chalk +``` + +## 使用方法 + +```js +import chalk from "chalk"; + +console.log(chalk.blue("Hello world!")); +``` + +Chalk 有一个易于使用的可组合的 API,你只需将你想要的样式连锁和嵌套。 + +```js +import chalk from "chalk"; + +const log = console.log; + +// 组合风格化字符串和普通字符串 +log(chalk.blue("Hello") + " World" + chalk.red("!")); + +// 使用可连锁的API构成多种样式 +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!" + ) +); + +// ES2015 字符串模板 +log(` +CPU: ${chalk.red("90%")} +RAM: ${chalk.green("40%")} +DISK: ${chalk.yellow("70%")} +`); + +// ES2015 标签化字符串模板 +log(chalk` +CPU: {red ${cpu.totalPercent}%} +RAM: {green ${(ram.used / ram.total) * 100}%} +DISK: {rgb(255,131,0) ${(disk.used / disk.total) * 100}%} +`); + +// 在支持RGB颜色的终端中使用RGB颜色 +log(chalk.rgb(123, 45, 67).underline("Underlined reddish color")); +log(chalk.hex("#DEADED").bold("Bold gray!")); +``` + +轻松地定义你自己的主题: + +```js +import chalk from "chalk"; + +const error = chalk.bold.red; +const warning = chalk.hex("#FFA500"); // Orange color + +console.log(error("Error!")); +console.log(warning("Warning!")); +``` + +利用 console.log[字符串替换](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.`