Add support for ansi
Signed-off-by: Richie Bendall <richiebendall@gmail.com>
This commit is contained in:
parent
88f323fb16
commit
d97dd528de
6 changed files with 37 additions and 5 deletions
16
index.d.ts
vendored
16
index.d.ts
vendored
|
|
@ -183,6 +183,14 @@ export interface ChalkInstance extends ChalkFunction {
|
||||||
*/
|
*/
|
||||||
hex: (color: string) => this;
|
hex: (color: string) => 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) => this;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
|
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set text color.
|
||||||
*/
|
*/
|
||||||
|
|
@ -207,6 +215,14 @@ export interface ChalkInstance extends ChalkFunction {
|
||||||
*/
|
*/
|
||||||
bgHex: (color: string) => this;
|
bgHex: (color: string) => 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.
|
||||||
|
30 <= code && code < 38 || 90 <= code && code < 98
|
||||||
|
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) => this;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
|
Use a [8-bit unsigned number](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) to set background color.
|
||||||
*/
|
*/
|
||||||
|
|
|
||||||
|
|
@ -43,9 +43,11 @@ expectType<string>(chalk`Works with numbers {bold.red ${1}}`);
|
||||||
// -- Color methods --
|
// -- Color methods --
|
||||||
expectAssignable<colorReturn>(chalk.rgb(0, 0, 0));
|
expectAssignable<colorReturn>(chalk.rgb(0, 0, 0));
|
||||||
expectAssignable<colorReturn>(chalk.hex('#DEADED'));
|
expectAssignable<colorReturn>(chalk.hex('#DEADED'));
|
||||||
|
expectAssignable<colorReturn>(chalk.ansi(30));
|
||||||
expectAssignable<colorReturn>(chalk.ansi256(0));
|
expectAssignable<colorReturn>(chalk.ansi256(0));
|
||||||
expectAssignable<colorReturn>(chalk.bgRgb(0, 0, 0));
|
expectAssignable<colorReturn>(chalk.bgRgb(0, 0, 0));
|
||||||
expectAssignable<colorReturn>(chalk.bgHex('#DEADED'));
|
expectAssignable<colorReturn>(chalk.bgHex('#DEADED'));
|
||||||
|
expectAssignable<colorReturn>(chalk.bgAnsi(30));
|
||||||
expectAssignable<colorReturn>(chalk.bgAnsi256(0));
|
expectAssignable<colorReturn>(chalk.bgAnsi256(0));
|
||||||
|
|
||||||
// -- Modifiers --
|
// -- Modifiers --
|
||||||
|
|
|
||||||
|
|
@ -42,7 +42,7 @@
|
||||||
"text"
|
"text"
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"ansi-styles": "^6.0.0",
|
"ansi-styles": "^6.1.0",
|
||||||
"supports-color": "^9.0.0"
|
"supports-color": "^9.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|
|
||||||
|
|
@ -298,6 +298,7 @@ The following color models can be used:
|
||||||
|
|
||||||
- [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
|
- [`rgb`](https://en.wikipedia.org/wiki/RGB_color_model) - Example: `chalk.rgb(255, 136, 0).bold('Orange!')`
|
||||||
- [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
|
- [`hex`](https://en.wikipedia.org/wiki/Web_colors#Hex_triplet) - Example: `chalk.hex('#FF8800').bold('Orange!')`
|
||||||
|
- [`ansi`](https://en.wikipedia.org/wiki/ANSI_escape_code#3/4_bit) - Example: `chalk.ansi(31).bgAnsi(93)('red on yellowBright')`
|
||||||
- [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
|
- [`ansi256`](https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit) - Example: `chalk.bgAnsi256(194)('Honeydew, more or less')`
|
||||||
|
|
||||||
## Browser support
|
## Browser support
|
||||||
|
|
|
||||||
|
|
@ -11,8 +11,8 @@ const {isArray} = Array;
|
||||||
|
|
||||||
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
// `supportsColor.level` → `ansiStyles.color[name]` mapping
|
||||||
const levelMapping = [
|
const levelMapping = [
|
||||||
'ansi256',
|
'ansi',
|
||||||
'ansi256',
|
'ansi',
|
||||||
'ansi256',
|
'ansi256',
|
||||||
'ansi16m'
|
'ansi16m'
|
||||||
];
|
];
|
||||||
|
|
@ -74,7 +74,15 @@ styles.visible = {
|
||||||
|
|
||||||
const getModelAnsi = (model, level, type, ...arguments_) => {
|
const getModelAnsi = (model, level, type, ...arguments_) => {
|
||||||
if (model === 'rgb') {
|
if (model === 'rgb') {
|
||||||
return level === 'ansi16m' ? ansiStyles[type].ansi16m(...arguments_) : ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
|
if (level === 'ansi16m') {
|
||||||
|
return ansiStyles[type].ansi16m(...arguments_);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level === 'ansi256') {
|
||||||
|
return ansiStyles[type].ansi256(ansiStyles.rgbToAnsi256(...arguments_));
|
||||||
|
}
|
||||||
|
|
||||||
|
return ansiStyles[type].ansi(ansiStyles.rgbToAnsi(...arguments_));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (model === 'hex') {
|
if (model === 'hex') {
|
||||||
|
|
@ -84,7 +92,7 @@ const getModelAnsi = (model, level, type, ...arguments_) => {
|
||||||
return ansiStyles[type](...arguments_);
|
return ansiStyles[type](...arguments_);
|
||||||
};
|
};
|
||||||
|
|
||||||
const usedModels = ['rgb', 'hex', 'ansi256'];
|
const usedModels = ['rgb', 'hex', 'ansi256', 'ansi'];
|
||||||
|
|
||||||
for (const model of usedModels) {
|
for (const model of usedModels) {
|
||||||
styles[model] = {
|
styles[model] = {
|
||||||
|
|
|
||||||
|
|
@ -93,6 +93,11 @@ test('line breaks should open and close colors with CRLF', t => {
|
||||||
t.is(chalk.grey('hello\r\nworld'), '\u001B[90mhello\u001B[39m\r\n\u001B[90mworld\u001B[39m');
|
t.is(chalk.grey('hello\r\nworld'), '\u001B[90mhello\u001B[39m\r\n\u001B[90mworld\u001B[39m');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('properly convert RGB to 16 colors on basic color terminals', t => {
|
||||||
|
t.is(new Chalk({level: 1}).hex('#FF0000')('hello'), '\u001B[91mhello\u001B[39m');
|
||||||
|
t.is(new Chalk({level: 1}).bgHex('#FF0000')('hello'), '\u001B[101mhello\u001B[49m');
|
||||||
|
});
|
||||||
|
|
||||||
test('properly convert RGB to 256 colors on basic color terminals', t => {
|
test('properly convert RGB to 256 colors on basic color terminals', t => {
|
||||||
t.is(new Chalk({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
|
t.is(new Chalk({level: 2}).hex('#FF0000')('hello'), '\u001B[38;5;196mhello\u001B[39m');
|
||||||
t.is(new Chalk({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
|
t.is(new Chalk({level: 2}).bgHex('#FF0000')('hello'), '\u001B[48;5;196mhello\u001B[49m');
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue