### Usage example with red, bold, and isColorSupported Source: https://github.com/ai/nanocolors Shows how to import and use color functions. Demonstrates nesting by wrapping functions instead of chaining. Checks `isColorSupported` to conditionally log color support. ```javascript import { red, bold, isColorSupported } from 'nanocolors' console.log(red('error:') + ' something failed') if (isColorSupported) { console.log('Colors are supported') // TTY, Windows, CI, FORCE_COLOR… } // Nesting: functions are not chainable, wrap them instead console.log(bold(red('FATAL'))) ``` -------------------------------- ### Nested color functions for background and foreground Source: https://github.com/ai/nanocolors/blob/main/README.md Example of using background and foreground colors together. Functions are not chainable, so they must be nested. ```js import { black, bgYellow } from 'nanocolors' console.log(bgYellow(black(' WARN '))) ``` -------------------------------- ### Usage example for browser bundle Source: https://github.com/ai/nanocolors Shows that all color functions are pass-through stringify when bundled for the browser. red('text') returns 'text', bold(42) returns '42', and red('') returns ''. ```javascript // Bundled for the browser — every call is a pass-through stringify import { red, bold } from 'nanocolors' red('text') // 'text' bold(42) // '42' red('') // '' ``` -------------------------------- ### Usage with Colors Source: https://github.com/ai/nanocolors Shows how to use the Colors interface with createColors to get enabled and disabled color sets. ```ts import { createColors, type Colors } from 'nanocolors' const enabled: Colors = createColors(true) const disabled: Colors = createColors(false) function apply(colors: Colors, msg: string): string { return colors.isColorSupported ? colors.bold(colors.green(msg)) : msg } ``` -------------------------------- ### CJS require usage example Source: https://github.com/ai/nanocolors Shows how to require nanocolors in CommonJS and use the color functions. Also demonstrates createColors with a custom enabled flag based on an environment variable. ```javascript // CJS require const { red, bold, isColorSupported, createColors } = require('nanocolors') console.log(red('via require')) console.log(bold(red('nested'))) if (isColorSupported) { /* TTY output */ } const custom = createColors(process.env.ENABLE_COLORS === '1') ``` -------------------------------- ### Respecting an application option with auto-detection fallback Source: https://github.com/ai/nanocolors Passing undefined falls back to the module-level auto-detection, so no explicit branching is needed. This example is shown in the README. ```js import { createColors } from 'nanocolors' // undefined falls back to the module-level auto-detection, // so no explicit branching is needed const { red } = createColors(options.enableColors === undefined ? undefined : options.enableColors) ``` -------------------------------- ### Import performance benchmark: colorette vs nanocolors Source: https://github.com/ai/nanocolors/wiki/Colorette-Changes Benchmark results comparing import time in milliseconds. Shows nanocolors has faster import performance. ```text colorette 2.0.2 1.034 ms nanocolors 0.486 ms ``` -------------------------------- ### Run codemod to replace chalk with nanocolors Source: https://github.com/ai/nanocolors/blob/main/README.md Command to automatically apply the chalk-to-nanocolors migration using a codemod. ```sh npx jscodeshift FILES -t https://gist.githubusercontent.com/gavrix/ff051941ad9a19c8ea3224f38c30bc9a/raw/09d81e93f880ecbc8f52dcf7819816c81e2ba340/chalk_nanocolors_transform.js ``` -------------------------------- ### Performance benchmark: colorette vs nanocolors Source: https://github.com/ai/nanocolors/wiki/Colorette-Changes Benchmark results comparing colorette 2.0.2 and nanocolors performance in operations per second. Shows nanocolors is significantly faster. ```text colorette 2.0.2 19,712,884 ops/sec nanocolors 47,256,069 ops/sec ``` -------------------------------- ### Package-level exports map in package.json Source: https://github.com/ai/nanocolors Defines the entry points for different environments: browser, require (CommonJS), import (ESM), and default. Includes `main`, `module`, `types`, `browser`, `react-native`, and `sideEffects` fields. ```json ".": { "browser": "./index.browser.js", "require": "./index.cjs", "import": "./index.js", "default": "./index.js" } ``` -------------------------------- ### Replacing chalk with nanocolors Source: https://github.com/ai/nanocolors/blob/main/README.md Steps to replace chalk with nanocolors: change imports, unprefix calls, replace chains with nested calls, and use nanocolors-template for template tags. ```diff - import chalk from 'chalk' + import { red, bold } from 'nanocolors' ``` ```diff - chalk.red(text) + red(text) ``` ```diff - chalk.red.bold(text) + red(bold(text)) ``` ```diff - import chalk from 'chalk' + import { colorize } from 'nanocolors-template' - chalk.yellow.bold`yellow {red ${ ``` -------------------------------- ### Configure createColors with app-level override Source: https://github.com/ai/nanocolors Use this snippet to configure color output from the consuming application. It respects the library's auto-detection but allows an app-level override by passing `undefined` when `options.colors` is undefined. It also shows how to inspect the detected support using `isColorSupported`. ```javascript import { createColors, isColorSupported } from 'nanocolors' // Respect the library's auto-detection, but allow an app-level override const { red } = createColors(options.colors === undefined ? undefined : options.colors) // Inspect the detected support if (isColorSupported) { console.log('Color output enabled') } ``` -------------------------------- ### Basic usage of nanocolors Source: https://github.com/ai/nanocolors/blob/main/README.md Basic usage of the nanocolors library to colorize and bold text in terminal output. ```js import { green, bold } from 'nanocolors' console.log( green(`Task ${bold('1')} was finished`) ) ``` -------------------------------- ### createColors(enabled?) Source: https://github.com/ai/nanocolors Factory function that creates a Colors object with color functions. Accepts an optional boolean to explicitly enable or disable color support, overriding auto-detection. ```APIDOC ## createColors(enabled?) ### Description Factory function that creates a Colors object with color functions. Accepts an optional boolean to explicitly enable or disable color support, overriding auto-detection. ### Method Function ### Signature `createColors(enabled?: boolean): Colors` ### Parameters - **enabled** (boolean) - Optional - Whether to enable color support. If omitted, auto-detection is used. ### Returns - **Colors** - An object containing all 24 color functions. ### Example ```js import { createColors } from 'nanocolors' const colors = createColors(true) console.log(colors.red('error')) ``` ``` -------------------------------- ### isColorSupported Source: https://github.com/ai/nanocolors Boolean constant indicating whether colors are supported in the current environment. Auto-detected at module load time. ```APIDOC ## isColorSupported ### Description Boolean constant indicating whether colors are supported in the current environment. Auto-detected at module load time. ### Type `boolean` ### Example ```js import { isColorSupported } from 'nanocolors' if (isColorSupported) { console.log('Colors are supported') } ``` ``` -------------------------------- ### TypeScript definitions for nanocolors exports Source: https://github.com/ai/nanocolors Declares the 25 named exports and two named types. The `types` field in package.json points to this file. ```typescript export const isColorSupported: boolean export function createColors(enabled?: boolean): Colors export const reset: Color // ... all 24 Color constants ``` -------------------------------- ### Shell-level control via environment variables and CLI flags Source: https://github.com/ai/nanocolors This snippet shows how to control color output from the shell using environment variables and CLI flags. `NO_COLOR=1` forces plain output even if `FORCE_COLOR` is set, while `FORCE_COLOR=1` forces colored output even when piped. The `--no-color` and `--color` flags provide direct control. ```javascript // Shell-level control // NO_COLOR=1 node app.js -> plain output, even overriding FORCE_COLOR // FORCE_COLOR=1 node app.js -> colored output even when piped // node app.js --no-color -> plain output // node app.js --color -> colored output ``` -------------------------------- ### Usage with Color Source: https://github.com/ai/nanocolors Shows how to use the Color type as a function parameter and assign a color function like red to it. ```ts import { type Color, red } from 'nanocolors' const paint: Color = red // red matches the Color signature function badge(text: string, style: Color): string { return style(`[ ${text} ]`) } console.log(badge('OK', red)) ``` -------------------------------- ### CJS stub (index.browser.cjs) Source: https://github.com/ai/nanocolors No-op stub for CommonJS. Exports the return value of createColors() with isColorSupported=false, sets a default key for interop, and attaches createColors as a method. ```javascript // index.browser.cjs:1 let nope = s => '' + s // index.browser.cjs:3–33 function createColors() { return { isColorSupported: false, reset: nope, bold: nope, /* …24 keys */ } } // index.browser.cjs:34–36 module.exports = createColors() module.exports.default = module.exports module.exports.createColors = createColors ``` -------------------------------- ### Direct ESM import of CJS file Source: https://github.com/ai/nanocolors Shows that importing the CJS file from ESM synthesizes a default export that equals the full module.exports object. Both cjs.red and cjs.default.red work, and createColors is available on both. ```javascript // Direct ESM import of the CJS file (shows the synthesized "default", // which equals the full exports object) import cjs from '../index.cjs' cjs.red('text') // works — default === module.exports cjs.default.red('text') // also works — default.default wrapper cjs.createColors(true) // available on both ``` -------------------------------- ### createColors Source: https://github.com/ai/nanocolors Creates a custom color instance with an optional enabled flag. The default parameter is `isColorSupported`. ```APIDOC ## createColors ### Description Creates a custom color instance with an optional enabled flag. The default parameter is `isColorSupported`. ### Parameters - `enabled` (boolean) - Optional - Whether to enable colors. Defaults to `isColorSupported`. ### Returns - `Colors` - An object with color functions. ### Usage Example ```js const custom = createColors(process.env.ENABLE_COLORS === '1') ``` ``` -------------------------------- ### Check color support with isColorSupported Source: https://github.com/ai/nanocolors/blob/main/README.md Check if the current environment supports colors using the isColorSupported export. ```js import { isColorSupported } from 'nanocolors' if (isColorSupported) { console.log('With colors') } ``` -------------------------------- ### ESM stub (index.browser.js) Source: https://github.com/ai/nanocolors No-op stub for browser/React Native bundles. Exports isColorSupported=false and 24 aliases of a nope helper, plus a createColors factory that ignores its argument. ```javascript // index.browser.js:1 let nope = s => '' + s // index.browser.js:3 export let isColorSupported = false export let reset = nope export let bold = nope // ... dim, italic, underline, inverse, hidden, strikethrough, // ... black, red, green, yellow, blue, magenta, cyan, white, gray, // ... bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite // (index.browser.js:4–28) // index.browser.js:30–58 export function createColors() { return { isColorSupported, reset, bold, dim, italic, underline, inverse, hidden, strikethrough, black, red, green, yellow, blue, magenta, cyan, white, gray, bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite } } ``` -------------------------------- ### Composition by nesting Source: https://github.com/ai/nanocolors Shows how to combine styles by nesting function calls. Functions are not chainable; nesting is required for composition. The comment explains that inner dim closes with \x1b[22m, then \x1b[1m restores the outer bold. ```javascript import { black, bgYellow, red, bold, dim } from 'nanocolors' console.log(bgYellow(black(' WARN '))) // nested fg + bg console.log(red(bold('FATAL'))) // style inside color console.log(bold('b ' + red('r ' + dim('d') + ' r') + ' b')) // \x1b[1mb \x1b[31mr \x1b[2md\x1b[22m\x1b[1m r\x1b[39m b\x1b[22m // inner dim closes with \x1b[22m, then \x1b[1m restores the outer bold ``` -------------------------------- ### isColorSupported Source: https://github.com/ai/nanocolors A boolean constant indicating whether color output is supported in the current environment. It is evaluated at module load time and cannot be changed afterwards. ```APIDOC ## isColorSupported ### Description A boolean constant indicating whether color output is supported in the current environment. It is evaluated at module load time and cannot be changed afterwards. ### Usage Example ```js import { isColorSupported } from 'nanocolors' if (isColorSupported) { console.log('Color output enabled') } ``` ``` -------------------------------- ### Override color support with createColors Source: https://github.com/ai/nanocolors/blob/main/README.md Manually override color support detection by passing a boolean to createColors. On undefined, it uses auto-detection. ```js import { createColors } from 'nanocolors' const { red } = createColors(options.enableColors) ``` -------------------------------- ### Auto-detection logic in index.js (Node builds) Source: https://github.com/ai/nanocolors Evaluated once at module load in Node builds. Colors are enabled iff not disabled and at least one of forced, Windows, compatible terminal, or known CI. Uses presence checks ('VAR' in process.env) and guards tty usage. ```javascript let isDisabled = 'NO_COLOR' in process.env || process.argv.includes('--no-color') let isForced = 'FORCE_COLOR' in process.env || process.argv.includes('--color') let isWindows = process.platform === 'win32' let isCompatibleTerminal = tty && tty.isatty(1) && process.env.TERM && process.env.TERM !== 'dumb' let isCI = 'CI' in process.env && ('GITHUB_ACTIONS' in process.env || 'GITLAB_CI' in process.env || 'CIRCLECI' in process.env) export let isColorSupported = !isDisabled && (isForced || isWindows || isCompatibleTerminal || isCI) ``` -------------------------------- ### createColors(enabled) Source: https://github.com/ai/nanocolors Creates a color function object with configurable color support. The enabled parameter controls whether escape codes are emitted or plain strings are returned. ```APIDOC ## createColors(enabled) ### Description Creates a color function object with configurable color support. The `enabled` parameter controls whether escape codes are emitted or plain strings are returned. ### Parameters - **enabled** (boolean | undefined) - Optional - If `true`, always emits escape codes; if `false`, always returns plain strings; if `undefined`, falls back to the module-level `isColorSupported` constant. ### Usage Example ```js import { createColors } from 'nanocolors' // Respect the library's auto-detection, but allow an app-level override const { red } = createColors(options.colors === undefined ? undefined : options.colors) ``` ``` -------------------------------- ### color() wrapper function (index.js:22-39) Source: https://github.com/ai/nanocolors Internal wrapper that creates a styling function. Returns an empty string for empty input, wraps falsy non-empty values, and handles nested styling by replacing close sequences with restore. Never throws. ```javascript function color(open, close, closeRegexp, restore = open) { function func(s) { if (!s || !s.length) { if (s === '') { return '' } else { return open + s + close } } else { return ( open + (!!~s.indexOf(close, 4) ? s.replace(closeRegexp, restore) : s) + close ) } } return func } ``` -------------------------------- ### Colors Source: https://github.com/ai/nanocolors The `Colors` interface defines a set of color functions and a flag indicating whether color support is enabled. It is returned by `createColors` and provides methods for styling text with ANSI escape codes. ```APIDOC ## Colors ### Description The `Colors` interface defines a set of color functions and a flag indicating whether color support is enabled. It is returned by `createColors` and provides methods for styling text with ANSI escape codes. ### Interface Definition ```ts export interface Colors { isColorSupported: boolean reset: Color bold: Color dim: Color italic: Color underline: Color inverse: Color hidden: Color strikethrough: Color black: Color red: Color green: Color yellow: Color blue: Color magenta: Color cyan: Color white: Color gray: Color bgBlack: Color bgRed: Color bgGreen: Color bgYellow: Color bgBlue: Color bgMagenta: Color bgCyan: Color bgWhite: Color } ``` ### Fields - `isColorSupported` (boolean) - Required - Whether this color set emits escape codes. `true` from a Node `createColors(true)` call; `false` from `createColors(false)` and from every browser build. - `reset` (Color) - Required - Resets all attributes. - `bold` (Color) - Required - Bold/strong text. - `dim` (Color) - Required - Dim/faint text. - `italic` (Color) - Required - Italic text. - `underline` (Color) - Required - Underlined text. - `inverse` (Color) - Required - Reversed (inverted) video. - `hidden` (Color) - Required - Hidden (invisible) text. - `strikethrough` (Color) - Required - Struck-through text. - `black` … `white`, `gray` (Color) - Required - Foreground colors (ANSI codes `30–37`, `90`). - `bgBlack` … `bgWhite` (Color) - Required - Background colors (ANSI codes `40–47`). ### Usage Example ```ts import { createColors, type Colors } from 'nanocolors' const enabled: Colors = createColors(true) const disabled: Colors = createColors(false) function apply(colors: Colors, msg: string): string { return colors.isColorSupported ? colors.bold(colors.green(msg)) : msg } ``` ``` -------------------------------- ### Module exports signature (index.cjs:138–166) Source: https://github.com/ai/nanocolors The CommonJS build of nanocolors exports 25 keys: isColorSupported, createColors, and 24 color functions. This is the module.exports object assigned at index.cjs:138–166. ```javascript // index.cjs:138–166 module.exports = { isColorSupported, createColors, reset, bold, dim, italic, underline, inverse, hidden, strikethrough, black, red, green, yellow, blue, magenta, cyan, white, gray, bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite } ``` -------------------------------- ### createColors(enabled?) Source: https://github.com/ai/nanocolors Creates a fresh, self-contained set of color functions. When enabled is true, returns functions that emit ANSI escape codes; when false, returns identity no-op functions. When omitted, falls back to the environment auto-detection result captured at module load. ```APIDOC ## createColors(enabled?) ### Description Creates a fresh, self-contained set of color functions. When enabled is true, returns functions that emit ANSI escape codes; when false, returns identity no-op functions. When omitted, falls back to the environment auto-detection result captured at module load. ### Signature ```js // index.js:48 export function createColors(enabled = isColorSupported) { ``` TypeScript (`index.d.ts:33`): ```ts export function createColors(enabled?: boolean): Colors ``` ### Parameters | Parameter | Type | Required | Default | Description | |-----------|------|----------|---------|-------------| | `enabled` | `boolean \| undefined` | no | `isColorSupported` (auto-detected value) | When `true` returns functions that emit ANSI escape codes; when `false` returns identity no-op functions. When omitted/`undefined`, falls back to the environment auto-detection result captured at module load. | ### Return Type `Colors` — an object with 25 keys: `isColorSupported` plus 24 color functions (`Color`). | Return member | Value with `enabled: true` | Value with `enabled: false` | |---------------|----------------------------|-----------------------------| | `isColorSupported` | `true` | `false` | | `reset` | `` s => `\x1b[0m${s}\x1b[0m` `` | `s => String(s)` | | `bold`, `dim` | via `color()` with restore sequence | `s => String(s)` | | `italic`, `underline`, `inverse`, `hidden`, `strikethrough` | via `color()` | `s => String(s)` | | `black` … `white`, `gray` | via `color()` (foreground close `\x1b[39m`) | `s => String(s)` | | `bgBlack` … `bgWhite` | via `color()` (background close `\x1b[49m`) | `s => String(s)` | When disabled, every color function is the shared `nope` function (`index.js:20`): `let nope = s => String(s)` — input is converted to a string and returned unchanged. ### Throws Never throws. All argument values (including `undefined`, `null`, numbers, objects) are accepted; non-string inputs are stringified through string concatenation inside `nope` or `color()`. ### Request Example Basic use: ```js import { createColors } from 'nanocolors' const { red, bold, isColorSupported } = createColors(true) console.log(red('task failed')) // \x1b[31mtask failed\x1b[39m console.log(red(bold('FATAL'))) // nested styles console.log(isColorSupported) // true ``` Respecting an application option with auto-detection fallback (as shown in the README): ```js import { createColors } from 'nanocolors' // undefined falls back to the module-level auto-detection, // so no explicit branching is needed const { red } = createColors(options.enableColors === undefined ? undefined : options.enableColors) ``` Disabled mode — pass `false` to force plain output regardless of the environment (e.g. for piping to a file): ```js import { createColors } from 'nanocolors' const { red, green } = createColors(false) red('text') // 'text' green(42) // '42' — coerced to string, no escape codes ``` ### Behavior Notes - The factory returns a **new** object each call; the returned color functions are independent of the module-level exports. - `enabled` is evaluated for truthiness; any truthy value enables colors, any falsy value (other than `undefined`) disables them. `undefined` triggers the default parameter `isColorSupported`. - The default parameter is captured from the module-level `isColorSupported` constant; that constant itself is computed once at load time and never re-evaluated after environment changes. ``` -------------------------------- ### Basic use of createColors Source: https://github.com/ai/nanocolors Creates a color set with colors enabled. red('task failed') outputs the ANSI escape sequence \x1b[31mtask failed\x1b[39m; red(bold('FATAL')) demonstrates nested styles; isColorSupported is true. ```js import { createColors } from 'nanocolors' const { red, bold, isColorSupported } = createColors(true) console.log(red('task failed')) // \x1b[31mtask failed\x1b[39m console.log(red(bold('FATAL'))) // nested styles console.log(isColorSupported) // true ``` -------------------------------- ### Color Functions Source: https://github.com/ai/nanocolors The module exports 24 color functions that apply styles to strings. These include reset, bold, dim, italic, underline, inverse, hidden, strikethrough, and various foreground and background colors. ```APIDOC ## Color Functions ### Description The module exports 24 color functions that apply styles to strings. These include reset, bold, dim, italic, underline, inverse, hidden, strikethrough, and various foreground and background colors. ### Functions - `reset` (function) - Resets styling. - `bold` (function) - Applies bold. - `dim` (function) - Applies dim. - `italic` (function) - Applies italic. - `underline` (function) - Applies underline. - `inverse` (function) - Applies inverse. - `hidden` (function) - Applies hidden. - `strikethrough` (function) - Applies strikethrough. - `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, `white`, `gray` (function) - Foreground colors. - `bgBlack`, `bgRed`, `bgGreen`, `bgYellow`, `bgBlue`, `bgMagenta`, `bgCyan`, `bgWhite` (function) - Background colors. ### Usage Example ```js const { red, bold } = require('nanocolors') console.log(red('text')) console.log(bold(red('nested'))) ``` ``` -------------------------------- ### createColors function signature (TypeScript) Source: https://github.com/ai/nanocolors TypeScript declaration from index.d.ts:33. Returns a Colors object with 25 keys: isColorSupported plus 24 color functions. ```ts export function createColors(enabled?: boolean): Colors ``` -------------------------------- ### Edge cases: non-string input, empty string, disabled mode Source: https://github.com/ai/nanocolors Demonstrates behavior with non-string inputs, empty strings, and disabled colors. Numbers and undefined are accepted and wrapped; empty strings are never wrapped. createColors(false) disables colors, returning plain strings. ```javascript import { red, createColors } from 'nanocolors' red(1) // '\x1b[31m1\x1b[39m' — numbers accepted red('') // '' — empty string is never wrapped red(undefined) // '\x1b[31mundefined\x1b[39m' const plain = createColors(false) plain.red(1) // '1' plain.red('') // '' ``` -------------------------------- ### createColors function signature (JavaScript) Source: https://github.com/ai/nanocolors Exported from index.js (ESM) and index.cjs (CJS). Creates a fresh, self-contained set of color functions. The default value is the module-level isColorSupported constant evaluated once at load time. ```js // index.js:48 export function createColors(enabled = isColorSupported) { ``` -------------------------------- ### module.exports Source: https://github.com/ai/nanocolors The CommonJS module exports an object containing color functions and utilities. It is selected by Node.js require('nanocolors') and provides the same API as the ESM build. ```APIDOC ## module.exports ### Description The CommonJS module exports an object with color functions and utilities. It is selected by Node.js `require('nanocolors')` and provides the same API as the ESM build. ### Exports - `isColorSupported` (boolean) - Computed once at load. - `createColors` (function) - Creates a custom color instance with an optional enabled flag. - 24 color functions (function) - Each applies a color/style to a string. ### Usage Example ```js const { red, bold, isColorSupported, createColors } = require('nanocolors') console.log(red('via require')) console.log(bold(red('nested'))) if (isColorSupported) { /* TTY output */ } const custom = createColors(process.env.ENABLE_COLORS === '1') ``` ``` -------------------------------- ### One call per exported function Source: https://github.com/ai/nanocolors Shows the output of each exported color function when colors are enabled. Each function wraps its input with the corresponding ANSI escape codes. ```javascript import { reset, bold, dim, italic, underline, inverse, hidden, strikethrough, black, red, green, yellow, blue, magenta, cyan, white, gray, bgBlack, bgRed, bgGreen, bgYellow, bgBlue, bgMagenta, bgCyan, bgWhite } from 'nanocolors' reset('x') // \x1b[0mx\x1b[0m bold('x') // \x1b[1mx\x1b[22m dim('x') // \x1b[2mx\x1b[22m italic('x') // \x1b[3mx\x1b[23m underline('x') // \x1b[4mx\x1b[24m inverse('x') // \x1b[7mx\x1b[27m hidden('x') // \x1b[8mx\x1b[28m strikethrough('x') // \x1b[9mx\x1b[29m black('x') // \x1b[30mx\x1b[39m red('x') // \x1b[31mx\x1b[39m green('x') // \x1b[32mx\x1b[39m yellow('x') // \x1b[33mx\x1b[39m blue('x') // \x1b[34mx\x1b[39m magenta('x') // \x1b[35mx\x1b[39m cyan('x') // \x1b[36mx\x1b[39m white('x') // \x1b[37mx\x1b[39m gray('x') // \x1b[90mx\x1b[39m bgBlack('x') // \x1b[40mx\x1b[49m bgRed('x') // \x1b[41mx\x1b[49m bgGreen('x') // \x1b[42mx\x1b[49m bgYellow('x') // \x1b[43mx\x1b[49m bgBlue('x') // \x1b[44mx\x1b[49m bgMagenta('x') // \x1b[45mx\x1b[49m bgCyan('x') // \x1b[46mx\x1b[49m bgWhite('x') // \x1b[47mx\x1b[49m ``` -------------------------------- ### bold Source: https://github.com/ai/nanocolors Applies bold styling to the input string. This function wraps the input with ANSI escape codes to enable bold text formatting. ```APIDOC ## bold ### Description Applies bold styling to the input string. This function wraps the input with ANSI escape codes to enable bold text formatting. ### Method Function ### Signature `bold(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply bold styling to. ### Response #### Success Response - **string** - The input wrapped in ANSI bold codes. #### Example ```js bold('x') // \x1b[1mx\x1b[22m ``` ``` -------------------------------- ### underline Source: https://github.com/ai/nanocolors Applies underline styling to the input string. This function wraps the input with ANSI escape codes to enable underlined text formatting. ```APIDOC ## underline ### Description Applies underline styling to the input string. This function wraps the input with ANSI escape codes to enable underlined text formatting. ### Method Function ### Signature `underline(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply underline styling to. ### Response #### Success Response - **string** - The input wrapped in ANSI underline codes. #### Example ```js underline('x') // \x1b[4mx\x1b[24m ``` ``` -------------------------------- ### Colors interface definition (index.d.ts:3–30) Source: https://github.com/ai/nanocolors Exact definition of the Colors interface, listing all color members and isColorSupported. ```ts export interface Colors { isColorSupported: boolean reset: Color bold: Color dim: Color italic: Color underline: Color inverse: Color hidden: Color strikethrough: Color black: Color red: Color green: Color yellow: Color blue: Color magenta: Color cyan: Color white: Color gray: Color bgBlack: Color bgRed: Color bgGreen: Color bgYellow: Color bgBlue: Color bgMagenta: Color bgCyan: Color bgWhite: Color } ``` -------------------------------- ### Disabled mode with createColors(false) Source: https://github.com/ai/nanocolors Pass false to force plain output regardless of the environment, e.g. for piping to a file. red('text') returns 'text'; green(42) returns '42' — coerced to string, no escape codes. ```js import { createColors } from 'nanocolors' const { red, green } = createColors(false) red('text') // 'text' green(42) // '42' — coerced to string, no escape codes ``` -------------------------------- ### inverse Source: https://github.com/ai/nanocolors Applies inverse (reverse video) styling to the input string. This function swaps the foreground and background colors of the text. ```APIDOC ## inverse ### Description Applies inverse (reverse video) styling to the input string. This function swaps the foreground and background colors of the text. ### Method Function ### Signature `inverse(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply inverse styling to. ### Response #### Success Response - **string** - The input wrapped in ANSI inverse codes. #### Example ```js inverse('x') // \x1b[7mx\x1b[27m ``` ``` -------------------------------- ### createColors Source: https://github.com/ai/nanocolors Creates a custom color function set with color output enabled or disabled. When disabled, the returned functions return the input unchanged without any ANSI codes. ```APIDOC ## createColors ### Description Creates a custom color function set with color output enabled or disabled. When disabled, the returned functions return the input unchanged without any ANSI codes. ### Method Function ### Signature `createColors(enabled: boolean): ColorFunctions` ### Parameters - **enabled** (boolean) - Required - Whether color output should be enabled (`true`) or disabled (`false`). ### Response #### Success Response - **ColorFunctions** - An object containing all color and style functions (e.g., `red`, `bold`, `bgYellow`) that respect the `enabled` setting. #### Example ```js const plain = createColors(false) plain.red(1) // '1' plain.red('') // '' ``` ``` -------------------------------- ### gray Source: https://github.com/ai/nanocolors Applies gray foreground color to the input string. This function wraps the input with ANSI escape codes to set the text color to gray. ```APIDOC ## gray ### Description Applies gray foreground color to the input string. This function wraps the input with ANSI escape codes to set the text color to gray. ### Method Function ### Signature `gray(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to color gray. ### Response #### Success Response - **string** - The input wrapped in ANSI gray color codes. #### Example ```js gray('x') // \x1b[90mx\x1b[39m ``` ``` -------------------------------- ### strikethrough Source: https://github.com/ai/nanocolors Applies strikethrough styling to the input string. This function draws a horizontal line through the text. ```APIDOC ## strikethrough ### Description Applies strikethrough styling to the input string. This function draws a horizontal line through the text. ### Method Function ### Signature `strikethrough(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply strikethrough styling to. ### Response #### Success Response - **string** - The input wrapped in ANSI strikethrough codes. #### Example ```js strikethrough('x') // \x1b[9mx\x1b[29m ``` ``` -------------------------------- ### dim Source: https://github.com/ai/nanocolors Applies dim (faint) styling to the input string. This function wraps the input with ANSI escape codes to reduce the intensity of the text. ```APIDOC ## dim ### Description Applies dim (faint) styling to the input string. This function wraps the input with ANSI escape codes to reduce the intensity of the text. ### Method Function ### Signature `dim(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply dim styling to. ### Response #### Success Response - **string** - The input wrapped in ANSI dim codes. #### Example ```js dim('x') // \x1b[2mx\x1b[22m ``` ``` -------------------------------- ### Color type definition (index.d.ts:1) Source: https://github.com/ai/nanocolors Exact definition of the Color type, a function type that takes a string or number and returns a string. ```ts export type Color = (text: string | number) => string ``` -------------------------------- ### bgGreen Source: https://github.com/ai/nanocolors Applies green background color to the input string. This function wraps the input with ANSI escape codes to set the background color to green. ```APIDOC ## bgGreen ### Description Applies green background color to the input string. This function wraps the input with ANSI escape codes to set the background color to green. ### Method Function ### Signature `bgGreen(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply a green background to. ### Response #### Success Response - **string** - The input wrapped in ANSI green background codes. #### Example ```js bgGreen('x') // \x1b[42mx\x1b[49m ``` ``` -------------------------------- ### white Source: https://github.com/ai/nanocolors Applies white foreground color to the input string. This function wraps the input with ANSI escape codes to set the text color to white. ```APIDOC ## white ### Description Applies white foreground color to the input string. This function wraps the input with ANSI escape codes to set the text color to white. ### Method Function ### Signature `white(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to color white. ### Response #### Success Response - **string** - The input wrapped in ANSI white color codes. #### Example ```js white('x') // \x1b[37mx\x1b[39m ``` ``` -------------------------------- ### bgBlue Source: https://github.com/ai/nanocolors Applies blue background color to the input string. This function wraps the input with ANSI escape codes to set the background color to blue. ```APIDOC ## bgBlue ### Description Applies blue background color to the input string. This function wraps the input with ANSI escape codes to set the background color to blue. ### Method Function ### Signature `bgBlue(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply a blue background to. ### Response #### Success Response - **string** - The input wrapped in ANSI blue background codes. #### Example ```js bgBlue('x') // \x1b[44mx\x1b[49m ``` ``` -------------------------------- ### hidden Source: https://github.com/ai/nanocolors Applies hidden styling to the input string. This function makes the text invisible while still occupying space. ```APIDOC ## hidden ### Description Applies hidden styling to the input string. This function makes the text invisible while still occupying space. ### Method Function ### Signature `hidden(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply hidden styling to. ### Response #### Success Response - **string** - The input wrapped in ANSI hidden codes. #### Example ```js hidden('x') // \x1b[8mx\x1b[28m ``` ``` -------------------------------- ### Color Source: https://github.com/ai/nanocolors The `Color` type is a function type that takes a string or number and returns a string. It is used to type color functions that wrap input in ANSI escape codes. ```APIDOC ## Color ### Description The `Color` type is a function type that takes a string or number and returns a string. It is used to type color functions that wrap input in ANSI escape codes. ### Type Definition ```ts export type Color = (text: string | number) => string ``` ### Parameters - `text` (string | number) - Optional - Input to wrap in ANSI escape codes. At runtime any value is accepted and stringified via concatenation, but the declared type is `string | number`. ### Returns - `string` - The input wrapped in `open`/`close` escape sequences when colors are enabled; the plain stringified input when disabled; `''` for an empty-string input. ### Usage Example ```ts import { type Color, red } from 'nanocolors' const paint: Color = red // red matches the Color signature function badge(text: string, style: Color): string { return style(`[ ${text} ]`) } console.log(badge('OK', red)) ``` ``` -------------------------------- ### bgWhite Source: https://github.com/ai/nanocolors Applies white background color to the input string. This function wraps the input with ANSI escape codes to set the background color to white. ```APIDOC ## bgWhite ### Description Applies white background color to the input string. This function wraps the input with ANSI escape codes to set the background color to white. ### Method Function ### Signature `bgWhite(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply a white background to. ### Response #### Success Response - **string** - The input wrapped in ANSI white background codes. #### Example ```js bgWhite('x') // \x1b[47mx\x1b[49m ``` ``` -------------------------------- ### bgYellow Source: https://github.com/ai/nanocolors Applies yellow background color to the input string. This function wraps the input with ANSI escape codes to set the background color to yellow. ```APIDOC ## bgYellow ### Description Applies yellow background color to the input string. This function wraps the input with ANSI escape codes to set the background color to yellow. ### Method Function ### Signature `bgYellow(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to apply a yellow background to. ### Response #### Success Response - **string** - The input wrapped in ANSI yellow background codes. #### Example ```js bgYellow('x') // \x1b[43mx\x1b[49m ``` ``` -------------------------------- ### cyan Source: https://github.com/ai/nanocolors Applies cyan foreground color to the input string. This function wraps the input with ANSI escape codes to set the text color to cyan. ```APIDOC ## cyan ### Description Applies cyan foreground color to the input string. This function wraps the input with ANSI escape codes to set the text color to cyan. ### Method Function ### Signature `cyan(input: string | number): string` ### Parameters - **input** (string | number) - Required - The text or value to color cyan. ### Response #### Success Response - **string** - The input wrapped in ANSI cyan color codes. #### Example ```js cyan('x') // \x1b[36mx\x1b[39m ``` ```