ansis

ANSI color styling for text in terminal.

remove beta version
Last updated 3 years ago by webdiscus .
ISC · Repository · Bugs · Original npm · Tarball · package.json
$ npm install ansis 
SYNC missed versions from official npm registry.

ansis
ANSI Styling


npm codecov node

This is a Node.js library for coloring text in terminal.
The ansis use the SGR (Select Graphic Rendition) codes defined in the ECMA-48 standard.
Why yet one lib? See comparison of most popular ANSI colors libraries and benchmark.

ansis

Install

npm install ansis

Quick Start

import ansis from 'ansis'; // ESM
// OR
const ansis = require('ansis'); // CommonJS

console.log(ansis.green(`Hello ${ansis.inverse('ANSI')} World!`));
console.log(ansis.black.bgYellow(`Warning:`) + ansis.cyan(' /path/to/file.js ') + ansis.red(`not found!`) );

Output:
output

Features

  • supports both ESM and CommonJS
  • standard API compatible with many popular ANSI color libraries
  • chained syntax
  • nested syntax
  • ANSI 256 colors and Truecolor
  • detects color support
  • supports the environment variables NO_COLOR FORCE_COLOR and flags --no-color --color
  • low level access to the open and close properties for each style
  • correct break of style at end of line
  • faster than many other libraries, see benchmarks
  • distributed unpacked size of code smaller than 4 KB
  • zero dependency
  • String.prototype stays virgin

Chained syntax

ansis.underline.italic.bold.red('Styled text');

Nested syntax

const c = ansis;
c.red(`${c.bold(`${c.italic(`${c.underline('underline')}italic`)}bold`)}red`);

Styles

reset inverse hidden visible bold dim(aliasfaint) italic underline doubleUnderline overline strikethrough(alias strike) frame encircle

Foreground colors

black red green blue magenta cyan white gray (alias grey) blackBright redBright greenBright yellowBright blueBright magentaBright cyanBright whiteBright

Background colors

bgBlack bgRed bgGreen bgYellow bgBlue bgMagenta bgCyan bgWhite bgGray bgBlackBright bgRedBright bgGreenBright bgYellowBright bgBlueBright bgMagentaBright bgCyanBright bgWhiteBright

ANSI 256 colors

Foreground: .ansi256(code) has aliases .ansi(code) and .fg(code)
Background: .bgAnsi256(code) has aliases .bgAnsi(code) and .bg(code)

The pre-defined set of 256 colors.

Code range Description
0 - 7 standard colors
8 - 15 bright colors
16 - 231 6 × 6 × 6 cube (216 colors)
232 - 255 grayscale from black to white in 24 steps

See ANSI color codes.

// foreground color
ansis.ansi256(96).bold('bold Bright Cyan');
ansis.fg(96).bold('bold Bright Cyan'); // `fg` is the short alias for `ansi256`

// background color
ansis.bgAnsi256(105)('Bright Magenta');
ansis.bg(105)('Bright Magenta'); // `bg` is the short alias for `bgAnsi256`

The ansi256() and bgAnsi256() methods are implemented for compatibility with the chalk API.

Truecolor

Foreground: hex rgb
Background: bgHex bgRgb

// foreground color
ansis.hex('#E0115F').bold('bold Ruby');
ansis.hex('#96C')('Amethyst');
ansis.rgb(224, 17, 95).italic.underline('italic underline Ruby');

// background color
ansis.bgHex('#E0115F')('Ruby');
ansis.bgHex('#96C')('Amethyst');
ansis.bgRgb(224, 17, 95)('Ruby');

Shortcuts

const theme = {
    error: ansis.red.bold,
    info: ansis.cyan.italic,
    warning: ansis.black.bgYellowBright,
    ruby: ansis.hex('#E0115F'),
};

theme.error('error');
theme.info('info');
theme.warning('warning');
theme.ruby('Ruby color');

Low level usage

You can use the open and close properties for each style.

const myStyle = ansis.bold.italic.black.bgHex('#ABCDEF');
console.log(`Hello ${myStyle.open}ANSI${myStyle.close} World!`);

Correct break of style at end of line

ansis.bgGreen(`\nAnsis\nNew Line\nNext New Line\n`);

output

Comparison of most popular libraries

Library Standard
style / color
naming
Chained
styles
Nested
styles
New
Line
ANSI 256
colors
methods
Truecolor
methods
Supports
NO_COLOR
colors.js no, e.g.
brightRed
(16 colors)
yes yes yes - - only
FORCE_COLOR
--no-color
--color
colorette yes
(16 colors)
- yes - - - yes
picocolors yes
(8 colors)
- yes - - - yes
cli-color yes
(16 colors)
yes yes - .xterm(num) - yes
color-cli no, e.g.
red_bbt
(16 colors)
yes buggy yes .x<num> - only
--no-color
--color
ansi-colors yes
(16 colors)
yes yes yes - - only
FORCE_COLOR
kleur yes
(8 colors)
yes* yes - - - yes
chalk yes
(16 colors)
yes yes yes .ansi256(num) .hex() .rgb() yes
ansis yes
(16 colors)
yes yes yes .ansi256(num)
.ansi(num)
.fg(num)
.hex() .rgb() yes

Column description

  • Standard style and color naming: red redBright bgRed bgRedBright etc., see above the Foreground / Background colors.
  • Chain styles: ansis.red.bold.underline('text').
    kleur use the chain of functions: kleur.red().bold().underline('text').
  • Nested styles: correct closing of nested escape sequences.
    c.red(`red ${c.green(`green ${c.underline(`underline`)} green`)} red`)
    
  • New Line: correct break of escape sequences at end of line.
    ansis.bgGreen(`\nAnsis\nNew Line\nNext New Line\n`);
    
  • NO_COLOR: supports the environment variables NO_COLOR FORCE_COLOR and flags --no-color --color

Show ANSI demo

git clone https://github.com/webdiscus/ansis.git
cd ./ansis
npm i
npm run demo

Benchmark

Setup

git clone https://github.com/webdiscus/ansis.git
cd ./ansis/bench
npm i

Run benchmark

npm run bench

Tested on

MacBook Pro 16" M1 Max 64GB
macOS Monterey 12.1
Node.js v16.13.1
Terminal iTerm2

Colorette bench

The benchmark used in colorette.

c.red(`${c.bold(`${c.cyan(`${c.yellow('yellow')}cyan`)}`)}red`);
  colors.js           1,158,572 ops/sec
  colorette           4,572,582 ops/sec
  picocolors          3,841,124 ops/sec
  cli-color             470,320 ops/sec
  color-cli             109,811 ops/sec
  ansi-colors         1,265,615 ops/sec
  kleur/colors        2,281,415 ops/sec
  kleur               2,228,639 ops/sec
  chalk               2,287,146 ops/sec
+ ansis               2,669,734 ops/sec

Base colors

const colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'];
colors.forEach((color) => c[color]('foo'));
  colors.js             640,101 ops/sec
  colorette           1,874,506 ops/sec
  picocolors          8,265,628 ops/sec
  cli-color             305,690 ops/sec
  color-cli             104,962 ops/sec
  ansi-colors         1,010,628 ops/sec
  kleur/colors        2,074,111 ops/sec
  kleur               5,455,121 ops/sec
  chalk               4,428,884 ops/sec
+ ansis               6,197,754 ops/sec

Chained styles

colors.forEach((color) => c[color].bold.underline.italic('foo'));
  colors.js             138,219 ops/sec
  colorette     (not supported)
  picocolors    (not supported)
  cli-color             144,837 ops/sec
  color-cli              52,732 ops/sec
  ansi-colors           158,921 ops/sec
  kleur/colors  (not supported)
  kleur                 514,035 ops/sec
  chalk               1,234,573 ops/sec
+ ansis               5,515,868 ops/sec

Nested calls

colors.forEach((color) => c[color](c.bold(c.underline(c.italic('foo')))));
  colors.js             166,425 ops/sec
  colorette             695,350 ops/sec
  picocolors            942,592 ops/sec
  cli-color              65,561 ops/sec
  color-cli              13,800 ops/sec
  ansi-colors           260,316 ops/sec
  kleur/colors          561,111 ops/sec
  kleur                 648,195 ops/sec
  chalk                 497,292 ops/sec
+ ansis                 558,575 ops/sec

Nested styles

c.red(`a red ${c.white('white')} red ${c.red('red')} red ${c.cyan('cyan')} red ${c.black('black')} red ${c.red('red')} red ${c.green('green')} red ${c.red('red')} red ${c.yellow('yellow')} red ${c.blue('blue')} red ${c.red('red')} red ${c.magenta('magenta')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.green('green')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.magenta('magenta')} red ${c.red('red')} red ${c.red('red')} red ${c.cyan('cyan')} red ${c.red('red')} red ${c.red('red')} red ${c.yellow('yellow')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} red ${c.red('red')} message`);
  colors.js              89,633 ops/sec
  colorette             243,139 ops/sec
  picocolors            243,975 ops/sec
  cli-color              41,657 ops/sec
  color-cli              14,264 ops/sec
  ansi-colors           121,451 ops/sec
  kleur/colors          234,132 ops/sec
  kleur                 221,446 ops/sec
  chalk                 189,960 ops/sec
+ ansis                 211,868 ops/sec

Deep nested styles

c.green(
  `green ${c.cyan(
    `cyan ${c.red(
      `red ${c.yellow(
        `yellow ${c.blue(
          `blue ${c.magenta(
            `magenta ${c.underline(
              `underline ${c.italic(`italic`)} underline`
            )} magenta`
          )} blue`
        )} yellow`
      )} red`
    )} cyan`
  )} green`
);
  colors.js             451,592 ops/sec
  colorette           1,131,757 ops/sec
  picocolors          1,002,649 ops/sec
  cli-color             213,441 ops/sec
  color-cli              40,340 ops/sec
  ansi-colors           362,733 ops/sec
  kleur/colors          478,547 ops/sec
  kleur                 464,004 ops/sec
  chalk                 565,965 ops/sec
+ ansis                 882,220 ops/sec

HEX colors

Only two libraries support truecolors methods: ansis and chalk

c.hex('#FBA')('foo');
  colors.js             (not supported)
  colorette             (not supported)
  picocolors            (not supported)
  cli-color             (not supported)
  color-cli             (not supported)
  ansi-colors           (not supported)
  kleur/colors          (not supported)
  kleur                 (not supported)
  chalk               2,891,684 ops/sec
+ ansis               4,944,572 ops/sec

Testing

npm run test will run the unit and integration tests.
npm run test:coverage will run the tests with coverage.

Also See

Most popular ANSI libraries for Node.js:

License

ISC

Current Tags

  • 1.6.0-beta.0                                ...           beta (a year ago)
  • 3.3.2                                ...           latest (4 months ago)

43 Versions

  • 3.3.2                                ...           4 months ago
  • 3.3.1                                ...           4 months ago
  • 3.3.0                                ...           4 months ago
  • 3.2.1                                ...           4 months ago
  • 3.2.0                                ...           7 months ago
  • 3.1.1                                ...           7 months ago
  • 3.1.0                                ...           7 months ago
  • 3.0.3                                ...           7 months ago
  • 3.0.2                                ...           7 months ago
  • 3.0.1                                ...           7 months ago
  • 3.0.0                                ...           8 months ago
  • 2.3.0                                ...           9 months ago
  • 2.2.0                                ...           9 months ago
  • 2.1.0                                ...           10 months ago
  • 2.0.3                                ...           a year ago
  • 2.0.2                                ...           a year ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.6.0-beta.0                                ...           a year ago
  • 1.5.6                                ...           a year ago
  • 1.5.5                                ...           2 years ago
  • 1.5.4                                ...           2 years ago
  • 1.5.3                                ...           2 years ago
  • 1.5.2                                ...           2 years ago
  • 1.5.1                                ...           2 years ago
  • 1.5.0 [deprecated]           ...           2 years ago
  • 1.5.0-alpha.0                                ...           2 years ago
  • 1.4.0                                ...           2 years ago
  • 1.3.6                                ...           3 years ago
  • 1.3.5 [deprecated]           ...           3 years ago
  • 1.3.4                                ...           3 years ago
  • 1.3.4-alpha.1 [deprecated]           ...           3 years ago
  • 1.3.4-alpha.0 [deprecated]           ...           3 years ago
  • 1.3.3                                ...           3 years ago
  • 1.3.2                                ...           3 years ago
  • 1.3.1                                ...           3 years ago
  • 1.3.0                                ...           3 years ago
  • 1.2.2                                ...           3 years ago
  • 1.2.1                                ...           3 years ago
  • 1.2.0                                ...           3 years ago
  • 1.1.1                                ...           3 years ago
  • 1.1.0                                ...           3 years ago
  • 1.0.0                                ...           3 years ago
Maintainers (1)
Downloads
Total 1
Today 1
This Week 1
This Month 1
Last Day 0
Last Week 0
Last Month 0
Dependencies (0)
None
Dev Dependencies (0)
None
Dependents (1)

© 2010 - cnpmjs.org x YWFE | Home | YWFE