Colorize 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_COLORS
friendly- automatic color support detection
- no dependencies
- typescript support
This package is ESM only.
In Node.js with yarn:
yarn add @flex-development/colors
See 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>
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
Remove ANSI escape codes from value
.
value
(unknown
) — the string or value to remove escape codes fromtoString
(ToString
, optional) — convertvalue
to a string- default:
String
- default:
(string
) Stringified value
with ANSI escape codes removed
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
) — reset sequenceopen
(string
) — sequence used to set colorsreplace
(string
, optional) — reset sequence replacement
(string
) Colorized string value
Registry of color functions (TypeScript interface).
Union of color names.
type Color = keyof ColorFunctions
To 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) — color output enabled?colors
(Partial<ColorConfigs>
, optional) — color function config map
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 supportedsupported
(boolean
) — color output supported? (readonly
)
Convert value
to a string (TypeScript type).
value
(unknown
) — the thing to stringify
(string
) Stringified value
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.