Skip to content

JuMastro/chromatic-console

Repository files navigation

chromatic-console

🌈 Make your node dev logs rainbow... :)

DEVELOPMENT ONLY

Getting Started

Install with npm:

npm install -D chromatic-console

The minimum version of Node to use chromatic-console is v12.0.0.

Examples

const Chromatic = require('chromatic-console')

// With default options
const chromatic = new Chromatic({
  adds: {},
  stylizer: null,
  replace: false,
  stdout: null,
  stderr: null,
  levels: {
    error: ['err', 'red'],
    warn: ['out', 'bright.yellow'],
    info: ['out', 'bright'],
    log: null, // stdout by default
    debug: null
  }
})

// Create your own colors
const chromatic = new Chromatic({
  adds: { custom: [140, 70, 200] },
  levels: {
    custom: ['out', 'bright.custom']
  }
})

chromatic.log('HelloWorld')
chromatic.bright('HelloWorld')
chromatic.custom('HelloWorld')

Options

The Chromatic options object is a wrap of Stylizer & Consilizer options.

Stylizer options:

Stylizer constructor argument is an object that may contains:

  • adds: An object that may include new colors.

Consolizer options:

Consolizer constructor argument is an object that may contains:

  • stylizer: A custom stylizer to provide (default: null).
  • replace: A state to define if it's replace the global console (default: false).
  • stdout: A Stream to send std outputs (default: process.stdout).
  • stderr: A Stream to send err outputs (default: process.stderr).
  • levels: An object that represent the provided levels to consolizer.
    • error: The error def (default: ['err', red]).
    • warn: The warn def (default: ['out', 'bright.yellow']).
    • info: The info def (default: ['out', 'bright']).
    • log: The log def (default: null).
    • debug: The debug def (default: null).

API

Chromatic

Chromatic inherit builded methods from Consolizer to logs object, levels and multiple styles.

Stylizer

  • addColor(name, [rgb]) Add a color to the instance.
  • removeColor(name) Remove a color from the instance.
  • pipe(...styles) Create a custom style from a pipe of styles.
  • createInspector(opts) Create a custom object inspector.
  • createSet(raw, flat) Create set of usables from Stylizer. If raw options is true, the return is a string formatable style, else is a function that wrap the style.
  • stylize(...items) Apply style to a list of items.
  • stylizeObject(obj) Apply a inspected style to an object.

Consolizer

  • buildLogsMethods() Create a logs methods for the stylizer registered styles.
  • logObject(obj) Log an object with the inspector format.
  • `getConsoleMethods() Return a instance logs std methods.

More examples

const chromatic = new Chromatic()

chromatic.yellow('I m yellow')

chromatic.bgyellow('I have a yellow background')

chromatic.bright('I m bright')

chromatic.log('By default I m stdout')

chromatic.error('By default I m red stderr')

chromatic.logObject(aDeepObject)

Create pipe style

const chromatic = new Chromatic({ adds: { custom: [0, 150, 0] } })
const stylizer = chromatic.getStylizer() // => get chromatic._stylizer
const styles = stylizer.styles
const custom = stylizer.pipe([
  styles.modifiers.bright,
  styles.foregrounds.custom
])

chromatic.log(custom('HelloWorld'))

Full custom

const stylizer = new Chromatic.Stylizer({
  adds: {
    custom1: [150, 70, 200]
  }
})

// Other way to add color.
stylizer.addColor('custom2', [200, 0, 100])

const chromatic = new Chromatic({ stylizer })

chromatic.log('HelloWorld')
chromatic.bright('HelloWorld')
chromatic.custom1('HelloWorld')
chromatic.custom2('HelloWorld')

Styles

Formats

  • bright
  • dim
  • underline
  • blink
  • hidden

Default colors

  • black
  • blue
  • cyan
  • green
  • orange
  • purple
  • red
  • white
  • yellow

Dev dependencies

  • Jest Delightful JavaScript Testing.
  • Eslint Javascript linter.