A lightweight, dependency-free ANSI styling library for terminal output.
Dye provides reusable modifier chains, named terminal colors, truecolor hexadecimal values, automatic color detection, and ANSI stripping while always returning primitive strings.
npm install @0xkahi/cli-dyebun add @0xkahi/cli-dyeimport { dye } from '@0xkahi/cli-dye';
console.log(dye.bold('Important'));
console.log(dye.italic().underline('Styled text'));
console.log(dye.colorize('Success', { fg: 'brightGreen' }));Dye supports the following modifiers:
resetbolddimitalicunderlineinversehiddenstrikethrough
Use a modifier directly:
const message = dye.bold('Hello');Or call it without text to create a reusable builder:
const heading = dye.bold().underline();
console.log(heading('First heading'));
console.log(heading('Second heading'));Modifiers can be chained in any order. Repeated modifiers are automatically deduplicated.
Use colorize() to apply an optional foreground, background, or both:
console.log(dye.colorize('Error', { fg: 'brightWhite', bg: 'red' }));
console.log(dye.colorize('Info', { fg: 'cyan' }));
console.log(dye.colorize('Highlighted', { bg: 'yellow' }));The following foreground and background colors are available:
| Regular | Bright |
|---|---|
black |
brightBlack |
red |
brightRed |
green |
brightGreen |
yellow |
brightYellow |
blue |
brightBlue |
magenta |
brightMagenta |
cyan |
brightCyan |
white |
brightWhite |
gray |
The exact appearance of named colors depends on the user's terminal palette.
grayandbrightBlackuse the same ANSI color code.
Create truecolor values with dye.hex():
const pink = dye.hex('#ed7892');
const blue = dye.hex('#00f');
console.log(dye.colorize('Truecolor', {
fg: pink,
bg: blue,
}));Both #RGB and #RRGGBB forms are accepted, case-insensitively. Hashless values, invalid digits, and alpha-bearing values throw a TypeError.
Hex colors are emitted directly using terminal 24-bit color sequences. Dye does not convert them to ANSI-256 or ANSI-16 palettes.
Calling colorize() with only options creates a reusable builder. Color builders support all modifiers:
const warning = dye
.colorize({ fg: 'brightYellow' })
.bold()
.underline();
console.log(warning('Check your configuration'));
console.log(warning('Connection is unstable'));Rendered values are always primitive strings—not string-like wrapper objects.
Dye detects terminal color support when the module is loaded. The resolved state is exposed through the read-only enabled property:
console.log(dye.enabled);Override color output when needed:
dye.setEnabled(true); // Always emit ANSI sequences
dye.setEnabled(false); // Always return plain text
dye.setEnabled(undefined); // Restore automatic detectionWhen output is disabled, styling and color functions return the supplied text without adding ANSI sequences.
Dye respects common terminal signals, including TTY support, NO_COLOR, FORCE_COLOR, and TERM=dumb.
Use strip() to remove ANSI CSI sequences from a string:
const message = dye.colorize({ fg: 'red' }).bold('Failure');
console.log(dye.strip(message)); // "Failure"It works with modifier-only output too:
console.log(dye.strip(dye.bold('Hello'))); // "Hello"strip() also removes supported CSI sequences generated by other ANSI libraries.
Dye includes TypeScript declarations and exports its public color and builder types:
import { dye } from '@0xkahi/cli-dye';
import type {
DyeColor,
ColorizeOptions,
HexColor,
StandardColor,
DyeStyler,
} from '@0xkahi/cli-dye';Applies a modifier immediately and returns a string.
Returns a reusable DyeStyler builder.
Applies foreground and/or background colors immediately.
Returns a reusable color and modifier builder.
Validates and converts #RGB or #RRGGBB into an opaque HexColor.
Removes ANSI CSI escape sequences.
Reports whether styling output is currently enabled.
Sets a true or false override. Pass undefined to restore automatic detection.