ansiWrap()function

ansiWrap(input: string, ...wrappers: ImmutableArray<string>): void
ParamType
inputstring
The string to wrap in ANSI codes. required
wrappersImmutableArray<string>
Any number of ANSI escape codes (e.g. ANSI_RED, ANSI_BOLD) to prepend before input. required
Return
unknown
The wrapped string, or input unchanged when the NO_COLOR environment variable is set.

Wrap a string in the ANSI color/style codes (at the start), and ANSI_RESET at the end.

  • The NO_COLOR environment variable is read live on every call, so runtimes that populate process.env late (e.g. Cloudflare Workers, where [vars] bindings are only reliably available within the request scope) are honoured rather than baking in whatever NO_COLOR was at module-load time.

Examples

ansiWrap("hello", ANSI_RED, ANSI_BOLD) // "\x1b[31m\x1b[1mhello\x1b[0m"