chalkolors
v1.0.0
Published
A tiny and fast library for terminal output formatting with ANSI colors
Maintainers
Readme
chalkolors
A tiny and fast library for terminal output formatting with ANSI colors.
import ck from "chalkolors"
console.log(
ck.green(`How are ${ck.italic(`you`)} doing?`)
)- No dependencies.
- 1.6 times faster than colorette and nanocolors and 2.5 times faster than chalk on complex formatting, on par for simple calls.
- Extended underline styles (double, curly, dotted, dashed), overline and underline colors.
- Node.js v16+. Support for both CJS and ESM projects.
- TypeScript type declarations included.
NO_COLORfriendly.
Install
npm install chalkolorsUsage
chalkolors provides an object which includes a variety of text coloring and formatting functions.
import ck from "chalkolors"Colors
black, red, green, yellow, blue, magenta, cyan, white, gray and bright variants blackBright, redBright, greenBright, yellowBright, blueBright, magentaBright, cyanBright, whiteBright.
console.log(`I see a ${ck.red("red door")} and I want it painted ${ck.black("black")}`)Background colors
bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite, bgGray and bright variants bgBlackBright, bgRedBright, bgGreenBright, bgYellowBright, bgBlueBright, bgMagentaBright, bgCyanBright, bgWhiteBright.
console.log(
ck.bgBlack(
ck.white(`Tom appeared on the sidewalk with a bucket of whitewash and a long-handled brush.`)
)
)Formatting
reset, bold, dim, italic, underline, overline, inverse, hidden, strikethrough.
for (let task of tasks) {
console.log(`${ck.bold(task.name)} ${ck.dim(task.durationMs + "ms")}`)
}Underline styles
underlineDouble, underlineCurly, underlineDotted, underlineDashed.
console.log(`This word is ${ck.underlineCurly("misspeled")}`)These use the SGR 4:x sequences. Terminals without support for them may show a plain underline or no underline at all.
Underline colors
underlineBlack, underlineRed, underlineGreen, underlineYellow, underlineBlue, underlineMagenta, underlineCyan, underlineWhite, underlineGray and bright variants underlineRedBright, underlineGreenBright, underlineYellowBright, underlineBlueBright, underlineMagentaBright, underlineCyanBright, underlineWhiteBright.
Underline colors only change the color of the line, so combine them with an underline style:
console.log(ck.underlineRed(ck.underlineCurly("misspeled")))Utilities
isColorSupported— boolean, explicitly tells whether or not the colors or formatting appear on the screenimport ck from "chalkolors" if (ck.isColorSupported) { console.log("Yay! This script can use colors and formatters") }createColors(enabled)— a function that returns a new API object with manually defined color support configurationimport ck from "chalkolors" let { red, bgWhite } = ck.createColors(options.enableColors)
Color support is detected from the environment: NO_COLOR or --no-color disable colors; FORCE_COLOR, --color, Windows, a TTY (unless TERM=dumb) or CI enable them.
Replacing chalk
Replace package name in import:
- import chalk from 'chalk' + import ck from 'chalkolors'Replace variable:
- chalk.red(text) + ck.red(text)Replace chains to nested calls:
- chalk.red.bold(text) + ck.red(ck.bold(text))You can use
colorize-templateto replace chalk’s tagged template literal.+ import { createColorize } from 'colorize-template' + let colorize = createColorize(ck) - chalk.red.bold`full {yellow ${"text"}}` + colorize`{red.bold full {yellow ${"text"}}}`
Benchmarks
Results from Node.js 24.21.0 on an Intel Core i9-10940X (Windows). Lower is better.
Complex formatting expression (nested calls):
$ node benchmarks/complex.mjs
chalk v4 676 ns
chalk v5 685 ns
cli-color 15,470 ns
ansi-colors 1,950 ns
kleur 613 ns
kleur/colors 526 ns
colorette 434 ns
nanocolors 441 ns
yoctocolors 445 ns
+ chalkolors 269 nsSimple API call:
$ node benchmarks/simple.mjs
chalk v4 28.52 ns
chalk v5 17.72 ns
cli-color 1,810.00 ns
ansi-colors 198.61 ns
kleur 27.00 ns
kleur/colors 20.66 ns
colorette 4.81 ns
nanocolors 5.14 ns
yoctocolors 5.08 ns
+ chalkolors 5.07 nsColoring already colored large text:
$ node benchmarks/recursion.mjs
chalk v4 63.77 µs
chalk v5 63.70 µs
cli-color 37.70 µs
ansi-colors 35.34 µs
kleur 33.34 µs
kleur/colors 33.34 µs
colorette 59.39 µs
nanocolors 33.48 µs
yoctocolors 41.97 µs
+ chalkolors 43.02 µsLibrary loading time:
$ node benchmarks/loading.mjs
chalk v4 1.91 µs
chalk v5 3.17 µs
cli-color 6.86 µs
ansi-colors 1.16 µs
kleur 1.10 µs
kleur/colors 1.12 µs
colorette 1.12 µs
nanocolors 1.12 µs
yoctocolors 5.87 µs
+ chalkolors 1.14 µsBundled size:
$ node benchmarks/size.mjs
chalk v4 49.26 KB
cli-color 138.27 KB
ansi-colors 10.22 KB
kleur 3.81 KB
kleur/colors 3.06 KB
colorette 6.04 KB
nanocolors 5.75 KB
yoctocolors 8.80 KB
+ chalkolors 6.29 KBTo run them yourself:
npm install
npm install --no-save chalk5@npm:chalk@5
node benchmarks/size.mjs
node benchmarks/loading.mjs
node benchmarks/simple.mjs
node benchmarks/complex.mjs
node benchmarks/recursion.mjsCredits
- picocolors by @alexeyraspopov — chalkolors is a fork of picocolors and keeps its ISC license.
- yoctocolors by @sindresorhus — the underline colors come from yoctocolors.
License
ISC. The underline colors are derived from yoctocolors and are MIT licensed. See LICENSE.
