Skip to content

Commit 6e0df05

Browse files
authoredOct 12, 2022
Export styles from ansi-styles (#567)
1 parent 92c55db commit 6e0df05

File tree

6 files changed

+219
-115
lines changed

6 files changed

+219
-115
lines changed
 

‎readme.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,19 @@ Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=
210210

211211
`chalkStderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `supportsColor` apply to this too. `supportsColorStderr` is exposed for convenience.
212212

213-
### modifiers, foregroundColors, backgroundColors, and colors
213+
### modifierNames, foregroundColorNames, backgroundColorNames, and colorNames
214214

215-
All supported style strings are exposed as an array of strings for convenience. `colors` is the combination of `foregroundColors` and `backgroundColors`.
215+
All supported style strings are exposed as an array of strings for convenience. `colorNames` is the combination of `foregroundColorNames` and `backgroundColorNames`.
216216

217217
This can be useful if you wrap Chalk and need to validate input:
218218

219219
```js
220-
import {modifiers, foregroundColors} from 'chalk';
220+
import {modifierNames, foregroundColorNames} from 'chalk';
221221

222-
console.log(modifiers.includes('bold'));
222+
console.log(modifierNames.includes('bold'));
223223
//=> true
224224

225-
console.log(foregroundColors.includes('pink'));
225+
console.log(foregroundColorNames.includes('pink'));
226226
//=> false
227227
```
228228

‎source/index.d.ts

+66-38
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,9 @@
11
// TODO: Make it this when TS suports that.
2+
// import {ModifierName, ForegroundColor, BackgroundColor, ColorName} from '#ansi-styles';
23
// import {ColorInfo, ColorSupportLevel} from '#supports-color';
4+
import {ModifierName, ForegroundColorName, BackgroundColorName, ColorName} from './vendor/ansi-styles/index.js';
35
import {ColorInfo, ColorSupportLevel} from './vendor/supports-color/index.js';
46

5-
type BasicColor = 'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white';
6-
type BrightColor = `${BasicColor}Bright`;
7-
type Grey = 'gray' | 'grey';
8-
9-
/**
10-
Basic foreground colors.
11-
12-
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
13-
*/
14-
15-
export type ForegroundColor = BasicColor | BrightColor | Grey;
16-
17-
/**
18-
Basic background colors.
19-
20-
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
21-
*/
22-
export type BackgroundColor = `bg${Capitalize<ForegroundColor>}`;
23-
24-
/**
25-
Basic colors.
26-
27-
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
28-
*/
29-
export type Color = ForegroundColor | BackgroundColor;
30-
31-
export type Modifiers =
32-
| 'reset'
33-
| 'bold'
34-
| 'dim'
35-
| 'italic'
36-
| 'underline'
37-
| 'overline'
38-
| 'inverse'
39-
| 'hidden'
40-
| 'strikethrough'
41-
| 'visible';
42-
437
export interface Options {
448
/**
459
Specify the color support for Chalk.
@@ -277,16 +241,80 @@ export const supportsColor: ColorInfo;
277241
export const chalkStderr: typeof chalk;
278242
export const supportsColorStderr: typeof supportsColor;
279243

244+
export {
245+
ModifierName, ForegroundColorName, BackgroundColorName, ColorName,
246+
modifierNames, foregroundColorNames, backgroundColorNames, colorNames,
247+
// } from '#ansi-styles';
248+
} from './vendor/ansi-styles/index.js';
249+
280250
export {
281251
ColorInfo,
282252
ColorSupport,
283253
ColorSupportLevel,
284254
// } from '#supports-color';
285255
} from './vendor/supports-color/index.js';
286256

257+
// TODO: Remove these aliases in the next major version
258+
/**
259+
@deprecated Use `ModifierName` instead.
260+
261+
Basic modifier names.
262+
*/
263+
export type Modifiers = ModifierName;
264+
265+
/**
266+
@deprecated Use `ForegroundColorName` instead.
267+
268+
Basic foreground color names.
269+
270+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
271+
*/
272+
export type ForegroundColor = ForegroundColorName;
273+
274+
/**
275+
@deprecated Use `BackgroundColorName` instead.
276+
277+
Basic background color names.
278+
279+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
280+
*/
281+
export type BackgroundColor = BackgroundColorName;
282+
283+
/**
284+
@deprecated Use `ColorName` instead.
285+
286+
Basic color names. The combination of foreground and background color names.
287+
288+
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
289+
*/
290+
export type Color = ColorName;
291+
292+
/**
293+
@deprecated Use `modifierNames` instead.
294+
295+
Basic modifier names.
296+
*/
287297
export const modifiers: readonly Modifiers[];
298+
299+
/**
300+
@deprecated Use `foregroundColorNames` instead.
301+
302+
Basic foreground color names.
303+
*/
288304
export const foregroundColors: readonly ForegroundColor[];
305+
306+
/**
307+
@deprecated Use `backgroundColorNames` instead.
308+
309+
Basic background color names.
310+
*/
289311
export const backgroundColors: readonly BackgroundColor[];
312+
313+
/**
314+
@deprecated Use `colorNames` instead.
315+
316+
Basic color names. The combination of foreground and background color names.
317+
*/
290318
export const colors: readonly Color[];
291319

292320
export default chalk;

‎source/index.js

+13-5
Original file line numberDiff line numberDiff line change
@@ -204,14 +204,22 @@ Object.defineProperties(createChalk.prototype, styles);
204204
const chalk = createChalk();
205205
export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level : 0});
206206

207+
export {
208+
modifierNames,
209+
foregroundColorNames,
210+
backgroundColorNames,
211+
colorNames,
212+
213+
// TODO: Remove these aliases in the next major version
214+
modifierNames as modifiers,
215+
foregroundColorNames as foregroundColors,
216+
backgroundColorNames as backgroundColors,
217+
colorNames as colors,
218+
} from './vendor/ansi-styles/index.js';
219+
207220
export {
208221
stdoutColor as supportsColor,
209222
stderrColor as supportsColorStderr,
210223
};
211224

212-
export const modifiers = Object.keys(ansiStyles.modifier);
213-
export const foregroundColors = Object.keys(ansiStyles.color);
214-
export const backgroundColors = Object.keys(ansiStyles.bgColor);
215-
export const colors = [...foregroundColors, ...backgroundColors];
216-
217225
export default chalk;

‎source/index.test-d.ts

+23-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import {expectType, expectAssignable, expectError} from 'tsd';
2-
import chalk, {Chalk, ChalkInstance, Color, ColorInfo, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr} from './index.js';
1+
import {expectType, expectAssignable, expectError, expectDeprecated} from 'tsd';
2+
import chalk, {
3+
Chalk, ChalkInstance, ColorInfo, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr,
4+
ModifierName, ForegroundColorName, BackgroundColorName, ColorName,
5+
Modifiers,
6+
} from './index.js';
37

48
// - supportsColor -
59
expectType<ColorInfo>(supportsColor);
@@ -141,6 +145,20 @@ expectType<string>(chalk.underline``);
141145
expectType<string>(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`);
142146
expectType<string>(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`);
143147

144-
// -- Color types ==
145-
expectAssignable<Color>('red');
146-
expectError<Color>('hotpink');
148+
// -- Modifiers types
149+
expectAssignable<ModifierName>('strikethrough');
150+
expectError<ModifierName>('delete');
151+
152+
// -- Foreground types
153+
expectAssignable<ForegroundColorName>('red');
154+
expectError<ForegroundColorName>('pink');
155+
156+
// -- Background types
157+
expectAssignable<BackgroundColorName>('bgRed');
158+
expectError<BackgroundColorName>('bgPink');
159+
160+
// -- Color types --
161+
expectAssignable<ColorName>('red');
162+
expectAssignable<ColorName>('bgRed');
163+
expectError<ColorName>('hotpink');
164+
expectError<ColorName>('bgHotpink');

‎source/vendor/ansi-styles/index.d.ts

+46
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;

‎source/vendor/ansi-styles/index.js

+66-62
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)) {
@@ -129,12 +128,12 @@ function assembleStyles() {
129128
},
130129
hexToRgb: {
131130
value(hex) {
132-
const matches = /(?<colorString>[a-f\d]{6}|[a-f\d]{3})/i.exec(hex.toString(16));
131+
const matches = /[a-f\d]{6}|[a-f\d]{3}/i.exec(hex.toString(16));
133132
if (!matches) {
134133
return [0, 0, 0];
135134
}
136135

137-
let {colorString} = matches.groups;
136+
let [colorString] = matches;
138137

139138
if (colorString.length === 3) {
140139
colorString = [...colorString].map(character => character + character).join('');
@@ -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];

0 commit comments

Comments
 (0)
Please sign in to comment.