JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q31297F
  • License MIT

Terminal coloring done fast

Package Exports

  • crayon.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (crayon.js) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

๐Ÿ–๏ธ Crayon.js


๐Ÿ“š About

Crayon.js is one of the best1 performing terminal styling. Additionaly written in TypeScript.

Instead of straight up saying "use it, it's better" check yourself what meets your requirements.

๐Ÿ“– TL;DR

๐Ÿ–๏ธ crayon.js has:

  • โšก High performance
  • ๐Ÿ“ฆ No dependencies
  • โฑ๏ธ Low import times
  • ๐Ÿฆ„ Automatic color support & fallbacking
  • ๐Ÿ”— Supported nesting & chaining
  • ๐ŸŒˆ 8bit (256) and 24bit (16.7m) color support
  • ๐ŸŒŸ Emojis, really just bcs of that you should star this repo

Installation

npm install crayon.js #yarn add crayon.js

Usage

const crayon = require('crayon.js')
/* In TypeScript you can do: 
 * import * as crayon from 'crayon.js'
 * import crayon = require('crayon.js')
 */

// Chainable API
console.log(crayon.bgYellow.black('Hello!'))

/* Notice how you can use CSS Keywords directives directly for colors.
 * If terminal does not support 24bit colors it will fallback to 8/4/3 bit or none
 * colors depending on which one is supported
 */
console.log(crayon.bgKhaki.orange('World!'))

/* Crayon.js supports also using function api
 * It's not as elegant but removes a bit of overhead
 * And I'm looking forward to improve it
 */
console.log(crayon.keyword('bgKhaki').keyword('orange')('The same world!'))

/* Crayon.js allows you to not only apply colors
 * but also attributes (named modifiers by some other libs)
 * Of course you can mix them with colors if you want
 */
console.log(crayon.bold(`Isn't this text kinda thicc`))

/* Styles (this means both attributes and colors)
 * can be nested together in a string
 */
console.log(`${crayon.blue.bold(`wow ${crayon.red('that')}`)} ${crayon.yellow('is kinda cool')}`)

/* You can cache style, chalk names it "theme".
 * Remember to call main crayon instance as function to create theme.
 * If you're using the same style X times it's
 * worth to cache it as it improves performance.
 * modifying cached style will result it to change everywhere
 * and cached style can't be reset.
 */
const error = crayon().bgYellow.red
console.log(error('this is error!'))
console.log(error('this will also have the same style applied!!'))

API

crayon.<[...style](value*) | ()[...style](value*)>

* - it has to be able to be converted to string otherwise it'll throw an error

// Examples:
crayon.bgRed.olive('Welcome')
crayon().bgRed.olive('This will work too just fine')
/* Style order don't matter, if they overlap
 * they'll be overwritten using last typed one.
 * However crayon doesn't clean up unused styles.
 * This means expression below will return false.
 */
crayon.red.blue('this will be blue') === crayon.blue('this will be blue')

crayon.colorSupport

interface ColorSupport {
    /** 24bit (16.7m) color palette */
    trueColor: boolean
    /** 8bit (256) color palette */
    highColor: boolean
    /** 4bit (16) color palette */
    fourBitColor: boolean
    /** 3bit (8) color palette */
    threeBitColor: boolean
}

Crayon should detect supported terminal colors but if it does not you can overwrite them.
Color support settings are defined globally, so if you change it in main crayon object it'll also affect other crayon instances

Crayon supports NO_COLOR env variable, this means that if you have set one to true-ish value it'll stop displaying colors.

Supported attributes

Crayon supports most of these displayed here.
To see exactly which ones you can go here and check attributes variable.

Color names

All 4bit colors have their own name Syntax of naming colors is

[bg][light | Light]<baseColorName | BaseColorName>

for example: red, lightRed, bgRed, bgLightRed

Available base color names:

black red green yellow
blue magenta cyan white

Functions

crayon.<...function(...arguments)>

crayon.$functions.<...function(...arguments)>

Color Functions

function rgb (red,green,blue) hsl(hue,saturation,lightness) hex ansi3 ansi4 ansi8 keyword
syntax (0-255, 0-255, number) (0-360, 0-100, 0-100) #000000 - #FFFFFF 0-7 0-15 0-255 css keyword

Misc Functions

function strip
syntax (string)
description returns text with no styling

If numerical arguments are out of given range they get automatically clamped.


1 - More on that

X packages define themself as the fastest one, however most of them just lie.

Kleur and ansi-colors advertise themself as "The fastest Node.js library for terminal styling".
However in later tests they fall far behind chalk, which is probably the most popular package for that right now.

Don't get me wrong, I have nothing to the authors of them but they either provide outdated info (chalk 3.0 received very big performance boost) or straight lie.

๐Ÿ“ฆ Feature set

Features
Feature crayon.js chalk ansi-colors kleur
Color fallback (conversion/detection) โœ”๏ธ โœ”๏ธ (missing 8->4bit) โœ–๏ธ (only using external libs) โœ–๏ธ (only using external libs)
Atrributes (Modifiers) โœ”๏ธ โœ”๏ธ โœ”๏ธ โœ”๏ธ
4bit (16) โœ”๏ธ โœ”๏ธ โœ”๏ธ โœ”๏ธ
8bit (HighColor) โœ”๏ธ โœ”๏ธ โœ–๏ธ โœ–๏ธ
24bit (TrueColor) โœ”๏ธ โœ”๏ธ โœ–๏ธ โœ–๏ธ
Doesn't extend prototype โœ”๏ธ โœ”๏ธ โœ”๏ธ โœ”๏ธ
Nested styling โœ”๏ธ โœ”๏ธ โœ”๏ธ โœ”๏ธ
Full Typescript/
Autocompletion support
โœ”๏ธ โœ–๏ธ โœ–๏ธ โœ–๏ธ

โšก Performance

Methodology:

All tests were done on my PC which is not in any way fast.

Require times have been measured using this script.
Access and render performance have been measured using this script.

Best performing subject (ยฑ 10%) has been marked with bold font

Results:
Test subject โฑ๏ธ Require times (ms) ๐Ÿงช Access time (kops) ๐Ÿ–๏ธ Render test (kops)
crayon.js (chain) 6.6 ยฑ0.13 246 18
crayon.js (func) 450 20
crayon.js (cached) 4000 22
chalk (chain) 8.8 ยฑ1.22 3333 16
chalk (cached) 4000 21
ansi-colors (chain) 6.9 ยฑ0.60 199 14
ansi-colors (cached) 788 16
kleur 6.1 ยฑ0.05 495 15

๐Ÿค Contributing

Feel free to fork, add commits and pull requests

๐Ÿ“ Licensing

This project is available under MIT License conditions.