GithubHelp home page GithubHelp logo

jonschlinkert / ansi-colors Goto Github PK

View Code? Open in Web Editor NEW

This project forked from doowb/ansi-colors

0.0 2.0 0.0 37 KB

Collection of ansi colors and styles.

License: MIT License

JavaScript 94.25% TypeScript 5.75%

ansi-colors's Introduction

ansi-colors NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Collection of ansi colors and styles.

Please consider following this project's author, Brian Woodward, and consider starring the project to show your ❤️ and support.

Install

Install with npm:

$ npm install --save ansi-colors

ansi-colors is a Node.js library for adding colors to text in the terminal. A more performant drop-in replacement for chalk, with no dependencies.

image

Why use this?

See a comparison to other libraries

Usage

const c = require('ansi-colors');

console.log(c.red('This is a red string!'));
console.log(c.green('This is a red string!'));
console.log(c.cyan('This is a cyan string!'));
console.log(c.yellow('This is a yellow string!'));

image

Features

Colors take multiple arguments.

console.log(c.red('Some', 'red', 'text', 'to', 'display'));

Chained styles

Supports chained styles.

console.log(c.bold.red('this is a bold red message'));
console.log(c.bold.yellow.italic('this is a bold yellow italicized message'));
console.log(c.green.bold.underline('this is a bold green underlined message'));

image

Nested styles

Supports nested styles.

// using template literals
console.log(c.yellow(`foo ${c.red.bold('red')} bar ${c.cyan('cyan')} baz`));

// or as arguments
console.log(c.yellow('foo', c.red.bold('red'), 'bar', c.cyan('cyan'), 'baz'));

image

Conditional color support

Easily enable/disable colors.

const c = require('ansi-colors');

// disable colors manually
c.enabled = false;

// or use a library to automatically detect support
c.enabled = require('color-support').stdout;

console.log(c.red('I will only be colored red if the terminal supports colors'));

printf-like formatting

Uses node's built-in util.format() to achieve printf-like formatting. The first argument is a string containing zero or more placeholder tokens. Each placeholder token is replaced with the converted value from the corresponding argument.

console.log(c.bold.red('%s:%s', 'foo', 'bar', 'baz'));

image

Even works with nested colors!

console.log(c.bold.bold('%s:%s:%s', 'foo', c.red('bar'), 'baz'));

image

Strip ANSI codes

Use the .unstyle method to manually strip ANSI codes from a string.

console.log(c.unstyle(c.blue.bold('foo bar baz')));
//=> 'foo bar baz'

Available styles

Note that bright and bright-background colors are not always supported.

Colors

  • black
  • blue
  • cyan
  • gray (U.S.) and grey (everyone else)
  • green
  • magenta
  • red
  • white
  • yellow

Bright colors

  • blueBright
  • cyanBright
  • greenBright
  • magentaBright
  • redBright
  • whiteBright
  • yellowBright

Background colors

  • bgBlack
  • bgBlue
  • bgCyan
  • bgGreen
  • bgMagenta
  • bgRed
  • bgWhite
  • bgYellow

Bright background colors

  • bgBlackBright
  • bgBlueBright
  • bgCyanBright
  • bgGreenBright
  • bgMagentaBright
  • bgRedBright
  • bgWhiteBright
  • bgYellowBright

Modifiers

  • bold

  • dim

  • hidden

  • inverse

  • italic (Not widely supported)

  • reset

  • strikethrough (Not widely supported)

  • underline

Symbols

A handful of common useful symbols are available on the c.symbols property.

console.log(c.symbols);

Available symbols

Windows

  • check:
  • cross: ×
  • ellipsis: '...
  • info: i
  • line:
  • pointer: '>
  • pointerSmall: »
  • question: ?
  • questionSmall:
  • warning:

Other platforms

  • check:
  • cross:
  • ellipsis:
  • info:
  • line:
  • pointer:
  • pointerSmall:
  • question: ?
  • questionSmall:
  • warning:

Benchmarks

MacBook Pro, Intel Core i7, 2.5 GHz, 16 GB.

Load time

Time it takes to load the module the first time:

ansi-colors: 2.057ms
chalk: 9.063ms
clorox: 1.701ms

Performance

# All Colors
  ansi-colors x 93,508 ops/sec ±1.19% (88 runs sampled)
  chalk x 8,871 ops/sec ±2.33% (81 runs sampled)
  clorox x 1,401 ops/sec ±2.62% (77 runs sampled)

# Stacked colors
  ansi-colors x 14,413 ops/sec ±1.35% (88 runs sampled)
  chalk x 1,824 ops/sec ±2.46% (79 runs sampled)
  clorox x 563 ops/sec ±2.83% (75 runs sampled)

# Nested colors
  ansi-colors x 37,897 ops/sec ±1.02% (92 runs sampled)
  chalk x 4,196 ops/sec ±1.88% (81 runs sampled)
  clorox x 676 ops/sec ±2.70% (69 runs sampled)

Comparison

Feature ansi-colors chalk clorox colors
Nested colors yes yes no yes
Chained colors yes yes You must call .toString() on result yes
Toggle color support yes yes no yes
printf-like formatting yes no no no
Includes symbols yes no no no

About

Contributing

Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Running Tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test
Building docs

(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Related projects

You might also be interested in these projects:

Contributors

Commits Contributor
14 doowb
3 jonschlinkert
2 Silic0nS0ldier

Author

Brian Woodward

License

Copyright © 2018, Brian Woodward. Released under the MIT License.


This file was generated by verb-generate-readme, v0.6.0, on May 04, 2018.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.