Skip to content
Jennifer Programming Language

ansi API reference

Terminal styling as string wrappers (SGR escapes). Each wrapper ends with the reset for its own attribute only (39 foreground, 49 background, 22 bold/dim, ...), so wrappers nest: bold("a " + red("b") + " c") keeps " c" bold. Colour is gated on stdout being a terminal, with the NO_COLOR / FORCE_COLOR overrides, so redirected output stays clean.

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

Contents

NameWhat it is
bgColorWrap a string in the named background colour.
bgColor256Wrap a string in a 256-colour palette background (xterm 48;5;n).
blackWrap a string in black foreground colour.
blueWrap a string in blue foreground colour.
boldWrap a string in the bold text style.
colorWrap a string in the named foreground colour.
color256Wrap a string in a 256-colour palette foreground (xterm 38;5;n), the indexed middle rung between the name...
cyanWrap a string in cyan foreground colour.
dimWrap a string in the dim text style.
grayWrap a string in gray foreground colour.
greenWrap a string in green foreground colour.
italicWrap a string in the italic text style.
magentaWrap a string in magenta foreground colour.
redWrap a string in red foreground colour.
reverseWrap a string in the reverse (inverted) text style.
rgbWrap a string in a 24-bit truecolor foreground.
rgbToColor256Map a 24-bit RGB triple to the nearest 256-colour index, for terminals without truecolor.
strikeWrap a string in the strikethrough (crossed-out) text style.
stripRemove every terminal escape sequence - the inverse of the wrappers, regardless of whether colour is curren...
styleWrap a string in the named text style.
underlineWrap a string in the underline text style.
whiteWrap a string in white foreground colour.
yellowWrap a string in yellow foreground colour.

Functions

bgColor

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

Wrap a string in the named background colour.

s
the text to colourize
name
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

bgColor256

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

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

s
the text to colourize
n
the palette index (0-255)
Returns string
the wrapped text, or s unchanged when colour is off
Throws Error
kind "value" when n is outside 0-255

black

jennifer
ansi.black(s as string)

Wrap a string in black foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

blue

jennifer
ansi.blue(s as string)

Wrap a string in blue foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

bold

jennifer
ansi.bold(s as string)

Wrap a string in the bold text style.

s
the text to style
Returns string
the wrapped text, or s unchanged when colour is off

color

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

Wrap a string in the named foreground colour.

s
the text to colourize
name
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

color256

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

s
the text to colourize
n
the palette index (0-15 base, 16-231 cube, 232-255 grayscale)
Returns string
the wrapped text, or s unchanged when colour is off
Throws Error
kind "value" when n is outside 0-255

cyan

jennifer
ansi.cyan(s as string)

Wrap a string in cyan foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

dim

jennifer
ansi.dim(s as string)

Wrap a string in the dim text style.

s
the text to style
Returns string
the wrapped text, or s unchanged when colour is off

gray

jennifer
ansi.gray(s as string)

Wrap a string in gray foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

green

jennifer
ansi.green(s as string)

Wrap a string in green foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

italic

jennifer
ansi.italic(s as string)

Wrap a string in the italic text style.

s
the text to style
Returns string
the wrapped text, or s unchanged when colour is off

magenta

jennifer
ansi.magenta(s as string)

Wrap a string in magenta foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

red

jennifer
ansi.red(s as string)

Wrap a string in red foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

reverse

jennifer
ansi.reverse(s as string)

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

s
the text to style
Returns string
the wrapped text, or s unchanged when colour is off

rgb

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

Wrap a string in a 24-bit truecolor foreground.

s
the text to colourize
r
the red channel (0-255)
g
the green channel (0-255)
b
the blue channel (0-255)
Returns string
the wrapped text, or s unchanged when colour is off
Throws Error
kind "value" when a channel is outside 0-255

rgbToColor256

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

r
the red channel (0-255)
g
the green channel (0-255)
b
the blue channel (0-255)
Returns int
a palette index in [16, 255]
Throws Error
kind "value" when a channel is outside 0-255

strike

jennifer
ansi.strike(s as string)

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

s
the text to style
Returns string
the wrapped text, or s unchanged when colour is off

strip

jennifer
ansi.strip(s as string)

Remove every terminal escape sequence - the inverse of the wrappers, regardless of whether colour is currently enabled - and any other CSI (cursor, erase), OSC (title, clipboard, hyperlink), DCS / SOS / PM / APC string or two-byte ESC sequence, so the result prints as plain text.

s
the text to strip
Returns string
the text with all escape sequences removed

style

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

Wrap a string in the named text style.

s
the text to style
name
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

underline

jennifer
ansi.underline(s as string)

Wrap a string in the underline text style.

s
the text to style
Returns string
the wrapped text, or s unchanged when colour is off

white

jennifer
ansi.white(s as string)

Wrap a string in white foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off

yellow

jennifer
ansi.yellow(s as string)

Wrap a string in yellow foreground colour.

s
the text to colourize
Returns string
the wrapped text, or s unchanged when colour is off