node-comfortv2.0.0

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"]));
GuideExplanations and examples for nc.color.
Read the guide →

Functions

nc.color.levelproperty

level: ColorLevel

The current color level. Set it to force one; 0 turns colors off.

nc.color.enabledproperty

enabled: boolean

Whether colors are being output.

nc.color.create()

create(level?: ColorLevel): ColorModule

A separate instance with its own level, for another stream or for tests.

Parameters

NameType
leveloptionalColorLevel | undefined

nc.color.detect()

detect(stream?: WriteStream | { isTTY?: boolean; } | undefined): ColorLevel

The color level a stream supports, taking NO_COLOR and FORCE_COLOR into account.

Parameters

NameType
streamoptionalNodeJS.WriteStream | { isTTY?: boolean; } | undefined

nc.color.strip()

strip(text: string): string

Removes colors and other terminal escape codes.

Parameters

NameType
textstring

nc.color.width()

width(text: string): number

How many columns the text takes, ignoring colors and counting emoji and CJK as 2.

Parameters

NameType
textstring

nc.color.gradient()

gradient(text: string, colors: string[]): string

Colors the text along a gradient of hex colors.

Parameters

NameType
textstring
colorsstring[]

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 & ColorMethods

ColorHelpers

Color helpers.

PropertyTypeDescription
levelColorLevelThe current color level. Set it to force one; 0 turns colors off.
enabledbooleanWhether colors are being output.
create(level?: ColorLevel) => ColorModuleA separate instance with its own level, for another stream or for tests.
detect(stream?: WriteStream | { isTTY?: boolean; } | undefined) => ColorLevelThe color level a stream supports, taking NO_COLOR and FORCE_COLOR into account.
strip(text: string) => stringRemoves colors and other terminal escape codes.
width(text: string) => numberHow many columns the text takes, ignoring colors and counting emoji and CJK as 2.
link(text: string, url: string) => stringA clickable link in terminals that support it, "text (url)" elsewhere.
gradient(text: string, colors: string[]) => stringColors 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 | 3

ColorMethods

Custom colors, available on every chain.

PropertyTypeDescription
rgb(r: number, g: number, b: number) => ColorChainText color from red, green and blue (0-255).
bgRgb(r: number, g: number, b: number) => ColorChainBackground from red, green and blue (0-255).
hex(hex: string) => ColorChainText color from a hex code like "#f80".
bgHex(hex: string) => ColorChainBackground from a hex code.
ansi256(code: number) => ColorChainText color from the 256-color palette.
bgAnsi256(code: number) => ColorChainBackground from the 256-color palette.

ColorModule

nc.color: every style and color, plus a few helpers.

ColorStyleProps

The named styles. Each one can be called on text or chained with others: color.red("x"), color.red.bold.underline("x").

PropertyTypeDescription
resetColorChainRemoves all styles.
boldColorChainBold text.
dimColorChainFaint text.
italicColorChainItalic text, where the terminal supports it.
underlineColorChainUnderlined text.
overlineColorChainLine above the text, where supported.
inverseColorChainSwaps text and background colors.
hiddenColorChainInvisible text, still copyable.
strikethroughColorChainCrossed-out text.
blackColorChainBlack text.
redColorChainRed text.
greenColorChainGreen text.
yellowColorChainYellow text.
blueColorChainBlue text.
magentaColorChainMagenta text.
cyanColorChainCyan text.
whiteColorChainWhite text.
grayColorChainGray text.
greyColorChainSame as gray.
blackBrightColorChainBright black text.
redBrightColorChainBright red text.
greenBrightColorChainBright green text.
yellowBrightColorChainBright yellow text.
blueBrightColorChainBright blue text.
magentaBrightColorChainBright magenta text.
cyanBrightColorChainBright cyan text.
whiteBrightColorChainBright white text.
bgBlackColorChainBlack background.
bgRedColorChainRed background.
bgGreenColorChainGreen background.
bgYellowColorChainYellow background.
bgBlueColorChainBlue background.
bgMagentaColorChainMagenta background.
bgCyanColorChainCyan background.
bgWhiteColorChainWhite background.
bgGrayColorChainGray background.
bgGreyColorChainSame as bgGray.
bgBlackBrightColorChainBright black background.
bgRedBrightColorChainBright red background.
bgGreenBrightColorChainBright green background.
bgYellowBrightColorChainBright yellow background.
bgBlueBrightColorChainBright blue background.
bgMagentaBrightColorChainBright magenta background.
bgCyanBrightColorChainBright cyan background.
bgWhiteBrightColorChainBright 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"
node-comfort v2.0.0View the source