Security News
JSR Working Group Kicks Off with Ambitious Roadmap and Plans for Open Governance
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
The colorette npm package is a Node.js library for colorizing text in the terminal. It provides a simple API to add colors and styles to console output, which can be useful for creating more readable and organized logs, command-line tools, and scripts.
Text coloring
Colorette allows you to color text output in the terminal. You can use predefined color functions to wrap your text and make it appear in the specified color when logged to the console.
const { red, green, blue } = require('colorette');
console.log(red('This is red text'));
console.log(green('This is green text'));
console.log(blue('This is blue text'));
Text styling
In addition to coloring, colorette provides functions to apply various text styles such as bold, underline, and italic.
const { bold, underline, italic } = require('colorette');
console.log(bold('This is bold text'));
console.log(underline('This is underlined text'));
console.log(italic('This is italic text'));
Combining styles
Colorette allows you to combine multiple styles and colors for a single piece of text. This can be useful for highlighting important information or creating visually distinct messages in the terminal.
const { red, bold, bgGreen } = require('colorette');
console.log(bold(red(bgGreen('Red text on green background'))));
Chalk is a popular npm package similar to colorette that allows for styling and coloring terminal text. It offers a chainable API and additional color options. Chalk is known for its ease of use and extensive customization capabilities, but it might be slightly larger in size compared to colorette.
Ansi-colors is another npm package for coloring terminal text. It focuses on performance and is dependency-free. It provides a similar API to colorette but with a different implementation approach. Ansi-colors may offer better performance in certain scenarios.
Kleur is a lightweight alternative to colorette with zero dependencies. It offers a similar API for text coloring and formatting. Kleur is designed for performance and has a minimalistic approach, which might make it faster in some cases.
Colorette is a Node.js library for colorizing text using ANSI escape sequences.
npm i colorette
Import the style functions you want to use.
const { red, blue, bold } = require("colorette")
Then use them to colorize your output.
console.log(bold(blue("Engage!")))
Mix it with template literals.
console.log(`
Beets are ${red("red")},
Plums are ${blue("blue")},
${bold("Colorette!")}.
`)
Use console.log's string substitution.
console.log(bold("Total: $%f"), 1.99)
Nest styles without breaking an existing open sequence.
console.log(red(`Red Shirt ${blue("Blue Shirt")} Red Shirt`))
Feeling adventurous? Try the pipeline operator.
console.log("Make it so!" |> bold |> blue)
Every style function returns its string argument wrapped in the corresponding ANSI escape sequence.
red("Red Alert") //=> \u001b[31mRed Alert\u001b[39m
Color support is automatically enabled if your terminal supports it, but you can toggle it on/off as needed.
const { options } = require("colorette")
options.enabled = false
Colorette supports the normal and bright color variations.
Colors | Background Colors | Bright Colors | Bright Background Colors | Modifiers |
---|---|---|---|---|
black | bgBlack | blackBright | bgBlackBright | dim |
red | bgRed | redBright | bgRedBright | bold |
green | bgGreen | greenBright | bgGreenBright | hidden |
yellow | bgYellow | yellowBright | bgYellowBright | italic |
blue | bgBlue | blueBright | bgBlueBright | underline |
magenta | bgMagenta | magentaBright | bgMagentaBright | |
cyan | bgCyan | cyanBright | bgCyanBright | reset |
white | bgWhite | whiteBright | bgWhiteBright | |
gray |
Chalk is the go-to terminal colorizer for Node.js. It's robust and actively maintained. So, why not chalk? Because we can do just as well with less. Chalk is larger than colorette and 20 to 100 times slower depending on what you're doing.
Part of the complexity comes from a chainable API where styles are used both as properties and methods by sharing the same prototype. It isn't evident how this could work unless you look at the implementation. You are just told it does, but none of it is essential to solve the problem at thand.
red.bold.underline("Do you believe in magic?")
Colorette's approach has more sticking parentheses, but it has been carved away of all the surprises.
red(bold(underline("Do you believe in magic?")))
All tests run on a 2.4GHz Intel Core i7 CPU with 16 GB memory.
npm i -C bench && node bench
# Using Styles chalk × 8,020 ops/sec kleur × 269,041 ops/sec ansi-colors × 156,343 ops/sec colorette × 692,207 ops/sec # Combining Styles chalk × 27,268 ops/sec kleur × 726,598 ops/sec ansi-colors × 252,814 ops/sec colorette × 1,903,657 ops/sec # Nesting Styles chalk × 22,736 ops/sec kleur × 281,761 ops/sec ansi-colors × 226,470 ops/sec colorette × 382,293 ops/sec
Colorette is MIT licensed. See LICENSE.
FAQs
🌈Easily set your terminal text color & styles.
The npm package colorette receives a total of 27,354,251 weekly downloads. As such, colorette popularity was classified as popular.
We found that colorette demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
At its inaugural meeting, the JSR Working Group outlined plans for an open governance model and a roadmap to enhance JavaScript package management.
Security News
Research
An advanced npm supply chain attack is leveraging Ethereum smart contracts for decentralized, persistent malware control, evading traditional defenses.
Security News
Research
Attackers are impersonating Sindre Sorhus on npm with a fake 'chalk-node' package containing a malicious backdoor to compromise developers' projects.