Style messages in the terminal and browser
This is a small, but useful, library for creating colorful messages in the terminal and browser consoles that support ANSI colors.
FORCE_COLOR,NO_COLOR, andNODE_DISABLE_COLORSfriendly- automatic color support detection
- no dependencies
- typescript support
This package is ESM only.
In Node.js with yarn:
yarn add @flex-development/colorsSee Git - Protocols | Yarn for details regarding installing from Git.
In Deno with esm.sh:
import { colors } from 'https://esm.sh/@flex-development/colors'In browsers with esm.sh:
<script type="module">
import { createColors } from 'https://esm.sh/@flex-development/colors'
</script>With bun:
bun add @flex-development/colors
See bun add for more details.
import c from '@flex-development/colors'
console.log(c.red(c.bold('Hello World!')))This package exports the following identifiers:
The default export is colors.
(Colors) The default colorizer.
Create a colorizer.
options(ColorizerOptions|boolean, optional) — boolean indicating if color output should be enabled, or an options object to create a custom colorizer- default:
isColorSupported()
- default:
(Colors) Colorizer object
Check if color output is supported.
(boolean) true if color output is supported, false otherwise
This package is fully typed with TypeScript.
List, where the first value is the numeric code for an ANSI escape sequence, and the second is the numeric code for the reset sequence. The last value is an optional replacement for the reset sequence (used in complex cases to nest styles without breaking existing color sequences).
type ColorConfig = [
open: number,
close: number,
replace?: string | null | undefined
]Record, where each key is a Color and each value is the configuration for a color function.
type ColorConfigs = Record<Color, ColorConfig>Colorize value (TypeScript interface).
value(unknown) — the thing to colorize
close(string) — the reset sequenceopen(string) — the sequence used to set stylesreplace(string, optional) — the reset sequence replacement
(string) Colorized string value
Registry of color functions (TypeScript interface).
Union of color names.
type Color = keyof ColorFunctionsTo register custom colors, augment ColorFunctions. They will be added to this union automatically.
declare module '@flex-development/colors' {
interface ColorFunctions {
blink: ColorFunction
}
}Options for creating a colorizer (TypeScript interface).
color(boolean, optional) — whether color output should be enabledcolors(Partial<ColorConfigs>, optional) — record, where each key is a the name of a color or style and each value is the configuration for that color or style
also exported as
Colorizer
A colorizer (TypeScript interface).
Colorizers contain Color functions. Each function corresponds to a terminal color or style.
The color property is an accessor that can be used to disable or enable color output.
get color(): boolean— color output enabled?set color(color: boolean | null | undefined)- enable or disable color output
👉 note: color output will be disabled if not supported
- enable or disable color output
styles(ColorConfigs) — record, where each key is the name of a color or style and each value is the configuration for a color function (readonly)supported(boolean) — whether color output is supported (readonly)
See CONTRIBUTING.md.
This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.