Skip to content
Jennifer Programming Language

ansi - terminal styling

Import with import "ansi.j" as ansi;. Wraps a string in ANSI SGR escape codes for colour, background colour, text style, the 256-colour palette, and 24-bit truecolor, and strips them back off. Pure Jennifer, so it runs on either binary.

Styling is TTY-aware: it suppresses itself when stdout is not a terminal (redirected to a file or a pipe). The NO_COLOR / FORCE_COLOR environment variables override the gate.

jennifer
use io;
import "ansi.j" as ansi;

io.printf("%s\n", ansi.bold(ansi.red("error:")) + " something broke");
io.printf("%s\n", ansi.green("ok") + " / " + ansi.yellow("warn"));
io.printf("%s\n", ansi.rgb("truecolor orange", 255, 128, 0));
io.printf("%s\n", ansi.underline(ansi.cyan("nested + underlined")));

Runnable: examples/modules/ansi_demo.j.

Surface

CallReturnsNotes
ansi.color(s, name)stringWrap s in the named foreground colour. Unknown name throws.
ansi.bgColor(s, name)stringWrap s in the named background colour.
ansi.style(s, name)stringWrap s in a text style: bold / dim / italic / underline / reverse / strike.
ansi.color256(s, n)string256-colour palette foreground (38;5;n); n must be 0-255.
ansi.bgColor256(s, n)string256-colour palette background (48;5;n); n must be 0-255.
ansi.rgbToColor256(r, g, b)intNearest palette index for an RGB triple, for a 256-colour terminal.
ansi.rgb(s, r, g, b)string24-bit truecolor foreground; each channel 0-255.
ansi.strip(s)stringRemove every escape sequence (SGR and other CSI, OSC, DCS / SOS / PM / APC, two-byte ESC, 7- and 8-bit forms) - the inverse of the wrappers.

Wrappers nest: each ends with the reset for its own attribute only (39 foreground, 49 background, 22 bold and dim, 23 italic, 24 underline, 27 reverse, 29 strike), so in ansi.bold("a " + ansi.red("b") + " c") the " c" stays bold. Two wrappers of the same attribute do not stack: a colour inside another colour, or bold inside dim, ends at the default, not the outer value.

Colour and style names

  • Foreground / background (color / bgColor): black, red, green, yellow, blue, magenta, cyan, white. color also accepts the bright gray (alias grey).
  • Styles (style): bold, dim, italic, underline, reverse, strike.

An unrecognized name throws an Error of kind "value".

256-colour palette

color256 / bgColor256 are the indexed middle rung between the named colours and rgb. The index n follows the xterm layout: 0-15 the base colours, 16-231 a 6x6x6 colour cube, 232-255 a 24-step grayscale ramp. An index outside 0-255, or an rgb / rgbToColor256 channel outside 0-255, throws kind "value" (whether or not colour is on).

rgbToColor256(r, g, b) quantizes a truecolor triple to the nearest palette index (the closer of the cube and the grayscale ramp), for a terminal without 24-bit support:

jennifer
use io;
import "ansi.j" as ansi;

def n as int init ansi.rgbToColor256(255, 128, 0);   # orange -> cube index
io.printf("%s\n", ansi.color256("orange-ish", $n));

Shortcuts

Each common colour and style has a one-argument shortcut: ansi.NAME(s) is ansi.color(s, "NAME"), or ansi.style(s, "NAME") for a style.

KindShortcuts
Coloursblack, red, green, yellow, blue, magenta, cyan, white, gray
Stylesbold, dim, italic, underline, reverse, strike

When styling is emitted

ansi decides per call (it is stateless; there is no toggle to store):

  1. NO_COLOR set to a non-empty value: off.
  2. else FORCE_COLOR set to a non-empty value: off for 0 or false, on for anything else.
  3. else os.isTerminal("stdout"): on for a terminal, off otherwise (also when the stream cannot be inspected).

strip ignores this gate and removes escapes unconditionally, so it cleans up already-styled text (or a captured subprocess's output). Besides styling it removes cursor and erase sequences, OSC strings (window title, OSC 52 clipboard, OSC 8 hyperlinks; ended by BEL, ESC \ or the end of the text) and DCS / APC strings. Other control characters (BEL, CR) are kept.

jennifer
use io;
import "ansi.j" as ansi;

def styled as string init ansi.bold(ansi.blue("styled"));
io.printf("stripped: [%s]\n", ansi.strip($styled));   # stripped: [styled]

See also

  • os.md - os.isTerminal, the TTY gate, and os.getEnv for the NO_COLOR / FORCE_COLOR reads.
  • regex.md - strip uses regex.replace to drop escape sequences.
  • modules/index.md - the module catalog and import rules.

Academy talks

TalkWhat it shows
Your first command-line toolcoloured output that respects NO_COLOR

API reference

Every exported function, struct and constant of ansi, with its signature, generated from the module's doc comments: the ansi API reference.