nc.color
Terminal colors you can chain, like chalk. Truecolor is supported and downgraded on older terminals. Colors switch off by themselves when the output isn't a terminal or NO_COLOR is set; FORCE_COLOR=1|2|3 turns them back on.
const { color } = require("@ix-xs/node-comfort");
import { create } from "@ix-xs/node-comfort/color";console.log(color.green.bold("✔ Done"), color.gray("in 1.2s"));
console.log(color.bgHex("#1e1e2e").hex("#cba6f7")(" nc "));
console.log(color.gradient("node-comfort", ["#ff5f6d", "#ffc371"]));nc.color.Functions
nc.color.levelproperty
level: ColorLevelThe current color level. Set it to force one; 0 turns colors off.
nc.color.enabledproperty
enabled: booleanWhether colors are being output.
nc.color.create()
create(level?: ColorLevel): ColorModuleA separate instance with its own level, for another stream or for tests.
Parameters
| Name | Type |
|---|---|
leveloptional | ColorLevel | undefined |
nc.color.detect()
detect(stream?: WriteStream | { isTTY?: boolean; } | undefined): ColorLevelThe color level a stream supports, taking NO_COLOR and FORCE_COLOR into account.
Parameters
| Name | Type |
|---|---|
streamoptional | NodeJS.WriteStream | { isTTY?: boolean; } | undefined |
nc.color.strip()
strip(text: string): stringRemoves colors and other terminal escape codes.
Parameters
| Name | Type |
|---|---|
text | string |
nc.color.width()
width(text: string): numberHow many columns the text takes, ignoring colors and counting emoji and CJK as 2.
Parameters
| Name | Type |
|---|---|
text | string |
nc.color.link()
link(text: string, url: string): stringA clickable link in terminals that support it, "text (url)" elsewhere.
Parameters
| Name | Type |
|---|---|
text | string |
url | string |
nc.color.gradient()
gradient(text: string, colors: string[]): stringColors the text along a gradient of hex colors.
Parameters
| Name | Type |
|---|---|
text | string |
colors | string[] |
Also available
reset bold dim italic underline overline inverse hidden strikethrough black red green yellow blue magenta cyan white gray grey blackBright redBright greenBright yellowBright blueBright magentaBright cyanBright whiteBright bgBlack bgRed bgGreen bgYellow bgBlue bgMagenta bgCyan bgWhite bgGray bgGrey bgBlackBright bgRedBright bgGreenBright bgYellowBright bgBlueBright bgMagentaBright bgCyanBright bgWhiteBright rgb bgRgb hex bgHex ansi256 bgAnsi256
Types
Import any of them in TypeScript with import type { ColorChain } from "@ix-xs/node-comfort", or in JavaScript with import("@ix-xs/node-comfort").ColorChain.
ColorChain
A style you can call on text, or chain with more styles.
type ColorChain = ((...text: unknown[]) => string) & ColorStyles & ColorMethodsColorHelpers
Color helpers.
| Property | Type | Description |
|---|---|---|
level | ColorLevel | The current color level. Set it to force one; 0 turns colors off. |
enabled | boolean | Whether colors are being output. |
create | (level?: ColorLevel) => ColorModule | A separate instance with its own level, for another stream or for tests. |
detect | (stream?: WriteStream | { isTTY?: boolean; } | undefined) => ColorLevel | The color level a stream supports, taking NO_COLOR and FORCE_COLOR into account. |
strip | (text: string) => string | Removes colors and other terminal escape codes. |
width | (text: string) => number | How many columns the text takes, ignoring colors and counting emoji and CJK as 2. |
link | (text: string, url: string) => string | A clickable link in terminals that support it, "text (url)" elsewhere. |
gradient | (text: string, colors: string[]) => string | Colors the text along a gradient of hex colors. |
ColorLevel
How many colors the terminal can show: 0 none, 1 16, 2 256, 3 millions.
type ColorLevel = 0 | 1 | 2 | 3ColorMethods
Custom colors, available on every chain.
| Property | Type | Description |
|---|---|---|
rgb | (r: number, g: number, b: number) => ColorChain | Text color from red, green and blue (0-255). |
bgRgb | (r: number, g: number, b: number) => ColorChain | Background from red, green and blue (0-255). |
hex | (hex: string) => ColorChain | Text color from a hex code like "#f80". |
bgHex | (hex: string) => ColorChain | Background from a hex code. |
ansi256 | (code: number) => ColorChain | Text color from the 256-color palette. |
bgAnsi256 | (code: number) => ColorChain | Background from the 256-color palette. |
ColorModule
nc.color: every style and color, plus a few helpers.
type ColorModule = ColorStyles & ColorMethods & ColorHelpersColorStyleProps
The named styles. Each one can be called on text or chained with others: color.red("x"), color.red.bold.underline("x").
| Property | Type | Description |
|---|---|---|
reset | ColorChain | Removes all styles. |
bold | ColorChain | Bold text. |
dim | ColorChain | Faint text. |
italic | ColorChain | Italic text, where the terminal supports it. |
underline | ColorChain | Underlined text. |
overline | ColorChain | Line above the text, where supported. |
inverse | ColorChain | Swaps text and background colors. |
hidden | ColorChain | Invisible text, still copyable. |
strikethrough | ColorChain | Crossed-out text. |
black | ColorChain | Black text. |
red | ColorChain | Red text. |
green | ColorChain | Green text. |
yellow | ColorChain | Yellow text. |
blue | ColorChain | Blue text. |
magenta | ColorChain | Magenta text. |
cyan | ColorChain | Cyan text. |
white | ColorChain | White text. |
gray | ColorChain | Gray text. |
grey | ColorChain | Same as gray. |
blackBright | ColorChain | Bright black text. |
redBright | ColorChain | Bright red text. |
greenBright | ColorChain | Bright green text. |
yellowBright | ColorChain | Bright yellow text. |
blueBright | ColorChain | Bright blue text. |
magentaBright | ColorChain | Bright magenta text. |
cyanBright | ColorChain | Bright cyan text. |
whiteBright | ColorChain | Bright white text. |
bgBlack | ColorChain | Black background. |
bgRed | ColorChain | Red background. |
bgGreen | ColorChain | Green background. |
bgYellow | ColorChain | Yellow background. |
bgBlue | ColorChain | Blue background. |
bgMagenta | ColorChain | Magenta background. |
bgCyan | ColorChain | Cyan background. |
bgWhite | ColorChain | White background. |
bgGray | ColorChain | Gray background. |
bgGrey | ColorChain | Same as bgGray. |
bgBlackBright | ColorChain | Bright black background. |
bgRedBright | ColorChain | Bright red background. |
bgGreenBright | ColorChain | Bright green background. |
bgYellowBright | ColorChain | Bright yellow background. |
bgBlueBright | ColorChain | Bright blue background. |
bgMagentaBright | ColorChain | Bright magenta background. |
bgCyanBright | ColorChain | Bright cyan background. |
bgWhiteBright | ColorChain | Bright white background. |
ColorStyles
The named styles, read-only.
type ColorStyles = Readonly<ColorStyleProps>StyleEntry
A style in a chain: fixed codes, or a color resolved at output time.
type StyleEntry = { open: string; close: string; } | { rgb: [number, number, number]; bg: boolean; } | { ansi256: number; bg: boolean; }StyleName
Every named style.
type StyleName = "reset" | "bold" | "dim" | "italic" | "underline" | "overline" | "inverse" | "hidden" | "strikethrough" | "black" | "red" | "green" | "yellow" | "blue" | "magenta" | "cyan" | "white" | "gray" | "grey" | "blackBright" | "redBright" | "greenBright" | "yellowBright" | "blueBright" | "magentaBright" | "cyanBright" | "whiteBright" | "bgBlack" | "bgRed" | "bgGreen" | "bgYellow" | "bgBlue" | "bgMagenta" | "bgCyan" | "bgWhite" | "bgGray" | "bgGrey" | "bgBlackBright" | "bgRedBright" | "bgGreenBright" | "bgYellowBright" | "bgBlueBright" | "bgMagentaBright" | "bgCyanBright" | "bgWhiteBright"