Skip to content

Commit

Permalink
Add bright colors
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed Feb 29, 2024
1 parent ab190c8 commit de246c9
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 2 deletions.
3 changes: 3 additions & 0 deletions fixture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {red} from './index.js';

console.log(red('foo'));
16 changes: 16 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,19 @@ export const bgMagenta: Format;
export const bgCyan: Format;
export const bgWhite: Format;
export const bgGray: Format;

export const redBright: Format;
export const greenBright: Format;
export const yellowBright: Format;
export const blueBright: Format;
export const magentaBright: Format;
export const cyanBright: Format;
export const whiteBright: Format;

export const bgRedBright: Format;
export const bgGreenBright: Format;
export const bgYellowBright: Format;
export const bgBlueBright: Format;
export const bgMagentaBright: Format;
export const bgCyanBright: Format;
export const bgWhiteBright: Format;
16 changes: 16 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,19 @@ export const bgMagenta = format(45, 49);
export const bgCyan = format(46, 49);
export const bgWhite = format(47, 49);
export const bgGray = format(100, 49);

export const redBright = format(91, 39);
export const greenBright = format(92, 39);
export const yellowBright = format(93, 39);
export const blueBright = format(94, 39);
export const magentaBright = format(95, 39);
export const cyanBright = format(96, 39);
export const whiteBright = format(97, 39);

export const bgRedBright = format(101, 49);
export const bgGreenBright = format(102, 49);
export const bgYellowBright = format(103, 49);
export const bgBlueBright = format(104, 49);
export const bgMagentaBright = format(105, 49);
export const bgCyanBright = format(106, 49);
export const bgWhiteBright = format(107, 49);
40 changes: 40 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,44 @@
import {expectType} from 'tsd';
import * as colors from './index.js';

expectType<string>(colors.reset('x'));
expectType<string>(colors.bold('x'));
expectType<string>(colors.dim('x'));
expectType<string>(colors.italic('x'));
expectType<string>(colors.underline('x'));
expectType<string>(colors.overline('x'));
expectType<string>(colors.inverse('x'));
expectType<string>(colors.hidden('x'));
expectType<string>(colors.strikethrough('x'));
expectType<string>(colors.black('x'));
expectType<string>(colors.red('x'));
expectType<string>(colors.green('x'));
expectType<string>(colors.yellow('x'));
expectType<string>(colors.blue('x'));
expectType<string>(colors.magenta('x'));
expectType<string>(colors.cyan('x'));
expectType<string>(colors.white('x'));
expectType<string>(colors.gray('x'));
expectType<string>(colors.bgBlack('x'));
expectType<string>(colors.bgRed('x'));
expectType<string>(colors.bgGreen('x'));
expectType<string>(colors.bgYellow('x'));
expectType<string>(colors.bgBlue('x'));
expectType<string>(colors.bgMagenta('x'));
expectType<string>(colors.bgCyan('x'));
expectType<string>(colors.bgWhite('x'));
expectType<string>(colors.bgGray('x'));
expectType<string>(colors.redBright('x'));
expectType<string>(colors.greenBright('x'));
expectType<string>(colors.yellowBright('x'));
expectType<string>(colors.blueBright('x'));
expectType<string>(colors.magentaBright('x'));
expectType<string>(colors.cyanBright('x'));
expectType<string>(colors.whiteBright('x'));
expectType<string>(colors.bgRedBright('x'));
expectType<string>(colors.bgGreenBright('x'));
expectType<string>(colors.bgYellowBright('x'));
expectType<string>(colors.bgBlueBright('x'));
expectType<string>(colors.bgMagentaBright('x'));
expectType<string>(colors.bgCyanBright('x'));
expectType<string>(colors.bgWhiteBright('x'));
14 changes: 14 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ console.log(colors.red('Yo!'));
- `cyan`
- `white`
- `gray`
- `redBright`
- `greenBright`
- `yellowBright`
- `blueBright`
- `magentaBright`
- `cyanBright`
- `whiteBright`

### Background colors

Expand All @@ -65,6 +72,13 @@ console.log(colors.red('Yo!'));
- `bgCyan`
- `bgWhite`
- `bgGray`
- `bgRedBright`
- `bgGreenBright`
- `bgYellowBright`
- `bgBlueBright`
- `bgMagentaBright`
- `bgCyanBright`
- `bgWhiteBright`

## Prior art

Expand Down
56 changes: 54 additions & 2 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
import {execFile} from 'node:child_process';
import {env} from 'node:process';
import {promisify} from 'node:util';
import test from 'ava';
import * as colors from './index.js';

test('main', t => {
t.is(colors.red('foo'), '\u001B[31mfoo\u001B[39m');
const pExecFile = promisify(execFile);

const testColor = (t, methodName, startCode, endCode) => {
t.is(colors[methodName]('foo'), `\u001B[${startCode}mfoo\u001B[${endCode}m`);
};

test('Add color ANSI sequences - reset', testColor, 'reset', 0, 0);
test('Add color ANSI sequences - bold', testColor, 'bold', 1, 22);
test('Add color ANSI sequences - dim', testColor, 'dim', 2, 22);
test('Add color ANSI sequences - italic', testColor, 'italic', 3, 23);
test('Add color ANSI sequences - underline', testColor, 'underline', 4, 24);
test('Add color ANSI sequences - overline', testColor, 'overline', 53, 55);
test('Add color ANSI sequences - inverse', testColor, 'inverse', 7, 27);
test('Add color ANSI sequences - hidden', testColor, 'hidden', 8, 28);
test('Add color ANSI sequences - strikethrough', testColor, 'strikethrough', 9, 29);
test('Add color ANSI sequences - black', testColor, 'black', 30, 39);
test('Add color ANSI sequences - red', testColor, 'red', 31, 39);
test('Add color ANSI sequences - green', testColor, 'green', 32, 39);
test('Add color ANSI sequences - yellow', testColor, 'yellow', 33, 39);
test('Add color ANSI sequences - blue', testColor, 'blue', 34, 39);
test('Add color ANSI sequences - magenta', testColor, 'magenta', 35, 39);
test('Add color ANSI sequences - cyan', testColor, 'cyan', 36, 39);
test('Add color ANSI sequences - white', testColor, 'white', 37, 39);
test('Add color ANSI sequences - gray', testColor, 'gray', 90, 39);
test('Add color ANSI sequences - bgBlack', testColor, 'bgBlack', 40, 49);
test('Add color ANSI sequences - bgRed', testColor, 'bgRed', 41, 49);
test('Add color ANSI sequences - bgGreen', testColor, 'bgGreen', 42, 49);
test('Add color ANSI sequences - bgYellow', testColor, 'bgYellow', 43, 49);
test('Add color ANSI sequences - bgBlue', testColor, 'bgBlue', 44, 49);
test('Add color ANSI sequences - bgMagenta', testColor, 'bgMagenta', 45, 49);
test('Add color ANSI sequences - bgCyan', testColor, 'bgCyan', 46, 49);
test('Add color ANSI sequences - bgWhite', testColor, 'bgWhite', 47, 49);
test('Add color ANSI sequences - bgGray', testColor, 'bgGray', 100, 49);
test('Add color ANSI sequences - redBright', testColor, 'redBright', 91, 39);
test('Add color ANSI sequences - greenBright', testColor, 'greenBright', 92, 39);
test('Add color ANSI sequences - yellowBright', testColor, 'yellowBright', 93, 39);
test('Add color ANSI sequences - blueBright', testColor, 'blueBright', 94, 39);
test('Add color ANSI sequences - magentaBright', testColor, 'magentaBright', 95, 39);
test('Add color ANSI sequences - cyanBright', testColor, 'cyanBright', 96, 39);
test('Add color ANSI sequences - whiteBright', testColor, 'whiteBright', 97, 39);
test('Add color ANSI sequences - bgRedBright', testColor, 'bgRedBright', 101, 49);
test('Add color ANSI sequences - bgGreenBright', testColor, 'bgGreenBright', 102, 49);
test('Add color ANSI sequences - bgYellowBright', testColor, 'bgYellowBright', 103, 49);
test('Add color ANSI sequences - bgBlueBright', testColor, 'bgBlueBright', 104, 49);
test('Add color ANSI sequences - bgMagentaBright', testColor, 'bgMagentaBright', 105, 49);
test('Add color ANSI sequences - bgCyanBright', testColor, 'bgCyanBright', 106, 49);
test('Add color ANSI sequences - bgWhiteBright', testColor, 'bgWhiteBright', 107, 49);

test('Is noop when no colors are supported', async t => {
const {stdout} = await pExecFile('node', ['fixture.js'], {env: {...env, FORCE_COLOR: '0'}});
t.is(stdout, 'foo\n');
});

0 comments on commit de246c9

Please sign in to comment.