yoctocolors

repository·main·Indexed 21 days ago

https://github.com/sindresorhus/yoctocolors

A tiny, fast, and tree-shakeable command-line coloring package with no dependencies. It supports nested colors, ANSI styles, and foreground, background, and underline colors. Available as an ESM package or as yoctocolors-cjs for CommonJS environments.

Tokens
1.2K
Snippets
7
Records
8
Agent score
25%

What's inside yoctocolors

  1. Control color output via environment variables

    main

    The package uses basic color detection. You can override this behavior using the following environment variables:

    • Force colors ON: Set FORCE_COLOR=1.
    • Force colors OFF: Set NO_COLOR or NODE_DISABLE_COLORS to any value.
  2. Use yoctocolors for terminal coloring

    main

    You can use yoctocolors by importing the default object or using named imports. The library supports nested colors, allowing you to wrap styled strings within other styled strings.

    Default import usage:

    import colors from 'yoctocolors';
    
    console.log(colors.red('Yo!'));
    console.log(colors.blue(`Welcome to the ${colors.green('yoctocolors')} package!`));

    Named imports usage:

    import {red, blue, green} from 'yoctocolors';
    
    console.log(red('Yo!'));
    console.log(blue(`Welcome to the ${green('yoctocolors')} package!`));
    import colors from 'yoctocolors';
    
    console.log(colors.red('Yo!'));
    
    console.log(colors.blue(`Welcome to the ${colors.green('yoctocolors')} package!`));
  3. Reference underline colors

    main

    Underline colors are set independently of the text color. They are only visible when an underline style (e.g., underline) is also applied. Note that underline styles are not widely supported.

    Example: underlineRed(underlineCurly('typo')) renders a red squiggle below text.

    underlineBlack
    underlineRed
    underlineGreen
    underlineYellow
    underlineBlue
    underlineMagenta
    underlineCyan
    underlineWhite
    underlineGray
    underlineRedBright
    underlineGreenBright
    underlineYellowBright
    underlineBlueBright
    underlineMagentaBright
    underlineWhiteBright
  4. Reference background colors

    main

    The following methods are available for setting the background color of the text.

    bgBlack
    bgRed
    bgGreen
    bgYellow
    bgBlue
    bgMagenta
    bgCyan
    bgWhite
    bgGray
    bgRedBright
    bgGreenBright
    bgYellowBright
    bgBlueBright
    bgMagentaBright
    bgCyanBright
    bgWhiteBright
  5. Reference text modifiers

    main

    The following modifiers can be used to change the style of the text. Note that some styles (like italic, underline, and strikethrough) may not be widely supported by all terminals.

    reset - Reset the current style.
    bold - Make the text bold.
    dim - Make the text have lower opacity.
    italic - Make the text italic.
    underline - Put a horizontal line below the text.
    underlineDouble - Put a double horizontal line below the text.
    underlineCurly - Put a curly horizontal line below the text.
    underlineDotted - Put a dotted horizontal line below the text.
    underlineDashed - Put a dashed horizontal line below the text.
    overline - Put a horizontal line above the text.
    inverse - Invert background and foreground colors.
    hidden - Print the text but make it invisible.
    strikethrough - Put a horizontal line through the center of the text.
  6. Use yoctocolors for terminal styling

    main

    The yoctocolors package provides a collection of functions to apply ANSI colors and styles (like bold, underline, etc.) to strings for terminal output. It exports all styling functions from its base module. You can import individual color functions or the entire module to wrap strings in terminal escape codes.

    import { red, blue, bold } from 'yoctocolors';
    
    console.log(red('This is red text'));
    console.log(bold(blue('This is bold blue text')));