Skip to main content

🖍️ Crayon.js for 🦕 Deno


📚 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

If you don’t need all the features but still like crayon.js checkout @crayon.js/lite

Installation

npm install crayon.js #yarn add crayon.js

Usage

import crayon from 'https://deno.land/x/crayon/src/index.ts'

// 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, 0-255) (0-360, 0-100, 0-100) #000000 - #FFFFFF 0-7 0-15 0-255 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 / overlapped depending on situation.


:package: Feature set

Features
Feature crayon.js chalk ansi-colors kleur
Color fallback (conversion/detection) :heavy_check_mark: :heavy_check_mark: (missing 8->4bit) :heavy_multiplication_x: (only using external libs) :heavy_multiplication_x: (only using external libs)
Atrributes (Modifiers) :heavy_check_mark: :heavy_check_mark: :heavy_check_mark: :heavy_check_mark:
4bit (16) :heavy_check_mark: :heavy_check_mark: :heavy_check_mark: :heavy_check_mark:
8bit (HighColor) :heavy_check_mark: :heavy_check_mark: :heavy_multiplication_x: :heavy_multiplication_x:
24bit (TrueColor) :heavy_check_mark: :heavy_check_mark: :heavy_multiplication_x: :heavy_multiplication_x:
Doesn’t extend prototype :heavy_check_mark: :heavy_check_mark: :heavy_check_mark: :heavy_check_mark:
Nested styling :heavy_check_mark: :heavy_check_mark: :heavy_check_mark: :heavy_check_mark:
Full Typescript/
Autocompletion support
:heavy_check_mark: :heavy_multiplication_x: :heavy_multiplication_x: :heavy_multiplication_x:

⚡ Performance

Methodology:

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

Access and render performance have been measured using this script.

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

Results:

todo

:handshake: Contributing

Feel free to fork, add commits and pull requests

📝 Licensing

This project is available under MIT License conditions.