Skip to content
Jennifer Programming Language

ansi API reference

Terminal styling as explicit string wrappers. The first module built on Jennifer's module system: pure Jennifer, no Go. Colour is gated on stdout being a terminal (with the NO_COLOR / FORCE_COLOR override), so wrapped output stays clean when it is redirected to a file or a pipe.

Import with import "ansi.j" as ansi;. See the ansi guide for prose and examples.

Functions

ansi.bgColor(s as string, name as string)

Wrap a string in the named background colour.

Parameters

  • s {string} - the text to colourize
  • name {string} - the background colour name (e.g. "red", "blue")

Returns {string} - the wrapped text, or s unchanged when colour is off

Throws

  • {Error} - when name is not a known background colour

ansi.bgColor256(s as string, n as int)

Wrap a string in a 256-colour palette background (xterm 48;5;n).

Parameters

  • s {string} - the text to colourize
  • n {int} - the palette index (0-255)

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.black(s as string)

Wrap a string in black foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.blue(s as string)

Wrap a string in blue foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.bold(s as string)

Wrap a string in the bold text style.

Parameters

  • s {string} - the text to style

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.color(s as string, name as string)

Wrap a string in the named foreground colour.

Parameters

  • s {string} - the text to colourize
  • name {string} - the colour name (e.g. "red", "green", "cyan")

Returns {string} - the wrapped text, or s unchanged when colour is off

Throws

  • {Error} - when name is not a known colour

ansi.color256(s as string, n as int)

Wrap a string in a 256-colour palette foreground (xterm 38;5;n), the indexed middle rung between the named colours and 24-bit rgb.

Parameters

  • s {string} - the text to colourize
  • n {int} - the palette index (0-15 base, 16-231 cube, 232-255 grayscale)

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.cyan(s as string)

Wrap a string in cyan foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.dim(s as string)

Wrap a string in the dim text style.

Parameters

  • s {string} - the text to style

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.gray(s as string)

Wrap a string in gray foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.green(s as string)

Wrap a string in green foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.italic(s as string)

Wrap a string in the italic text style.

Parameters

  • s {string} - the text to style

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.magenta(s as string)

Wrap a string in magenta foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.red(s as string)

Wrap a string in red foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.reverse(s as string)

Wrap a string in the reverse (inverted) text style.

Parameters

  • s {string} - the text to style

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.rgb(s as string, r as int, g as int, b as int)

Wrap a string in a 24-bit truecolor foreground.

Parameters

  • s {string} - the text to colourize
  • r {int} - the red channel (0-255)
  • g {int} - the green channel (0-255)
  • b {int} - the blue channel (0-255)

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.rgbToColor256(r as int, g as int, b as int)

Map a 24-bit RGB triple to the nearest 256-colour index, for terminals without truecolor. Picks the closer of the 6x6x6 colour cube (16-231) and the 24-step grayscale ramp (232-255).

Parameters

  • r {int} - the red channel (0-255)
  • g {int} - the green channel (0-255)
  • b {int} - the blue channel (0-255)

Returns {int} - a palette index in [16, 255]

ansi.strike(s as string)

Wrap a string in the strikethrough (crossed-out) text style.

Parameters

  • s {string} - the text to style

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.strip(s as string)

Remove every SGR escape - the inverse of the wrappers, regardless of whether colour is currently enabled.

Parameters

  • s {string} - the text to strip

Returns {string} - the text with all SGR escapes removed

ansi.style(s as string, name as string)

Wrap a string in the named text style.

Parameters

  • s {string} - the text to style
  • name {string} - the style name (e.g. "bold", "italic", "underline")

Returns {string} - the wrapped text, or s unchanged when colour is off

Throws

  • {Error} - when name is not a known style

ansi.underline(s as string)

Wrap a string in the underline text style.

Parameters

  • s {string} - the text to style

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.white(s as string)

Wrap a string in white foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off

ansi.yellow(s as string)

Wrap a string in yellow foreground colour.

Parameters

  • s {string} - the text to colourize

Returns {string} - the wrapped text, or s unchanged when colour is off