picocolors

repository·main·Indexed 23 days ago

https://github.com/alexeyraspopov/picocolors

A high-performance, zero-dependency library for terminal output formatting using ANSI colors. Supports Node.js v6+ and browsers, providing foreground, background, and formatting functions. It is NO_COLOR friendly and includes TypeScript type declarations.

Tokens
1.1K
Snippets
7
Records
10
Agent score
33%

What's inside picocolors

  1. Replace chalk with picocolors

    main

    To migrate from chalk to picocolors, follow these steps:

    1. Update imports: Change import chalk from 'chalk' to import pico from 'picocolors'.
    2. Update variable usage: Change chalk.red(text) to pico.red(text).
    3. Convert method chains to nested calls: Since picocolors does not support chaining, convert chalk.red.bold(text) to pico.red(pico.bold(text)).
    4. Handle tagged templates: If you use chalk's tagged template literals, use the colorize-template package to bridge the gap.
    - import chalk from 'chalk'
    + import pico from 'picocolors'
    
    - chalk.red(text)
    + pico.red(text)
    
    - chalk.red.bold(text)
    + pico.red(pico.bold(text))
  2. Use picocolors for terminal text formatting

    main

    Import picocolors to access a variety of functions for coloring and formatting terminal output. It supports Node.js v6+ and browsers, and works with both CJS and ESM. It is NO_COLOR friendly and includes TypeScript type declarations.

    import pc from "picocolors"
    
    console.log(
      pc.green(`How are ${pc.italic(`you`)} doing?`)
    )
  3. Create a custom color API with createColors()

    main

    The createColors(enabled) function returns a new API object where color support is manually configured based on the enabled boolean.

    import pc from "picocolors"
    
    let { red, bgWhite } = pc.createColors(options.enableColors)
  4. Check color support with isColorSupported

    main

    Use the isColorSupported boolean property to determine if colors or formatting are supported in the current terminal environment.

    import pc from "picocolors"
    
    if (pc.isColorSupported) {
      console.log("Yay! This script can use colors and formatters")
    }
  5. Reference background color functions

    main

    The picocolors object provides the following background color modifier functions:

    • bgBlack
    • bgRed
    • bgGreen
    • bgYellow
    • bgBlue
    • bgMagenta
    • bgCyan
    • bgWhite
    • bgBlackBright
    • bgRedBright
    • bgGreenBright
    • bgYellowBright
    • bgBlueBright
    • bgMagentaBright
    • bgCyanBright
    • bgWhiteBright
    import pc from "picocolors"
    
    console.log(
      pc.bgBlack(
        pc.white(`Tom appeared on the sidewalk with a bucket of whitewash and a long-handled brush.`)
      )
    )
  6. Reference formatting functions

    main

    The picocolors object provides the following formatting functions:

    • dim
    • bold
    • hidden
    • italic
    • underline
    • strikethrough
    • reset
    • inverse
    • blackBright
    • redBright
    • greenBright
    • yellowBright
    • blueBright
    • magentaBright
    • cyanBright
    • whiteBright
    import pc from "picocolors"
    
    for (let task of tasks) {
      console.log(`${pc.bold(task.name)} ${pc.dim(task.durationMs + "ms")}`)
    }
  7. Reference coloring functions

    main

    The picocolors object provides the following foreground color functions:

    • black
    • red
    • green
    • yellow
    • blue
    • magenta
    • cyan
    • white
    • gray
    import pc from "picocolors"
    
    console.log(`I see a ${pc.red("red door")} and I want it painted ${pc.black("black")}`)
  8. Use the default picocolors instance

    main
    By default, picocolors exports a pre-configured instance of colors that automatically detects if color support is enabled in your environment. It respects standard flags and environment variables like NO_COLOR, --no-color, FORCE_COLOR, and --color.
  9. Create a custom picocolors instance with createColors()

    main
    You can manually control color support by using createColors(enabled). If enabled is true, it returns an object with all color formatting functions. If false, it returns an object where all functions simply return the input string without any ANSI escape codes.