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.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.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.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