Skip to content

Commit 6e0df05

Browse files
authored
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

Lines changed: 5 additions & 5 deletions
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

Lines changed: 66 additions & 38 deletions
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

Lines changed: 13 additions & 5 deletions
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

Lines changed: 23 additions & 5 deletions
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

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;

0 commit comments

Comments
 (0)