colorette

repository·main·Indexed 23 days ago

https://github.com/jorgebucaran/colorette

A lightweight, zero-dependency Node.js library for setting terminal text colors and styles. It features automatic color support detection, high performance, and supports standard ANSI colors, bright variants, background colors, and text modifiers like bold and underline. It respects NO_COLOR and FORCE_COLOR environment variables as well as --no-color and --color CLI flags.

Tokens
2.5K
Snippets
7
Records
14
Agent score
81%

What's inside colorette

  1. Control color support via Environment and CLI

    main

    Colorette respects standard terminal color control mechanisms:

    CLI Flags Use --no-color to disable color or --color to force it.

    Environment Variables

    • NO_COLOR=: Disables color.
    • FORCE_COLOR=: Forces color.
    $ ./example.js --no-color | ./consumer.js
    
    $ NO_COLOR= ./example.js | ./consumer.js
  2. Quickstart with colorette

    main

    Import specific color or style functions from colorette to wrap strings. You can nest styles (e.g., applying bold to a blue string) and use them within template literals. Colorette handles nested styles without breaking existing color sequences.

    import { blue, bold, underline } from "colorette"
    
    console.log(
      blue("I'm blue"),
      bold(blue("da ba dee")),
      underline(bold(blue("da ba daa")))
    )
  3. Override color detection with createColors()

    main

    Use createColors({ useColor }) to manually control whether colors are enabled or disabled, bypassing the automatic terminal detection. This is useful for testing or specific environment requirements.

    import { createColors } from "colorette"
    
    const { blue } = createColors({ useColor: false })
    
    console.log(blue("Blue? Nope, nah"))
  4. Reference all supported colors and modifiers

    main

    Colorette provides the following color, background, and modifier functions:

    | 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             | <u>underline</u>  |
    | magenta | bgMagenta         | magentaBright | bgMagentaBright          | ~~strikethrough~~ |
    | cyan    | bgCyan            | cyanBright    | bgCyanBright             | reset             |
    | white   | bgWhite           | whiteBright   | bgWhiteBright            |                   |
    | gray    |                   |               |                          |                   |
  5. Create a custom color object with createColors()

    main

    The createColors() function returns a Colorette object containing all available color and style functions. This is useful if you want to pass a single object containing all styling capabilities around your application. You can optionally pass an options object to control color usage.

    Options:

    • useColor: boolean - Forces color usage (if true, colors will be applied regardless of terminal support).
  6. Create a custom color instance with createColors()

    main

    The createColors() function allows you to create a set of color/style functions. You can control whether colors are enabled by passing a useColor option.

    • If useColor is true (or omitted, defaulting to isColorSupported), it returns the standard color functions that wrap strings in ANSI escape codes.
    • If useColor is false, it returns a set of functions that simply return the input string as-is (effectively disabling all styling).
  7. Use color and style functions from colorette

    main
    Colorette provides a set of exported functions to apply ANSI colors and styles to strings or numbers. Each function follows the Color type signature: (text: string | number) => string. You can import these functions directly to wrap text for terminal output.
  8. Use built-in color and style functions

    main

    Colorette exports a collection of functions for applying ANSI colors and styles directly to strings. If colors are supported in the environment, these functions wrap the input string in the appropriate escape codes. If not, they return the string unchanged.

    Available Styles and Colors:

    Styles:

    • reset: Resets all styles
    • bold: Bold text
    • dim: Dim text
    • italic: Italic text
    • underline: Underlined text
    • inverse: Inverted colors
    • hidden: Hidden text
    • strikethrough: Strikethrough text

    Colors:

    • black, red, green, yellow, blue, magenta, cyan, white, gray
    • Bright variants: blackBright, redBright, greenBright, yellowBright, blueBright, magentaBright, cyanBright, whiteBright

    Background Colors:

    • bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite
    • Bright variants: bgBlackBright, bgRedBright, bgGreenBright, bgYellowBright, bgBlueBright, bgMagentaBright, bgCyanBright, bgWhiteBright
  9. Reference all exported color and style functions

    main

    The following functions are exported by colorette. Each accepts a string or number and returns a formatted string.

    Styles

    • reset
    • bold
    • dim
    • italic
    • underline
    • inverse
    • hidden
    • strikethrough

    Standard Colors

    • black
    • red
    • green
    • yellow
    • blue
    • magenta
    • cyan
    • white
    • gray

    Background Colors

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

    Bright Colors

    • blackBright
    • redBright
    • greenBright
    • yellowBright
    • blueBright
    • magentaBright
    • cyanBright
    • whiteBright

    Bright Background Colors

    • bgBlackBright
    • bgRedBright
    • bgGreenBright
    • bgYellowBright
    • bgBlueBright
    • bgMagentaBright
    • bgCyanBright
    • bgWhiteBright
    type Color = (text: string | number) => string
    
    // Styles
    const reset: Color
    const bold: Color
    const dim: Color
    const italic: Color
    const underline: Color
    const inverse: Color
    const hidden: Color
    const strikethrough: Color
    
    // Colors
    const black: Color
    const red: Color
    const green: Color
    const yellow: Color
    const blue: Color
    const magenta: Color
    const cyan: Color
    const white: Color
    const gray: Color
    
    // Backgrounds
    const bgBlack: Color
    const bgRed: Color
    const bgGreen: Color
    const bgYellow: Color
    const bgBlue: Color
    const bgMagenta: Color
    const bgCyan: Color
    const bgWhite: Color
    
    // Bright Colors
    const blackBright: Color
    const redBright: Color
    const greenBright: Color
    const yellowBright: Color
    const blueBright: Color
    const magentaBright: Color
    const cyanBright: Color
    const whiteBright: Color
    
    // Bright Backgrounds
    const bgBlackBright: Color
    const bgRedBright: Color
    const bgGreenBright: Color
    const bgYellowBright: Color
    const bgBlueBright: Color
    const bgMagentaBright: Color
    const bgCyanBright: Color
    const bgWhiteBright: Color