shelving/util/ansimodule

Constants and a helper for wrapping terminal output in ANSI escape codes. Use these when writing CLI tools or test reporters that need colour and style without pulling in a third-party library.

  • All colour/style constants are plain escape-code strings, so you can concatenate them yourself or pass them to ansiWrap().
  • ansiWrap is a no-op when the NO_COLOR environment variable is set, following the no-color.org convention.

Usage

Wrapping text in colour or style

ts
import { ansiWrap, ANSI_RED, ANSI_BOLD } from "shelving/util";

console.log(ansiWrap("Error!", ANSI_RED));
console.log(ansiWrap("Warning", ANSI_YELLOW, ANSI_BOLD)); // multiple wrappers

Pre-coloured status icons

ts
import { ANSI_SUCCESS, ANSI_FAILURE, ANSI_WAITING } from "shelving/util";

console.log(ANSI_SUCCESS); // ✓ in green
console.log(ANSI_FAILURE); // ✗ in red
console.log(ANSI_WAITING); // ⋯ in blue

Arrow icons (ANSI_UP, ANSI_DOWN, ANSI_LEFT, ANSI_RIGHT) are also available in blue.

Using raw escape constants

ts
import { ANSI_CYAN, ANSI_UNDERLINE, ANSI_RESET } from "shelving/util";

const styled = `${ANSI_CYAN}${ANSI_UNDERLINE}link text${ANSI_RESET}`;

Functions

Go

ansiWrap()function

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

ansiWrap(input: string, ...wrappers: ImmutableArray<string>): void

Types

Go

AnsiIcontype

A lazily-coloured icon that re-evaluates its ANSI colouring against the live NO_COLOR environment variable every time it is converted to a string.

{
	toString(): string;
}

Constants

Go

ANSI_DEFAULTconstant

ANSI escape code that resets the foreground colour to the terminal default.

Go

ANSI_BLACKconstant

ANSI escape code that sets the foreground colour to black.

Go

ANSI_REDconstant

ANSI escape code that sets the foreground colour to red.

Go

ANSI_GREENconstant

ANSI escape code that sets the foreground colour to green.

Go

ANSI_YELLOWconstant

ANSI escape code that sets the foreground colour to yellow.

Go

ANSI_BLUEconstant

ANSI escape code that sets the foreground colour to blue.

Go

ANSI_MAGENTAconstant

ANSI escape code that sets the foreground colour to magenta.

Go

ANSI_CYANconstant

ANSI escape code that sets the foreground colour to cyan.

Go

ANSI_WHITEconstant

ANSI escape code that sets the foreground colour to white.

Go

ANSI_BOLDconstant

ANSI escape code that enables bold text.

Go

ANSI_ITALICconstant

ANSI escape code that enables italic text.

Go

ANSI_UNDERLINEconstant

ANSI escape code that enables underlined text.

Go

ANSI_STRIKEconstant

ANSI escape code that enables strikethrough text.

Go

ANSI_INVERSEconstant

ANSI escape code that enables inverse (swapped foreground/background) text.

Go

ANSI_RESETconstant

ANSI escape code that resets all colour and style attributes.

Go

ANSI_WAITINGconstant

Lazily blue-coloured waiting icon (⋯) for use in template literals.

Go

ANSI_SUCCESSconstant

Lazily green-coloured success icon (✓) for use in template literals.

Go

ANSI_FAILUREconstant

Lazily red-coloured failure icon (✗) for use in template literals.

Go

ANSI_UPconstant

Lazily blue-coloured up arrow icon (↑) for use in template literals.

Go

ANSI_DOWNconstant

Lazily blue-coloured down arrow icon (↓) for use in template literals.

Go

ANSI_RIGHTconstant

Lazily blue-coloured right arrow icon (→) for use in template literals.

Go

ANSI_LEFTconstant

Lazily blue-coloured left arrow icon (←) for use in template literals.