parent
48d25d156a
commit
625a285772
9 changed files with 41 additions and 39 deletions
339
source/index.d.ts
vendored
Normal file
339
source/index.d.ts
vendored
Normal file
|
|
@ -0,0 +1,339 @@
|
|||
/**
|
||||
Basic foreground colors.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type ForegroundColor =
|
||||
| 'black'
|
||||
| 'red'
|
||||
| 'green'
|
||||
| 'yellow'
|
||||
| 'blue'
|
||||
| 'magenta'
|
||||
| 'cyan'
|
||||
| 'white'
|
||||
| 'gray'
|
||||
| 'grey'
|
||||
| 'blackBright'
|
||||
| 'redBright'
|
||||
| 'greenBright'
|
||||
| 'yellowBright'
|
||||
| 'blueBright'
|
||||
| 'magentaBright'
|
||||
| 'cyanBright'
|
||||
| 'whiteBright';
|
||||
|
||||
/**
|
||||
Basic background colors.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type BackgroundColor =
|
||||
| 'bgBlack'
|
||||
| 'bgRed'
|
||||
| 'bgGreen'
|
||||
| 'bgYellow'
|
||||
| 'bgBlue'
|
||||
| 'bgMagenta'
|
||||
| 'bgCyan'
|
||||
| 'bgWhite'
|
||||
| 'bgGray'
|
||||
| 'bgGrey'
|
||||
| 'bgBlackBright'
|
||||
| 'bgRedBright'
|
||||
| 'bgGreenBright'
|
||||
| 'bgYellowBright'
|
||||
| 'bgBlueBright'
|
||||
| 'bgMagentaBright'
|
||||
| 'bgCyanBright'
|
||||
| 'bgWhiteBright';
|
||||
|
||||
/**
|
||||
Basic colors.
|
||||
|
||||
[More colors here.](https://github.com/chalk/chalk/blob/main/readme.md#256-and-truecolor-color-support)
|
||||
*/
|
||||
export type Color = ForegroundColor | BackgroundColor;
|
||||
|
||||
export type Modifiers =
|
||||
| 'reset'
|
||||
| 'bold'
|
||||
| 'dim'
|
||||
| 'italic'
|
||||
| 'underline'
|
||||
| 'overline'
|
||||
| 'inverse'
|
||||
| 'hidden'
|
||||
| '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.
|
||||
|
||||
By default, color support is automatically detected based on the environment.
|
||||
|
||||
Levels:
|
||||
- `0` - All colors disabled.
|
||||
- `1` - Basic 16 colors support.
|
||||
- `2` - ANSI 256 colors support.
|
||||
- `3` - Truecolor 16 million colors support.
|
||||
*/
|
||||
readonly level?: ColorSupportLevel;
|
||||
}
|
||||
|
||||
/**
|
||||
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;
|
||||
}
|
||||
|
||||
interface ChalkFunction {
|
||||
/**
|
||||
Use a template string.
|
||||
|
||||
@remarks Template literals are unsupported for nested calls (see [issue #341](https://github.com/chalk/chalk/issues/341))
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
log(chalk`
|
||||
CPU: {red ${cpu.totalPercent}%}
|
||||
RAM: {green ${ram.used / ram.total * 100}%}
|
||||
DISK: {rgb(255,131,0) ${disk.used / disk.total * 100}%}
|
||||
`);
|
||||
```
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
log(chalk.red.bgBlack`2 + 3 = {bold ${2 + 3}}`)
|
||||
```
|
||||
*/
|
||||
(text: TemplateStringsArray, ...placeholders: unknown[]): string;
|
||||
|
||||
(...text: unknown[]): string;
|
||||
}
|
||||
|
||||
export interface ChalkInstance extends ChalkFunction {
|
||||
/**
|
||||
The color support for Chalk.
|
||||
|
||||
By default, color support is automatically detected based on the environment.
|
||||
|
||||
Levels:
|
||||
- `0` - All colors disabled.
|
||||
- `1` - Basic 16 colors support.
|
||||
- `2` - ANSI 256 colors support.
|
||||
- `3` - Truecolor 16 million colors support.
|
||||
*/
|
||||
level: ColorSupportLevel;
|
||||
|
||||
/**
|
||||
Use RGB values to set text color.
|
||||
*/
|
||||
rgb: (red: number, green: number, blue: number) => this;
|
||||
|
||||
/**
|
||||
Use HEX value to set text color.
|
||||
|
||||
@param color - Hexadecimal value representing the desired color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.hex('#DEADED');
|
||||
```
|
||||
*/
|
||||
hex: (color: string) => this;
|
||||
|
||||
/**
|
||||
Use an [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
|
||||
*/
|
||||
ansi256: (index: number) => this;
|
||||
|
||||
/**
|
||||
Use RGB values to set background color.
|
||||
*/
|
||||
bgRgb: (red: number, green: number, blue: number) => this;
|
||||
|
||||
/**
|
||||
Use HEX value to set background color.
|
||||
|
||||
@param color - Hexadecimal value representing the desired color.
|
||||
|
||||
@example
|
||||
```
|
||||
import chalk from 'chalk';
|
||||
|
||||
chalk.bgHex('#DEADED');
|
||||
```
|
||||
*/
|
||||
bgHex: (color: string) => this;
|
||||
|
||||
/**
|
||||
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
|
||||
*/
|
||||
bgAnsi256: (index: number) => this;
|
||||
|
||||
/**
|
||||
Modifier: Resets the current color chain.
|
||||
*/
|
||||
readonly reset: this;
|
||||
|
||||
/**
|
||||
Modifier: Make text bold.
|
||||
*/
|
||||
readonly bold: this;
|
||||
|
||||
/**
|
||||
Modifier: Make text slightly darker. (Inconsistent across terminals; might do nothing)
|
||||
*/
|
||||
readonly dim: this;
|
||||
|
||||
/**
|
||||
Modifier: Make text italic. (Not widely supported)
|
||||
*/
|
||||
readonly italic: this;
|
||||
|
||||
/**
|
||||
Modifier: Make text underline. (Not widely supported)
|
||||
*/
|
||||
readonly underline: this;
|
||||
|
||||
/**
|
||||
Modifier: Make text overline. (Not widely supported)
|
||||
*/
|
||||
readonly overline: this;
|
||||
|
||||
/**
|
||||
Modifier: Inverse background and foreground colors.
|
||||
*/
|
||||
readonly inverse: this;
|
||||
|
||||
/**
|
||||
Modifier: Prints the text, but makes it invisible.
|
||||
*/
|
||||
readonly hidden: this;
|
||||
|
||||
/**
|
||||
Modifier: Puts a horizontal line through the center of the text. (Not widely supported)
|
||||
*/
|
||||
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: this;
|
||||
|
||||
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: this;
|
||||
|
||||
/*
|
||||
Alias for `blackBright`.
|
||||
*/
|
||||
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: this;
|
||||
|
||||
/*
|
||||
Alias for `bgBlackBright`.
|
||||
*/
|
||||
readonly bgGrey: this;
|
||||
|
||||
readonly bgBlackBright: this;
|
||||
readonly bgRedBright: this;
|
||||
readonly bgGreenBright: this;
|
||||
readonly bgYellowBright: this;
|
||||
readonly bgBlueBright: this;
|
||||
readonly bgMagentaBright: this;
|
||||
readonly bgCyanBright: this;
|
||||
readonly bgWhiteBright: this;
|
||||
}
|
||||
|
||||
/**
|
||||
Main Chalk object that allows to chain styles together.
|
||||
|
||||
Call the last one as a method with a string argument.
|
||||
|
||||
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: ChalkInstance & ChalkFunction;
|
||||
|
||||
export const supportsColor: ColorSupport | false;
|
||||
|
||||
export const chalkStderr: typeof chalk;
|
||||
export const supportsColorStderr: typeof supportsColor;
|
||||
|
||||
export default chalk;
|
||||
|
|
@ -2,7 +2,7 @@ import ansiStyles from 'ansi-styles';
|
|||
import supportsColor from 'supports-color';
|
||||
import {
|
||||
stringReplaceAll,
|
||||
stringEncaseCRLFWithFirstIndex
|
||||
stringEncaseCRLFWithFirstIndex,
|
||||
} from './util.js';
|
||||
import template from './templates.js';
|
||||
|
||||
|
|
@ -18,7 +18,7 @@ const levelMapping = [
|
|||
'ansi',
|
||||
'ansi',
|
||||
'ansi256',
|
||||
'ansi16m'
|
||||
'ansi16m',
|
||||
];
|
||||
|
||||
const styles = Object.create(null);
|
||||
|
|
@ -66,7 +66,7 @@ for (const [styleName, style] of Object.entries(ansiStyles)) {
|
|||
const builder = createBuilder(this, createStyler(style.open, style.close, this[STYLER]), this[IS_EMPTY]);
|
||||
Object.defineProperty(this, styleName, {value: builder});
|
||||
return builder;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +75,7 @@ styles.visible = {
|
|||
const builder = createBuilder(this, this[STYLER], true);
|
||||
Object.defineProperty(this, 'visible', {value: builder});
|
||||
return builder;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const getModelAnsi = (model, level, type, ...arguments_) => {
|
||||
|
|
@ -108,7 +108,7 @@ for (const model of usedModels) {
|
|||
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'color', ...arguments_), ansiStyles.color.close, this[STYLER]);
|
||||
return createBuilder(this, styler, this[IS_EMPTY]);
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
const bgModel = 'bg' + model[0].toUpperCase() + model.slice(1);
|
||||
|
|
@ -119,7 +119,7 @@ for (const model of usedModels) {
|
|||
const styler = createStyler(getModelAnsi(model, levelMapping[level], 'bgColor', ...arguments_), ansiStyles.bgColor.close, this[STYLER]);
|
||||
return createBuilder(this, styler, this[IS_EMPTY]);
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
|
|
@ -132,8 +132,8 @@ const proto = Object.defineProperties(() => {}, {
|
|||
},
|
||||
set(level) {
|
||||
this[GENERATOR].level = level;
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const createStyler = (open, close, parent) => {
|
||||
|
|
@ -152,7 +152,7 @@ const createStyler = (open, close, parent) => {
|
|||
close,
|
||||
openAll,
|
||||
closeAll,
|
||||
parent
|
||||
parent,
|
||||
};
|
||||
};
|
||||
|
||||
|
|
@ -228,7 +228,7 @@ const chalkTag = (chalk, ...strings) => {
|
|||
for (let i = 1; i < firstString.length; i++) {
|
||||
parts.push(
|
||||
String(arguments_[i - 1]).replace(/[{}\\]/g, '\\$&'),
|
||||
String(firstString.raw[i])
|
||||
String(firstString.raw[i]),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -242,7 +242,7 @@ export const chalkStderr = createChalk({level: stderrColor ? stderrColor.level :
|
|||
|
||||
export {
|
||||
stdoutColor as supportsColor,
|
||||
stderrColor as supportsColorStderr
|
||||
stderrColor as supportsColorStderr,
|
||||
};
|
||||
|
||||
export default chalk;
|
||||
|
|
|
|||
153
source/index.test-d.ts
Normal file
153
source/index.test-d.ts
Normal file
|
|
@ -0,0 +1,153 @@
|
|||
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};
|
||||
|
||||
// - supportsColor -
|
||||
expectType<ColorSupport | false>(supportsColor);
|
||||
if (supportsColor) {
|
||||
expectType<boolean>(supportsColor.hasBasic);
|
||||
expectType<boolean>(supportsColor.has256);
|
||||
expectType<boolean>(supportsColor.has16m);
|
||||
}
|
||||
|
||||
// - stderr -
|
||||
expectAssignable<ChalkInstance>(chalkStderr);
|
||||
expectType<ColorSupport | false>(supportsColorStderr);
|
||||
if (supportsColorStderr) {
|
||||
expectType<boolean>(supportsColorStderr.hasBasic);
|
||||
expectType<boolean>(supportsColorStderr.has256);
|
||||
expectType<boolean>(supportsColorStderr.has16m);
|
||||
}
|
||||
|
||||
// -- `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<ChalkInstance>(new Chalk({level: 1}));
|
||||
|
||||
// -- Properties --
|
||||
expectType<ColorSupportLevel>(chalk.level);
|
||||
|
||||
// -- Template literal --
|
||||
expectType<string>(chalk``);
|
||||
const name = 'John';
|
||||
expectType<string>(chalk`Hello {bold.red ${name}}`);
|
||||
expectType<string>(chalk`Works with numbers {bold.red ${1}}`);
|
||||
|
||||
// -- 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));
|
||||
|
||||
// -- Modifiers --
|
||||
expectType<string>(chalk.reset('foo'));
|
||||
expectType<string>(chalk.bold('foo'));
|
||||
expectType<string>(chalk.dim('foo'));
|
||||
expectType<string>(chalk.italic('foo'));
|
||||
expectType<string>(chalk.underline('foo'));
|
||||
expectType<string>(chalk.overline('foo'));
|
||||
expectType<string>(chalk.inverse('foo'));
|
||||
expectType<string>(chalk.hidden('foo'));
|
||||
expectType<string>(chalk.strikethrough('foo'));
|
||||
expectType<string>(chalk.visible('foo'));
|
||||
expectType<string>(chalk.reset`foo`);
|
||||
expectType<string>(chalk.bold`foo`);
|
||||
expectType<string>(chalk.dim`foo`);
|
||||
expectType<string>(chalk.italic`foo`);
|
||||
expectType<string>(chalk.underline`foo`);
|
||||
expectType<string>(chalk.inverse`foo`);
|
||||
expectType<string>(chalk.hidden`foo`);
|
||||
expectType<string>(chalk.strikethrough`foo`);
|
||||
expectType<string>(chalk.visible`foo`);
|
||||
|
||||
// -- Colors --
|
||||
expectType<string>(chalk.black('foo'));
|
||||
expectType<string>(chalk.red('foo'));
|
||||
expectType<string>(chalk.green('foo'));
|
||||
expectType<string>(chalk.yellow('foo'));
|
||||
expectType<string>(chalk.blue('foo'));
|
||||
expectType<string>(chalk.magenta('foo'));
|
||||
expectType<string>(chalk.cyan('foo'));
|
||||
expectType<string>(chalk.white('foo'));
|
||||
expectType<string>(chalk.gray('foo'));
|
||||
expectType<string>(chalk.grey('foo'));
|
||||
expectType<string>(chalk.blackBright('foo'));
|
||||
expectType<string>(chalk.redBright('foo'));
|
||||
expectType<string>(chalk.greenBright('foo'));
|
||||
expectType<string>(chalk.yellowBright('foo'));
|
||||
expectType<string>(chalk.blueBright('foo'));
|
||||
expectType<string>(chalk.magentaBright('foo'));
|
||||
expectType<string>(chalk.cyanBright('foo'));
|
||||
expectType<string>(chalk.whiteBright('foo'));
|
||||
expectType<string>(chalk.bgBlack('foo'));
|
||||
expectType<string>(chalk.bgRed('foo'));
|
||||
expectType<string>(chalk.bgGreen('foo'));
|
||||
expectType<string>(chalk.bgYellow('foo'));
|
||||
expectType<string>(chalk.bgBlue('foo'));
|
||||
expectType<string>(chalk.bgMagenta('foo'));
|
||||
expectType<string>(chalk.bgCyan('foo'));
|
||||
expectType<string>(chalk.bgWhite('foo'));
|
||||
expectType<string>(chalk.bgBlackBright('foo'));
|
||||
expectType<string>(chalk.bgRedBright('foo'));
|
||||
expectType<string>(chalk.bgGreenBright('foo'));
|
||||
expectType<string>(chalk.bgYellowBright('foo'));
|
||||
expectType<string>(chalk.bgBlueBright('foo'));
|
||||
expectType<string>(chalk.bgMagentaBright('foo'));
|
||||
expectType<string>(chalk.bgCyanBright('foo'));
|
||||
expectType<string>(chalk.bgWhiteBright('foo'));
|
||||
expectType<string>(chalk.black`foo`);
|
||||
expectType<string>(chalk.red`foo`);
|
||||
expectType<string>(chalk.green`foo`);
|
||||
expectType<string>(chalk.yellow`foo`);
|
||||
expectType<string>(chalk.blue`foo`);
|
||||
expectType<string>(chalk.magenta`foo`);
|
||||
expectType<string>(chalk.cyan`foo`);
|
||||
expectType<string>(chalk.white`foo`);
|
||||
expectType<string>(chalk.gray`foo`);
|
||||
expectType<string>(chalk.grey`foo`);
|
||||
expectType<string>(chalk.blackBright`foo`);
|
||||
expectType<string>(chalk.redBright`foo`);
|
||||
expectType<string>(chalk.greenBright`foo`);
|
||||
expectType<string>(chalk.yellowBright`foo`);
|
||||
expectType<string>(chalk.blueBright`foo`);
|
||||
expectType<string>(chalk.magentaBright`foo`);
|
||||
expectType<string>(chalk.cyanBright`foo`);
|
||||
expectType<string>(chalk.whiteBright`foo`);
|
||||
expectType<string>(chalk.bgBlack`foo`);
|
||||
expectType<string>(chalk.bgRed`foo`);
|
||||
expectType<string>(chalk.bgGreen`foo`);
|
||||
expectType<string>(chalk.bgYellow`foo`);
|
||||
expectType<string>(chalk.bgBlue`foo`);
|
||||
expectType<string>(chalk.bgMagenta`foo`);
|
||||
expectType<string>(chalk.bgCyan`foo`);
|
||||
expectType<string>(chalk.bgWhite`foo`);
|
||||
expectType<string>(chalk.bgBlackBright`foo`);
|
||||
expectType<string>(chalk.bgRedBright`foo`);
|
||||
expectType<string>(chalk.bgGreenBright`foo`);
|
||||
expectType<string>(chalk.bgYellowBright`foo`);
|
||||
expectType<string>(chalk.bgBlueBright`foo`);
|
||||
expectType<string>(chalk.bgMagentaBright`foo`);
|
||||
expectType<string>(chalk.bgCyanBright`foo`);
|
||||
expectType<string>(chalk.bgWhiteBright`foo`);
|
||||
|
||||
// -- Complex --
|
||||
expectType<string>(chalk.red.bgGreen.underline('foo'));
|
||||
expectType<string>(chalk.underline.red.bgGreen('foo'));
|
||||
|
||||
// -- Complex template literal --
|
||||
expectType<string>(chalk.underline``);
|
||||
expectType<string>(chalk.red.bgGreen.bold`Hello {italic.blue ${name}}`);
|
||||
expectType<string>(chalk.strikethrough.cyanBright.bgBlack`Works with {reset {bold numbers}} {bold.red ${1}}`);
|
||||
|
||||
// -- Color types ==
|
||||
expectAssignable<Color>('red');
|
||||
expectError<Color>('hotpink');
|
||||
|
|
@ -13,7 +13,7 @@ const ESCAPES = new Map([
|
|||
['0', '\0'],
|
||||
['\\', '\\'],
|
||||
['e', '\u001B'],
|
||||
['a', '\u0007']
|
||||
['a', '\u0007'],
|
||||
]);
|
||||
|
||||
function unescape(c) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue