Skip to content

Commit

Permalink
Re-export types from supports-color (#526)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Nov 21, 2021
1 parent c987c61 commit 09fd5c4
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 50 deletions.
1 change: 1 addition & 0 deletions examples/screenshot.js
@@ -1,3 +1,4 @@
import process from 'node:process';
import styles from 'ansi-styles';
import chalk from '../source/index.js';

Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -42,7 +42,7 @@
],
"dependencies": {
"ansi-styles": "^6.1.0",
"supports-color": "^9.0.2"
"supports-color": "^9.1.0"
},
"devDependencies": {
"ava": "^3.15.0",
Expand All @@ -54,7 +54,7 @@
"nyc": "^15.1.0",
"tsd": "^0.17.0",
"typescript": "^4.3.5",
"xo": "^0.42.0",
"xo": "^0.46.4",
"yoctodelay": "^1.2.0"
},
"types": "./source/index.d.ts",
Expand Down
46 changes: 10 additions & 36 deletions source/index.d.ts
@@ -1,3 +1,5 @@
import {ColorInfo, ColorSupportLevel} from 'supports-color';

/**
Basic foreground colors.
Expand Down Expand Up @@ -67,15 +69,6 @@ export type Modifiers =
| 'strikethrough'
| 'visible';

/**
Levels:
- `0` - All colors disabled.
- `1` - Basic 16 colors support.
- `2` - ANSI 256 colors support.
- `3` - Truecolor 16 million colors support.
*/
export type ColorSupportLevel = 0 | 1 | 2 | 3;

export interface Options {
/**
Specify the color support for Chalk.
Expand All @@ -94,32 +87,7 @@ export interface Options {
/**
Return a new Chalk instance.
*/
export const Chalk: new (options?: Options) => ChalkInstance;

/**
Detect whether the terminal supports color.
*/
export interface ColorSupport {
/**
The color level used by Chalk.
*/
level: ColorSupportLevel;

/**
Return whether Chalk supports basic 16 colors.
*/
hasBasic: boolean;

/**
Return whether Chalk supports ANSI 256 colors.
*/
has256: boolean;

/**
Return whether Chalk supports Truecolor 16 million colors.
*/
has16m: boolean;
}
export const Chalk: new (options?: Options) => ChalkInstance; // eslint-disable-line @typescript-eslint/naming-convention

export interface ChalkInstance {
(...text: unknown[]): string;
Expand Down Expand Up @@ -333,9 +301,15 @@ This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
*/
declare const chalk: ChalkInstance;

export const supportsColor: ColorSupport | false;
export const supportsColor: ColorInfo;

export const chalkStderr: typeof chalk;
export const supportsColorStderr: typeof supportsColor;

export {
ColorInfo,
ColorSupport,
ColorSupportLevel,
} from 'supports-color';

export default chalk;
23 changes: 11 additions & 12 deletions source/index.test-d.ts
@@ -1,20 +1,19 @@
import {expectType, expectAssignable, expectError} from 'tsd';
import chalk, {Chalk, ChalkInstance, Color, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr} from './index.js';

// - Helpers -
type colorReturn = ChalkInstance & {supportsColor?: never};
import chalk, {Chalk, ChalkInstance, Color, ColorInfo, ColorSupport, ColorSupportLevel, chalkStderr, supportsColor, supportsColorStderr} from './index.js';

// - supportsColor -
expectType<ColorSupport | false>(supportsColor);
expectType<ColorInfo>(supportsColor);
if (supportsColor) {
expectType<ColorSupport>(supportsColor);
expectType<ColorSupportLevel>(supportsColor.level);
expectType<boolean>(supportsColor.hasBasic);
expectType<boolean>(supportsColor.has256);
expectType<boolean>(supportsColor.has16m);
}

// - stderr -
expectAssignable<ChalkInstance>(chalkStderr);
expectType<ColorSupport | false>(supportsColorStderr);
expectType<ColorInfo>(supportsColorStderr);
if (supportsColorStderr) {
expectType<boolean>(supportsColorStderr.hasBasic);
expectType<boolean>(supportsColorStderr.has256);
Expand All @@ -35,12 +34,12 @@ expectType<ChalkInstance>(new Chalk({level: 1}));
expectType<ColorSupportLevel>(chalk.level);

// -- Color methods --
expectAssignable<colorReturn>(chalk.rgb(0, 0, 0));
expectAssignable<colorReturn>(chalk.hex('#DEADED'));
expectAssignable<colorReturn>(chalk.ansi256(0));
expectAssignable<colorReturn>(chalk.bgRgb(0, 0, 0));
expectAssignable<colorReturn>(chalk.bgHex('#DEADED'));
expectAssignable<colorReturn>(chalk.bgAnsi256(0));
expectType<ChalkInstance>(chalk.rgb(0, 0, 0));
expectType<ChalkInstance>(chalk.hex('#DEADED'));
expectType<ChalkInstance>(chalk.ansi256(0));
expectType<ChalkInstance>(chalk.bgRgb(0, 0, 0));
expectType<ChalkInstance>(chalk.bgHex('#DEADED'));
expectType<ChalkInstance>(chalk.bgAnsi256(0));

// -- Modifiers --
expectType<string>(chalk.reset('foo'));
Expand Down
1 change: 1 addition & 0 deletions test/chalk.js
@@ -1,3 +1,4 @@
import process from 'node:process';
import test from 'ava';
import chalk, {Chalk, chalkStderr} from '../source/index.js';

Expand Down

0 comments on commit 09fd5c4

Please sign in to comment.