Skip to main content
Home
Works with
This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score100%
Downloads4/wk
Published9 months ago (0.2.0)

ANSI code helpers for colorful logging in node.js and chrome.

@braebo/ansi

ANSI code helpers for colorful logging in node.js and chrome.


  • Hex -> True Color
  • Dynamic colorization
  • Gradients
  • TypeScript / ESM
  • Dependency-free
  • Lightweight
  • Tree shakable

Install

# npm
pnpm i -D @braebo/ansi

# jsr
pnpm dlx jsr add @braebo/ansi

Usage

Hex

Create colors from hex values:

import { ansiHex } from '@braebo/ansi'

const coral = ansiHex('#FF7F50')
console.log(coral('This text will be coral colored'))

Style

Create styles from the ansiStyle function:

import { ansiStyle } from '@braebo/ansi'

const bold = ansiStyle('bold')
console.log(bold('This text will be bold'))

Gradient

Interpolate gradients with any number of colors:

import { ansiGradient } from '@braebo/ansi'

// Create a gradient function.
const g = ansiGradient('#38b2db', '#5959b5', '#e84067')

Now that we have a gradient function, we can pass it a string:

console.log(g('Simple gradient text.'))
Gradient string

Or pass it a number to get a color stop:

const fade = `
${g(0.5)}■■■■■■■■■■■■
${g(0.0)}■■■■■■■■■■■■
${g(0.9)}■■■■■■■■■■■■
${CLEAR}`

console.log(fade)
Gradient color stops

Mini Methods

Colored logging can quickly become unweildy, so I like to use the mini methods for common colors and styles:

import { l } from '@braebo/ansi' // console.log

import { r, g, b } from '@braebo/ansi' // Colors
import { d, bd, em } from '@braebo/ansi' // Styles

// Colors
l(r('red'))
l(g('green'))
l(b('blue'))

// Styles
l(d('dimmed'))
l(bd('bold'))
l(em('italic'))
Mini

If no arguments are provided, the mini methods will return the ANSI code:

console.log(r() + 'red', y() + 'yellow', g() + 'green')
Mini no args
Note

When no string is provided to a mini method, it won't be wrapped in a corresponding reset code. Use the clear method (clr()) to reset the styles yourself.


logger

The logger function used by the mini methods is somewhat involved, so I decided to expose / document it. It can be used to create your own custom logging functions that colorize input dynamically.

const err = logger({
	prefix: r('| '),
	printWidth: 20,
	fn: (...args: any[]) => {
		console.log(r('>'), r(bd('ERROR')))
		console.log(...args)
	},
})

err('Something went wrong:', { ok: false, cause: '¯\\_(ツ)_/¯' })
Logger custom screenshot

LogOptions

The logger function accepts the following options:


printWidth

Controls the expansion of objects and arrays into multiline output.

@default 60

const l = logger({ printWidth: 50 })
l({ foo: true, bar: [1, 'two', { three: () => 3 }] })
Logger printWidth screenshot
Note

Internally, printWidth is calculated somewhat roughly, so this option is generally a ballpark estimate.


inline

Forces inputs into either inline or multiline output.

@default undefined

const l = logger({ inline: true })
l({ foo: true, bar: [1, 'two', { three: () => 3 }] })
Logger inline screenshot

Add __inline__ to an object for a granular overrides.

l({
	one: 1,
	two: 2,
	nested: { __inline__: true, one: 1, two: 2, three: 3 },
})
Logger object overrides screenshot

For array overrides, use the strings __inline__ and __multiline__ to force inline or multiline output.

l([true, 1, 'two', () => 3, '__multiline__'])
Logger array overrides screenshot

delimiter

The delimiter used between rest args.

@default ' '

const l = logger({ delimiter: c(' · ') })
l(true, 1, 'two', () => 3)
Logger delimiter screenshot

prefix

A prefix to prepend to each line (works with multiline output).

@default ''

const l = logger({ prefix: c('⌇ '), delimiter: '' })
l('# ', bd('Header'))
l()
l(d('<p>'), em('Hello, world!'), d('</p>'))
Logger prefix screenshot

fn

A custom logger function.

@default console.log

const l = logger({ fn: console.warn })
l('E-gad!')
Logger warn screenshot

paint()

The paint function used by logger can be used directly to colorize arbitrary input:

console.log(
	paint(
		{
			num: 123,
			bool: true,
			str: 'foo',
			fn: () => 'bar',
		},
		{ inline: false },
	),
)
Paint screenshot

Why

NIH syndrome / copy-paste fatigue.


License

MIT © braebo

Report package

Please provide a reason for reporting this package. We will review your report and take appropriate action.

Please review the JSR usage policy before submitting a report.

Add Package

deno add jsr:@braebo/ansi

Import symbol

import * as ansi from "@braebo/ansi";
or

Import directly with a jsr specifier

import * as ansi from "jsr:@braebo/ansi";

Add Package

pnpm i jsr:@braebo/ansi
or (using pnpm 10.8 or older)
pnpm dlx jsr add @braebo/ansi

Import symbol

import * as ansi from "@braebo/ansi";

Add Package

yarn add jsr:@braebo/ansi
or (using Yarn 4.8 or older)
yarn dlx jsr add @braebo/ansi

Import symbol

import * as ansi from "@braebo/ansi";

Add Package

vlt install jsr:@braebo/ansi

Import symbol

import * as ansi from "@braebo/ansi";

Add Package

npx jsr add @braebo/ansi

Import symbol

import * as ansi from "@braebo/ansi";

Add Package

bunx jsr add @braebo/ansi

Import symbol

import * as ansi from "@braebo/ansi";