ansi-styles
ANSI转义码 用于对终端中的字符串进行样式化
你可能想要更高级的 粉笔 模块,用于对字符串进行样式化。

安装
npm install ansi-styles
用法
import styles from 'ansi-styles';
console.log(`${styles.green.open}Hello world!${styles.green.close}`);
// Color conversion between 256/truecolor
// NOTE: When converting from truecolor to 256 colors, the original color
// may be degraded to fit the new color palette. This means terminals
// that do not support 16 million colors will best-match the
// original color.
console.log(`${styles.color.ansi(styles.rgbToAnsi(199, 20, 250))}Hello World${styles.color.close}`)
console.log(`${styles.color.ansi256(styles.rgbToAnsi256(199, 20, 250))}Hello World${styles.color.close}`)
console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${styles.color.close}`)
API
open 和 close
每种风格都有一个 open 和 close 财产。
modifierNames, foregroundColorNames, backgroundColorNames, underlineColorNames, colorNames
为方便起见,所有支持的样式字符串都公开为字符串数组。 colorNames 是 foregroundColorNames 和 backgroundColorNames. 中的下划线颜色名称保持分开 underlineColorNames。
如果你需要验证输入,这可能很有用:
import {modifierNames, foregroundColorNames} from 'ansi-styles';
console.log(modifierNames.includes('bold'));
//=> true
console.log(foregroundColorNames.includes('pink'));
//=> false
风格
修饰符
resetbolddimitalic(不被广泛支持)underline-在文本下面画一条水平线。 (不被广泛支持)underlineDouble-在文本下面放一条双水平线。 (不被广泛支持)underlineCurly-在文本下方放置一条卷曲的水平线。 (不被广泛支持)underlineDotted-在文本下面画一条虚线。 (不被广泛支持)underlineDashed-在文本下面放一条虚线。 (不被广泛支持)overline支持vte终端、GNOME终端、mintty、Git Bash。inversehiddenstrikethrough(不被广泛支持)
颜色
blackredgreenyellowbluemagentacyanwhiteblackBright(别名:gray,grey)redBrightgreenBrightyellowBrightblueBrightmagentaBrightcyanBrightwhiteBright
背景颜色
bgBlackbgRedbgGreenbgYellowbgBluebgMagentabgCyanbgWhitebgBlackBright(别名:bgGray,bgGrey)bgRedBrightbgGreenBrightbgYellowBrightbgBlueBrightbgMagentaBrightbgCyanBrightbgWhiteBright
下划线颜色
下划线颜色的设置独立于文本颜色,因此只有在应用下划线样式时,颜色才可见。 (不被广泛支持)
与文本和背景颜色不同,下划线颜色没有基本的16色形式。命名下划线颜色和 underlineColor.ansi() 使用256色调色板中的前16个条目。 underlineColor.ansi256() 发出256色的逃逸光,并且 underlineColor.ansi16m() 发出真彩色的光。
underlineBlackunderlineRedunderlineGreenunderlineYellowunderlineBlueunderlineMagentaunderlineCyanunderlineWhiteunderlineBlackBright(别名:underlineGray,underlineGrey)underlineRedBrightunderlineGreenBrightunderlineYellowBrightunderlineBlueBrightunderlineMagentaBrightunderlineCyanBrightunderlineWhiteBright
高级用法
默认情况下,您将获得一个样式映射,但是样式也可以作为组使用。它们是不可枚举的,所以除非显式地访问它们,否则它们不会显示出来。这使得在更高级的模块中只公开一个子集变得更容易。
styles.modifierstyles.colorstyles.bgColorstyles.underlineColor
样式组示例
import styles from 'ansi-styles';
console.log(styles.color.green.open);
各样式的主要SGR参数见下 styles.codes,它返回 Map 打开代码作为键,关闭代码作为值。参数化的样式,例如 4:2 和 58;5;0 都是由它们的首要参数决定的, 4 和 58 分别。
样式代码示例
import styles from 'ansi-styles';
console.log(styles.codes.get(36));
//=> 39
16 / 256 / 1600万(TrueColor)支持
ansi-styles 允许在各种颜色格式和ANSI转义之间进行转换,支持16,256和 1600万种颜色。
支持以下颜色空间:
rgbhexansi256ansi
要使用这些,使用预期的输出调用相关的转换函数,例如:
import styles from 'ansi-styles';
styles.color.ansi(styles.rgbToAnsi(100, 200, 15)); // RGB to 16 color ansi foreground code
styles.bgColor.ansi(styles.hexToAnsi('#C0FFEE')); // HEX to 16 color ansi foreground code
styles.color.ansi256(styles.rgbToAnsi256(100, 200, 15)); // RGB to 256 color ansi foreground code
styles.bgColor.ansi256(styles.hexToAnsi256('#C0FFEE')); // HEX to 256 color ansi foreground code
styles.underlineColor.ansi(styles.rgbToAnsi(100, 200, 15)); // RGB to underline code using the first 16 palette entries
styles.color.ansi16m(100, 200, 15); // RGB to 16 million color foreground code
styles.bgColor.ansi16m(...styles.hexToRgb('#C0FFEE')); // Hex (RGB) to 16 million color foreground code
styles.underlineColor.ansi256(styles.hexToAnsi256('#C0FFEE')); // HEX to 256 color underline code
styles.underlineColor.ansi16m(...styles.hexToRgb('#C0FFEE')); // Hex (RGB) to 16 million color underline code
相关
- ansi-escapes -用于操作终端的ANSI转义码