Skip to content

Commit c427028

Browse files
Expose style names (#82)
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 6e5de6e commit c427028

File tree

4 files changed

+148
-62
lines changed

4 files changed

+148
-62
lines changed

index.d.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,52 @@ export interface ConvertColor {
180180
hexToAnsi(hex: string): number;
181181
}
182182

183+
/**
184+
Basic modifier names.
185+
*/
186+
export type ModifierName = keyof Modifier;
187+
188+
/**
189+
Basic foreground color names.
190+
191+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
192+
*/
193+
export type ForegroundColorName = keyof ForegroundColor;
194+
195+
/**
196+
Basic background color names.
197+
198+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
199+
*/
200+
export type BackgroundColorName = keyof BackgroundColor;
201+
202+
/**
203+
Basic color names. The combination of foreground and background color names.
204+
205+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
206+
*/
207+
export type ColorName = ForegroundColorName | BackgroundColorName;
208+
209+
/**
210+
Basic modifier names.
211+
*/
212+
export const modifierNames: readonly ModifierName[];
213+
214+
/**
215+
Basic foreground color names.
216+
*/
217+
export const foregroundColorNames: readonly ForegroundColorName[];
218+
219+
/**
220+
Basic background color names.
221+
*/
222+
export const backgroundColorNames: readonly BackgroundColorName[];
223+
224+
/*
225+
Basic color names. The combination of foreground and background color names.
226+
*/
227+
export const colorNames: readonly ColorName[];
228+
183229
declare const ansiStyles: {
184230
readonly modifier: Modifier;
185231
readonly color: ColorBase & ForegroundColor;

index.js

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,68 +6,67 @@ const wrapAnsi256 = (offset = 0) => code => `\u001B[${38 + offset};5;${code}m`;
66

77
const wrapAnsi16m = (offset = 0) => (red, green, blue) => `\u001B[${38 + offset};2;${red};${green};${blue}m`;
88

9+
const styles = {
10+
modifier: {
11+
reset: [0, 0],
12+
// 21 isn't widely supported and 22 does the same thing
13+
bold: [1, 22],
14+
dim: [2, 22],
15+
italic: [3, 23],
16+
underline: [4, 24],
17+
overline: [53, 55],
18+
inverse: [7, 27],
19+
hidden: [8, 28],
20+
strikethrough: [9, 29],
21+
},
22+
color: {
23+
black: [30, 39],
24+
red: [31, 39],
25+
green: [32, 39],
26+
yellow: [33, 39],
27+
blue: [34, 39],
28+
magenta: [35, 39],
29+
cyan: [36, 39],
30+
white: [37, 39],
31+
32+
// Bright color
33+
blackBright: [90, 39],
34+
gray: [90, 39], // Alias of `blackBright`
35+
grey: [90, 39], // Alias of `blackBright`
36+
redBright: [91, 39],
37+
greenBright: [92, 39],
38+
yellowBright: [93, 39],
39+
blueBright: [94, 39],
40+
magentaBright: [95, 39],
41+
cyanBright: [96, 39],
42+
whiteBright: [97, 39],
43+
},
44+
bgColor: {
45+
bgBlack: [40, 49],
46+
bgRed: [41, 49],
47+
bgGreen: [42, 49],
48+
bgYellow: [43, 49],
49+
bgBlue: [44, 49],
50+
bgMagenta: [45, 49],
51+
bgCyan: [46, 49],
52+
bgWhite: [47, 49],
53+
54+
// Bright color
55+
bgBlackBright: [100, 49],
56+
bgGray: [100, 49], // Alias of `bgBlackBright`
57+
bgGrey: [100, 49], // Alias of `bgBlackBright`
58+
bgRedBright: [101, 49],
59+
bgGreenBright: [102, 49],
60+
bgYellowBright: [103, 49],
61+
bgBlueBright: [104, 49],
62+
bgMagentaBright: [105, 49],
63+
bgCyanBright: [106, 49],
64+
bgWhiteBright: [107, 49],
65+
},
66+
};
67+
968
function assembleStyles() {
1069
const codes = new Map();
11-
const styles = {
12-
modifier: {
13-
reset: [0, 0],
14-
// 21 isn't widely supported and 22 does the same thing
15-
bold: [1, 22],
16-
dim: [2, 22],
17-
italic: [3, 23],
18-
underline: [4, 24],
19-
overline: [53, 55],
20-
inverse: [7, 27],
21-
hidden: [8, 28],
22-
strikethrough: [9, 29],
23-
},
24-
color: {
25-
black: [30, 39],
26-
red: [31, 39],
27-
green: [32, 39],
28-
yellow: [33, 39],
29-
blue: [34, 39],
30-
magenta: [35, 39],
31-
cyan: [36, 39],
32-
white: [37, 39],
33-
34-
// Bright color
35-
blackBright: [90, 39],
36-
redBright: [91, 39],
37-
greenBright: [92, 39],
38-
yellowBright: [93, 39],
39-
blueBright: [94, 39],
40-
magentaBright: [95, 39],
41-
cyanBright: [96, 39],
42-
whiteBright: [97, 39],
43-
},
44-
bgColor: {
45-
bgBlack: [40, 49],
46-
bgRed: [41, 49],
47-
bgGreen: [42, 49],
48-
bgYellow: [43, 49],
49-
bgBlue: [44, 49],
50-
bgMagenta: [45, 49],
51-
bgCyan: [46, 49],
52-
bgWhite: [47, 49],
53-
54-
// Bright color
55-
bgBlackBright: [100, 49],
56-
bgRedBright: [101, 49],
57-
bgGreenBright: [102, 49],
58-
bgYellowBright: [103, 49],
59-
bgBlueBright: [104, 49],
60-
bgMagentaBright: [105, 49],
61-
bgCyanBright: [106, 49],
62-
bgWhiteBright: [107, 49],
63-
},
64-
};
65-
66-
// Alias bright black as gray (and grey)
67-
styles.color.gray = styles.color.blackBright;
68-
styles.bgColor.bgGray = styles.bgColor.bgBlackBright;
69-
styles.color.grey = styles.color.blackBright;
70-
styles.bgColor.bgGrey = styles.bgColor.bgBlackBright;
7170

7271
for (const [groupName, group] of Object.entries(styles)) {
7372
for (const [styleName, style] of Object.entries(group)) {
@@ -217,3 +216,8 @@ function assembleStyles() {
217216
const ansiStyles = assembleStyles();
218217

219218
export default ansiStyles;
219+
220+
export const modifierNames = Object.keys(styles.modifier);
221+
export const foregroundColorNames = Object.keys(styles.color);
222+
export const backgroundColorNames = Object.keys(styles.bgColor);
223+
export const colorNames = [...foregroundColorNames, ...backgroundColorNames];

index.test-d.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {expectType} from 'tsd';
2-
import ansiStyles, {CSPair} from './index.js';
1+
import {expectAssignable, expectError, expectType} from 'tsd';
2+
import ansiStyles, {CSPair, ModifierName, ForegroundColorName, BackgroundColorName, ColorName} from './index.js';
33

44
expectType<ReadonlyMap<number, number>>(ansiStyles.codes);
55

@@ -118,3 +118,21 @@ expectType<CSPair>(ansiStyles.italic);
118118
expectType<CSPair>(ansiStyles.reset);
119119
expectType<CSPair>(ansiStyles.strikethrough);
120120
expectType<CSPair>(ansiStyles.underline);
121+
122+
// --- ModifierName ---
123+
expectAssignable<ModifierName>('strikethrough');
124+
expectError<ModifierName>('delete');
125+
126+
// --- ForegroundColorName ---
127+
expectAssignable<ForegroundColorName>('blue');
128+
expectError<ForegroundColorName>('pink');
129+
130+
// --- ForegroundColorName ---
131+
expectAssignable<BackgroundColorName>('bgBlue');
132+
expectError<BackgroundColorName>('bgPink');
133+
134+
// --- ColorName ---
135+
expectAssignable<ColorName>('blue');
136+
expectAssignable<ColorName>('bgBlue');
137+
expectError<ColorName>('pink');
138+
expectError<ColorName>('bgPink');

readme.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,26 @@ console.log(`${styles.color.ansi16m(...styles.hexToRgb('#abcdef'))}Hello World${
3232

3333
## API
3434

35+
### `open` and `close`
36+
3537
Each style has an `open` and `close` property.
3638

39+
### `modifierNames`, `foregroundColorNames`, `backgroundColorNames`, and `colorNames`
40+
41+
All supported style strings are exposed as an array of strings for convenience. `colorNames` is the combination of `foregroundColorNames` and `backgroundColorNames`.
42+
43+
This can be useful if you need to validate input:
44+
45+
```js
46+
import {modifierNames, foregroundColorNames} from 'ansi-styles';
47+
48+
console.log(modifierNames.includes('bold'));
49+
//=> true
50+
51+
console.log(foregroundColorNames.includes('pink'));
52+
//=> false
53+
```
54+
3755
## Styles
3856

3957
### Modifiers

0 commit comments

Comments
 (0)