Convert Figma logo to code with AI

lukeed logokleur

The fastest Node.js library for formatting terminal text with ANSI colors~!

1,609
46
1,609
12

Top Related Projects

21,891

🖍 Terminal string styling done right

get colors in your node.js console

🌈Easily set your terminal text color & styles

1,496

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

Quick Overview

Kleur is a minimalist and fast Node.js library for styling terminal text with ANSI colors. It provides a simple API for adding color and style to console output, making it easier to create visually appealing command-line interfaces and improve the readability of terminal messages.

Pros

  • Lightweight and fast, with zero dependencies
  • Supports both nested and chained color methods
  • Automatically detects color support in the terminal
  • Includes TypeScript definitions

Cons

  • Limited to basic ANSI color and style options
  • Not suitable for complex terminal UI applications
  • Lacks advanced features like cursor manipulation or input handling

Code Examples

Basic color usage:

import kleur from 'kleur';

console.log(kleur.red('This text is red'));
console.log(kleur.blue().bold('This text is blue and bold'));

Nested color usage:

import { red, blue, green } from 'kleur';

console.log(red(`This is red ${blue('with blue')} and ${green('green')} nested`));

Conditional color application:

import kleur from 'kleur';

// Disable colors
kleur.enabled = false;

console.log(kleur.red('This will not be red'));

// Enable colors
kleur.enabled = true;

console.log(kleur.red('This will be red'));

Getting Started

To use Kleur in your project, first install it using npm:

npm install kleur

Then, import and use it in your JavaScript or TypeScript file:

import kleur from 'kleur';

console.log(kleur.cyan('Hello, colorful world!'));
console.log(kleur.yellow().bold().underline('Styled text'));

That's it! You can now start adding colors and styles to your terminal output using Kleur's simple and intuitive API.

Competitor Comparisons

21,891

🖍 Terminal string styling done right

Pros of Chalk

  • More extensive color and style options, including background colors and advanced formatting
  • Wider community adoption and ecosystem support
  • Automatic color support detection for different environments

Cons of Chalk

  • Larger package size and potentially slower performance
  • More complex API with additional features that may not be necessary for simpler use cases

Code Comparison

Chalk:

import chalk from 'chalk';

console.log(chalk.blue.bold('Hello world!'));
console.log(chalk.green.bgRed.bold('Colorful text'));

Kleur:

import kleur from 'kleur';

console.log(kleur.blue().bold('Hello world!'));
console.log(kleur.green().bold('Colorful text'));

Key Differences

  • Chalk offers more styling options, including background colors
  • Kleur focuses on simplicity and performance
  • Chalk uses a chaining API, while Kleur uses a function-based approach
  • Kleur is more lightweight and has a smaller package size
  • Chalk provides automatic color support detection, while Kleur requires manual configuration

Use Cases

  • Choose Chalk for projects requiring extensive styling options and broad environment support
  • Opt for Kleur in performance-critical applications or when a minimal, lightweight solution is preferred

get colors in your node.js console

Pros of colors.js

  • More extensive color palette with support for 256 colors and custom color definitions
  • Includes additional features like rainbow text and background colors
  • Has a longer history and larger user base, potentially leading to better community support

Cons of colors.js

  • Larger package size due to more features, which may impact load times
  • More complex API with additional methods and options
  • Higher maintenance overhead due to more extensive codebase

Code Comparison

colors.js:

const colors = require('colors');
console.log('Hello'.green); // outputs green text
console.log('World'.blue.bgRed); // blue text with red background
console.log(colors.rainbow('Multicolor!')); // rainbow text

kleur:

const { red, blue, green } = require('kleur');
console.log(green('Hello')); // outputs green text
console.log(blue().bgRed('World')); // blue text with red background
// No built-in rainbow function

Both libraries provide similar core functionality for coloring console output, but colors.js offers more advanced features at the cost of a larger package size. kleur focuses on simplicity and performance, making it a lighter alternative for basic color needs. The choice between the two depends on the specific requirements of your project and the importance of package size versus feature set.

🌈Easily set your terminal text color & styles

Pros of Colorette

  • Smaller bundle size (1.1kB vs 1.4kB for Kleur)
  • Supports both CommonJS and ES modules out of the box
  • Slightly more intuitive API for chaining multiple styles

Cons of Colorette

  • Fewer color options compared to Kleur
  • Less actively maintained (fewer recent updates)
  • No specific browser support (Kleur has a browser-compatible version)

Code Comparison

Colorette:

import { red, bold } from 'colorette'

console.log(red(bold('Hello, World!')))

Kleur:

import kleur from 'kleur'

console.log(kleur.red().bold('Hello, World!'))

Both libraries provide similar functionality for adding color to console output. Colorette uses individual imports for each color/style, while Kleur uses a chaining approach. The syntax difference is minimal, but some developers may prefer one style over the other.

Overall, both libraries are lightweight and efficient for adding color to terminal output. Colorette might be preferred for its smaller size and dual module support, while Kleur offers more color options and better browser compatibility.

1,496

🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

Pros of Color

  • More comprehensive feature set, including support for 256 colors and RGB/HSL
  • Provides additional utilities like color mixing and conversion
  • Offers both CLI and library usage

Cons of Color

  • Larger package size due to more features
  • Potentially more complex API for simple use cases
  • Written in Go, which may not be ideal for all projects

Code Comparison

Kleur:

import kleur from 'kleur';
console.log(kleur.red('This text is red'));

Color:

import "github.com/gookit/color"
color.Red.Println("This text is red")

Summary

Kleur is a lightweight JavaScript library focused on simple ANSI color output, while Color is a more feature-rich Go library offering advanced color manipulation and CLI tools. Kleur is ideal for projects needing basic color output with minimal overhead, whereas Color is better suited for applications requiring extensive color handling capabilities.

The choice between the two depends on the specific project requirements, programming language preference, and the level of color manipulation needed. Kleur's simplicity makes it a good choice for JavaScript projects with basic color needs, while Color's comprehensive features make it suitable for more complex Go applications.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

kleur
The fastest Node.js library for formatting terminal text with ANSI colors~!

Features


As of v3.0 the Chalk-style syntax (magical getter) is no longer used.
Please visit History for migration paths supporting that syntax.


Install

$ npm install --save kleur

Usage

import kleur from 'kleur';

// basic usage
kleur.red('red text');

// chained methods
kleur.blue().bold().underline('howdy partner');

// nested methods
kleur.bold(`${ white().bgRed('[ERROR]') } ${ kleur.red().italic('Something happened')}`);

Chained Methods

const { bold, green } = require('kleur');

console.log(bold().red('this is a bold red message'));
console.log(bold().italic('this is a bold italicized message'));
console.log(bold().yellow().bgRed().italic('this is a bold yellow italicized message'));
console.log(green().bold().underline('this is a bold green underlined message'));

Nested Methods

const { yellow, red, cyan } = require('kleur');

console.log(yellow(`foo ${red().bold('red')} bar ${cyan('cyan')} baz`));
console.log(yellow('foo ' + red().bold('red') + ' bar ' + cyan('cyan') + ' baz'));

Conditional Support

Toggle color support as needed; kleur includes simple auto-detection which may not cover all cases.

Note: Both kleur and kleur/colors share the same detection logic.

import kleur from 'kleur';

// manually disable
kleur.enabled = false;

// or use another library to detect support
kleur.enabled = require('color-support').level > 0;

console.log(kleur.red('I will only be colored red if the terminal supports colors'));

Important:
Colors will be disabled automatically in non TTY contexts. For example, spawning another process or piping output into another process will disable colorization automatically. To force colors in your piped output, you may do so with the FORCE_COLOR=1 environment variable:

$ node app.js #=> COLORS
$ node app.js > log.txt  #=> NO COLORS
$ FORCE_COLOR=1 node app.js > log.txt #=> COLORS
$ FORCE_COLOR=0 node app.js > log.txt #=> NO COLORS

API

Any kleur method returns a String when invoked with input; otherwise chaining is expected.

It's up to the developer to pass the output to destinations like console.log, process.stdout.write, etc.

The methods below are grouped by type for legibility purposes only. They each can be chained or nested with one another.

Colors:

black — red — green — yellow — blue — magenta — cyan — white — gray — grey

Backgrounds:

bgBlack — bgRed — bgGreen — bgYellow — bgBlue — bgMagenta — bgCyan — bgWhite

Modifiers:

reset — bold — dim — italic* — underline — inverse — hidden — strikethrough*

* Not widely supported

Individual Colors

When you only need a few colors, it doesn't make sense to import all of kleur because, as small as it is, kleur is not treeshakeable, and so most of its code will be doing nothing. In order to fix this, you can import from the kleur/colors submodule which fully supports tree-shaking.

The caveat with this approach is that color functions are not chainable~!
Each function receives and colorizes its input. You may combine colors, backgrounds, and modifiers by nesting function calls within other functions.

// or: import * as kleur from 'kleur/colors';
import { red, underline, bgWhite } from 'kleur/colors';

red('red text');
//~> kleur.red('red text');

underline(red('red underlined text'));
//~> kleur.underline().red('red underlined text');

bgWhite(underline(red('red underlined text w/ white background')));
//~> kleur.bgWhite().underline().red('red underlined text w/ white background');

Note: All the same colors, backgrounds, and modifiers are available.

Conditional Support

The kleur/colors submodule also allows you to toggle color support, as needed.
It includes the same initial assumptions as kleur, in an attempt to have colors enabled by default.

Unlike kleur, this setting exists as kleur.$.enabled instead of kleur.enabled:

import * as kleur from 'kleur/colors';
// or: import { $, red } from 'kleur/colors';

// manually disabled
kleur.$.enabled = false;

// or use another library to detect support
kleur.$.enabled = require('color-support').level > 0;

console.log(red('I will only be colored red if the terminal supports colors'));

Benchmarks

Using Node v10.13.0

Load time

chalk        :: 5.303ms
kleur        :: 0.488ms
kleur/colors :: 0.369ms
ansi-colors  :: 1.504ms

Performance

# All Colors
  ansi-colors      x 177,625 ops/sec ±1.47% (92 runs sampled)
  chalk            x 611,907 ops/sec ±0.20% (92 runs sampled)
  kleur            x 742,509 ops/sec ±1.47% (93 runs sampled)
  kleur/colors     x 881,742 ops/sec ±0.19% (98 runs sampled)

# Stacked colors
  ansi-colors      x  23,331 ops/sec ±1.81% (94 runs sampled)
  chalk            x 337,178 ops/sec ±0.20% (98 runs sampled)
  kleur            x  78,299 ops/sec ±1.01% (97 runs sampled)
  kleur/colors     x 104,431 ops/sec ±0.22% (97 runs sampled)

# Nested colors
  ansi-colors      x  67,181 ops/sec ±1.15% (92 runs sampled)
  chalk            x 116,361 ops/sec ±0.63% (94 runs sampled)
  kleur            x 139,514 ops/sec ±0.76% (95 runs sampled)
  kleur/colors     x 145,716 ops/sec ±0.97% (97 runs sampled)

History

This project originally forked ansi-colors.

Beginning with kleur@3.0, the Chalk-style syntax (magical getter) has been replaced with function calls per key:

// Old:
c.red.bold.underline('old');

// New:
c.red().bold().underline('new');

As I work more with Rust, the newer syntax feels so much better & more natural!

If you prefer the old syntax, you may migrate to ansi-colors or newer chalk releases.
Versions below kleur@3.0 have been officially deprecated.

License

MIT © Luke Edwards

NPM DownloadsLast 30 Days