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.jsUsage
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 |