UNPKG

4.91 kBMarkdownView Raw
1# ansi-styles
2
3> [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) for styling strings in the terminal
4
5You probably want the higher-level [chalk](https://github.com/chalk/chalk) module for styling your strings.
6
7![](screenshot.png)
8
9## Install
10
11```sh
12npm install ansi-styles
13```
14
15## Usage
16
17```js
18import styles from 'ansi-styles';
19
20console.log(`${styles.green.open}Hello world!${styles.green.close}`);
21
22
23// Color conversion between 256/truecolor
24// NOTE: When converting from truecolor to 256 colors, the original color
25// may be degraded to fit the new color palette. This means terminals
26// that do not support 16 million colors will best-match the
27// original color.
28console.log(`${styles.color.ansi(styles.rgbToAnsi(199, 20, 250))}Hello World${styles.color.close}`)
29console.log(`${styles.color.ansi256(styles.rgbToAnsi256(199, 20, 250))}Hello World${styles.color.close}`)
30console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${styles.color.close}`)
31```
32
33## API
34
35### `open` and `close`
36
37Each style has an `open` and `close` property.
38
39### `modifierNames`, `foregroundColorNames`, `backgroundColorNames`, and `colorNames`
40
41All supported style strings are exposed as an array of strings for convenience. `colorNames` is the combination of `foregroundColorNames` and `backgroundColorNames`.
42
43This can be useful if you need to validate input:
44
45```js
46import {modifierNames, foregroundColorNames} from 'ansi-styles';
47
48console.log(modifierNames.includes('bold'));
49//=> true
50
51console.log(foregroundColorNames.includes('pink'));
52//=> false
53```
54
55## Styles
56
57### Modifiers
58
59- `reset`
60- `bold`
61- `dim`
62- `italic` *(Not widely supported)*
63- `underline`
64- `overline` *Supported on VTE-based terminals, the GNOME terminal, mintty, and Git Bash.*
65- `inverse`
66- `hidden`
67- `strikethrough` *(Not widely supported)*
68
69### Colors
70
71- `black`
72- `red`
73- `green`
74- `yellow`
75- `blue`
76- `magenta`
77- `cyan`
78- `white`
79- `blackBright` (alias: `gray`, `grey`)
80- `redBright`
81- `greenBright`
82- `yellowBright`
83- `blueBright`
84- `magentaBright`
85- `cyanBright`
86- `whiteBright`
87
88### Background colors
89
90- `bgBlack`
91- `bgRed`
92- `bgGreen`
93- `bgYellow`
94- `bgBlue`
95- `bgMagenta`
96- `bgCyan`
97- `bgWhite`
98- `bgBlackBright` (alias: `bgGray`, `bgGrey`)
99- `bgRedBright`
100- `bgGreenBright`
101- `bgYellowBright`
102- `bgBlueBright`
103- `bgMagentaBright`
104- `bgCyanBright`
105- `bgWhiteBright`
106
107## Advanced usage
108
109By default, you get a map of styles, but the styles are also available as groups. They are non-enumerable so they don't show up unless you access them explicitly. This makes it easier to expose only a subset in a higher-level module.
110
111- `styles.modifier`
112- `styles.color`
113- `styles.bgColor`
114
115###### Example
116
117```js
118import styles from 'ansi-styles';
119
120console.log(styles.color.green.open);
121```
122
123Raw escape codes (i.e. without the CSI escape prefix `\u001B[` and render mode postfix `m`) are available under `styles.codes`, which returns a `Map` with the open codes as keys and close codes as values.
124
125###### Example
126
127```js
128import styles from 'ansi-styles';
129
130console.log(styles.codes.get(36));
131//=> 39
132```
133
134## 16 / 256 / 16 million (TrueColor) support
135
136`ansi-styles` allows converting between various color formats and ANSI escapes, with support for 16, 256 and [16 million colors](https://gist.github.com/XVilka/8346728).
137
138The following color spaces are supported:
139
140- `rgb`
141- `hex`
142- `ansi256`
143- `ansi`
144
145To use these, call the associated conversion function with the intended output, for example:
146
147```js
148import styles from 'ansi-styles';
149
150styles.color.ansi(styles.rgbToAnsi(100, 200, 15)); // RGB to 16 color ansi foreground code
151styles.bgColor.ansi(styles.hexToAnsi('#C0FFEE')); // HEX to 16 color ansi foreground code
152
153styles.color.ansi256(styles.rgbToAnsi256(100, 200, 15)); // RGB to 256 color ansi foreground code
154styles.bgColor.ansi256(styles.hexToAnsi256('#C0FFEE')); // HEX to 256 color ansi foreground code
155
156styles.color.ansi16m(100, 200, 15); // RGB to 16 million color foreground code
157styles.bgColor.ansi16m(...styles.hexToRgb('#C0FFEE')); // Hex (RGB) to 16 million color foreground code
158```
159
160## Related
161
162- [ansi-escapes](https://github.com/sindresorhus/ansi-escapes) - ANSI escape codes for manipulating the terminal
163
164## Maintainers
165
166- [Sindre Sorhus](https://github.com/sindresorhus)
167- [Josh Junon](https://github.com/qix-)
168
169## For enterprise
170
171Available as part of the Tidelift Subscription.
172
173The maintainers of `ansi-styles` and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. [Learn more.](https://tidelift.com/subscription/pkg/npm-ansi-styles?utm_source=npm-ansi-styles&utm_medium=referral&utm_campaign=enterprise&utm_term=repo)