Skip to content

Commit

Permalink
Add named exports (#432)
Browse files Browse the repository at this point in the history
  • Loading branch information
Richienb committed Apr 17, 2021
1 parent fa16f4e commit d798222
Show file tree
Hide file tree
Showing 10 changed files with 149 additions and 167 deletions.
145 changes: 71 additions & 74 deletions index.d.ts
Expand Up @@ -93,7 +93,7 @@ export interface Options {
/**
Return a new Chalk instance.
*/
export type ChalkInstance = new (options?: Options) => Chalk;
export const Chalk: new (options?: Options) => ChalkInstance;

/**
Detect whether the terminal supports color.
Expand Down Expand Up @@ -149,12 +149,7 @@ interface ChalkFunction {
(...text: unknown[]): string;
}

export interface Chalk extends ChalkFunction {
/**
Return a new Chalk instance.
*/
Instance: ChalkInstance;

export interface ChalkInstance extends ChalkFunction {
/**
The color support for Chalk.
Expand All @@ -180,7 +175,7 @@ export interface Chalk extends ChalkFunction {
chalk.hex('#DEADED');
```
*/
hex: (color: string) => Chalk;
hex: (color: string) => this;

/**
Use keyword color value to set text color.
Expand All @@ -194,40 +189,40 @@ export interface Chalk extends ChalkFunction {
chalk.keyword('orange');
```
*/
keyword: (color: string) => Chalk;
keyword: (color: string) => this;

/**
Use RGB values to set text color.
*/
rgb: (red: number, green: number, blue: number) => Chalk;
rgb: (red: number, green: number, blue: number) => this;

/**
Use HSL values to set text color.
*/
hsl: (hue: number, saturation: number, lightness: number) => Chalk;
hsl: (hue: number, saturation: number, lightness: number) => this;

/**
Use HSV values to set text color.
*/
hsv: (hue: number, saturation: number, value: number) => Chalk;
hsv: (hue: number, saturation: number, value: number) => this;

/**
Use HWB values to set text color.
*/
hwb: (hue: number, whiteness: number, blackness: number) => Chalk;
hwb: (hue: number, whiteness: number, blackness: number) => this;

/**
Use a [Select/Set Graphic Rendition](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters) (SGR) [color code number](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) to set text color.
30 <= code && code < 38 || 90 <= code && code < 98
For example, 31 for red, 91 for redBright.
*/
ansi: (code: number) => Chalk;
ansi: (code: number) => this;

/**
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
*/
ansi256: (index: number) => Chalk;
ansi256: (index: number) => this;

/**
Use HEX value to set background color.
Expand All @@ -241,7 +236,7 @@ export interface Chalk extends ChalkFunction {
chalk.bgHex('#DEADED');
```
*/
bgHex: (color: string) => Chalk;
bgHex: (color: string) => this;

/**
Use keyword color value to set background color.
Expand All @@ -255,27 +250,27 @@ export interface Chalk extends ChalkFunction {
chalk.bgKeyword('orange');
```
*/
bgKeyword: (color: string) => Chalk;
bgKeyword: (color: string) => this;

/**
Use RGB values to set background color.
*/
bgRgb: (red: number, green: number, blue: number) => Chalk;
bgRgb: (red: number, green: number, blue: number) => this;

/**
Use HSL values to set background color.
*/
bgHsl: (hue: number, saturation: number, lightness: number) => Chalk;
bgHsl: (hue: number, saturation: number, lightness: number) => this;

/**
Use HSV values to set background color.
*/
bgHsv: (hue: number, saturation: number, value: number) => Chalk;
bgHsv: (hue: number, saturation: number, value: number) => this;

/**
Use HWB values to set background color.
*/
bgHwb: (hue: number, whiteness: number, blackness: number) => Chalk;
bgHwb: (hue: number, whiteness: number, blackness: number) => this;

/**
Use a [Select/Set Graphic Rendition](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters) (SGR) [color code number](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) to set background color.
Expand All @@ -284,114 +279,114 @@ export interface Chalk extends ChalkFunction {
For example, 31 for red, 91 for redBright.
Use the foreground code, not the background code (for example, not 41, nor 101).
*/
bgAnsi: (code: number) => Chalk;
bgAnsi: (code: number) => this;

/**
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
*/
bgAnsi256: (index: number) => Chalk;
bgAnsi256: (index: number) => this;

/**
Modifier: Resets the current color chain.
*/
readonly reset: Chalk;
readonly reset: this;

/**
Modifier: Make text bold.
*/
readonly bold: Chalk;
readonly bold: this;

/**
Modifier: Emitting only a small amount of light.
*/
readonly dim: Chalk;
readonly dim: this;

/**
Modifier: Make text italic. (Not widely supported)
*/
readonly italic: Chalk;
readonly italic: this;

/**
Modifier: Make text underline. (Not widely supported)
*/
readonly underline: Chalk;
readonly underline: this;

/**
Modifier: Inverse background and foreground colors.
*/
readonly inverse: Chalk;
readonly inverse: this;

/**
Modifier: Prints the text, but makes it invisible.
*/
readonly hidden: Chalk;
readonly hidden: this;

/**
Modifier: Puts a horizontal line through the center of the text. (Not widely supported)
*/
readonly strikethrough: Chalk;
readonly strikethrough: this;

/**
Modifier: Prints the text only when Chalk has a color support level > 0.
Can be useful for things that are purely cosmetic.
*/
readonly visible: Chalk;
readonly visible: this;

readonly black: Chalk;
readonly red: Chalk;
readonly green: Chalk;
readonly yellow: Chalk;
readonly blue: Chalk;
readonly magenta: Chalk;
readonly cyan: Chalk;
readonly white: Chalk;
readonly black: this;
readonly red: this;
readonly green: this;
readonly yellow: this;
readonly blue: this;
readonly magenta: this;
readonly cyan: this;
readonly white: this;

/*
Alias for `blackBright`.
*/
readonly gray: Chalk;
readonly gray: this;

/*
Alias for `blackBright`.
*/
readonly grey: Chalk;

readonly blackBright: Chalk;
readonly redBright: Chalk;
readonly greenBright: Chalk;
readonly yellowBright: Chalk;
readonly blueBright: Chalk;
readonly magentaBright: Chalk;
readonly cyanBright: Chalk;
readonly whiteBright: Chalk;

readonly bgBlack: Chalk;
readonly bgRed: Chalk;
readonly bgGreen: Chalk;
readonly bgYellow: Chalk;
readonly bgBlue: Chalk;
readonly bgMagenta: Chalk;
readonly bgCyan: Chalk;
readonly bgWhite: Chalk;
readonly grey: this;

readonly blackBright: this;
readonly redBright: this;
readonly greenBright: this;
readonly yellowBright: this;
readonly blueBright: this;
readonly magentaBright: this;
readonly cyanBright: this;
readonly whiteBright: this;

readonly bgBlack: this;
readonly bgRed: this;
readonly bgGreen: this;
readonly bgYellow: this;
readonly bgBlue: this;
readonly bgMagenta: this;
readonly bgCyan: this;
readonly bgWhite: this;

/*
Alias for `bgBlackBright`.
*/
readonly bgGray: Chalk;
readonly bgGray: this;

/*
Alias for `bgBlackBright`.
*/
readonly bgGrey: Chalk;
readonly bgGrey: this;

readonly bgBlackBright: Chalk;
readonly bgRedBright: Chalk;
readonly bgGreenBright: Chalk;
readonly bgYellowBright: Chalk;
readonly bgBlueBright: Chalk;
readonly bgMagentaBright: Chalk;
readonly bgCyanBright: Chalk;
readonly bgWhiteBright: Chalk;
readonly bgBlackBright: this;
readonly bgRedBright: this;
readonly bgGreenBright: this;
readonly bgYellowBright: this;
readonly bgBlueBright: this;
readonly bgMagentaBright: this;
readonly bgCyanBright: this;
readonly bgWhiteBright: this;
}

/**
Expand All @@ -403,9 +398,11 @@ Order doesn't matter, and later styles take precedent in case of a conflict.
This simply means that `chalk.red.yellow.green` is equivalent to `chalk.green`.
*/
declare const chalk: Chalk & ChalkFunction & {
supportsColor: ColorSupport | false;
stderr: Chalk & {supportsColor: ColorSupport | false};
};
declare const chalk: ChalkInstance & ChalkFunction;

export const supportsColor: ColorSupport | false;

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

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

// - Helpers -
type colorReturn = Chalk & {supportsColor?: never};
type colorReturn = ChalkInstance & {supportsColor?: never};

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

// - stderr -
expectAssignable<Chalk>(chalk.stderr);
expectType<ColorSupport | false>(chalk.stderr.supportsColor);
if (chalk.stderr.supportsColor) {
expectType<boolean>(chalk.stderr.supportsColor.hasBasic);
expectType<boolean>(chalk.stderr.supportsColor.has256);
expectType<boolean>(chalk.stderr.supportsColor.has16m);
expectAssignable<ChalkInstance>(chalkStderr);
expectType<ColorSupport | false>(supportsColorStderr);
if (supportsColorStderr) {
expectType<boolean>(supportsColorStderr.hasBasic);
expectType<boolean>(supportsColorStderr.has256);
expectType<boolean>(supportsColorStderr.has16m);
}

// -- `stderr` is not a member of the Chalk interface --
expectError(chalk.reset.stderr);
// -- `supportsColorStderr` is not a member of the Chalk interface --
expectError(chalk.reset.supportsColorStderr);

// -- `supportsColor` is not a member of the Chalk interface --
expectError(chalk.reset.supportsColor);

// - Chalk -
// -- Instance --
expectType<Chalk>(new chalk.Instance({level: 1}));
expectType<ChalkInstance>(new Chalk({level: 1}));

// -- Properties --
expectType<ColorSupportLevel>(chalk.level);
Expand Down
10 changes: 5 additions & 5 deletions readme.md
Expand Up @@ -171,9 +171,9 @@ Color support is automatically detected, but you can override it by setting the
If you need to change this in a reusable module, create a new instance:

```js
import chalk from 'chalk';
import {Chalk} from 'chalk';

const customChalk = new chalk.Instance({level: 0});
const customChalk = new Chalk({level: 0});
```

| Level | Description |
Expand All @@ -183,17 +183,17 @@ const customChalk = new chalk.Instance({level: 0});
| `2` | 256 color support |
| `3` | Truecolor support (16 million colors) |

### chalk.supportsColor
### supportsColor

Detect whether the terminal [supports color](https://github.com/chalk/supports-color). Used internally and handled for you, but exposed for convenience.

Can be overridden by the user with the flags `--color` and `--no-color`. For situations where using `--color` is not possible, use the environment variable `FORCE_COLOR=1` (level 1), `FORCE_COLOR=2` (level 2), or `FORCE_COLOR=3` (level 3) to forcefully enable color, or `FORCE_COLOR=0` to forcefully disable. The use of `FORCE_COLOR` overrides all other color support checks.

Explicit 256/Truecolor mode can be enabled using the `--color=256` and `--color=16m` flags, respectively.

### chalk.stderr and chalk.stderr.supportsColor
### chalkStderr and supportsColorStderr

`chalk.stderr` contains a separate instance configured with color support detected for `stderr` stream instead of `stdout`. Override rules from `chalk.supportsColor` apply to this too. `chalk.stderr.supportsColor` is exposed for convenience.
`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.

## Styles

Expand Down

0 comments on commit d798222

Please sign in to comment.