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
| Name | What it is |
|---|---|
bgColor | Wrap a string in the named background colour. |
bgColor256 | Wrap a string in a 256-colour palette background (xterm 48;5;n). |
black | Wrap a string in black foreground colour. |
blue | Wrap a string in blue foreground colour. |
bold | Wrap a string in the bold text style. |
color | Wrap a string in the named foreground colour. |
color256 | Wrap a string in a 256-colour palette foreground (xterm 38;5;n), the indexed middle rung between the name... |
cyan | Wrap a string in cyan foreground colour. |
dim | Wrap a string in the dim text style. |
gray | Wrap a string in gray foreground colour. |
green | Wrap a string in green foreground colour. |
italic | Wrap a string in the italic text style. |
magenta | Wrap a string in magenta foreground colour. |
red | Wrap a string in red foreground colour. |
reverse | Wrap a string in the reverse (inverted) text style. |
rgb | Wrap a string in a 24-bit truecolor foreground. |
rgbToColor256 | Map a 24-bit RGB triple to the nearest 256-colour index, for terminals without truecolor. |
strike | Wrap a string in the strikethrough (crossed-out) text style. |
strip | Remove every terminal escape sequence - the inverse of the wrappers, regardless of whether colour is curren... |
style | Wrap a string in the named text style. |
underline | Wrap a string in the underline text style. |
white | Wrap a string in white foreground colour. |
yellow | Wrap a string in yellow foreground colour. |
Functions
bgColor
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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