Skip to content

Using Ansi

Style terminal output with colors, backgrounds, and text effects. Ansi annotations describe the style; the ANSI renderer converts them into escape sequences without changing the box's dimensions.

Every example below is complete. After installing the dependencies, save one as example.ts and run bun run example.ts with Bun or npx tsx example.ts with Node. The previews show the string produced by the displayed code.

Apply text colors

Attach an Ansi style with Box.annotate, then render the layout with Box.renderPrettySync. The preview shows every line created by the example.

Red text
Green text
Blue text
Yellow text
Magenta text
Cyan text
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const colors = [
  Box.annotate(Box.text("Red text"), Ansi.red),
  Box.annotate(Box.text("Green text"), Ansi.green),
  Box.annotate(Box.text("Blue text"), Ansi.blue),
  Box.annotate(Box.text("Yellow text"), Ansi.yellow),
  Box.annotate(Box.text("Magenta text"), Ansi.magenta),
  Box.annotate(Box.text("Cyan text"), Ansi.cyan),
];
const output = Box.renderPrettySync(Box.vcat(colors, Box.left));
console.log(output);

Standard foreground colors are black, red, green, yellow, blue, magenta, cyan, and white. Each has a bright variant, such as brightRed or brightWhite. fgDefault selects the terminal's default foreground. The terminal theme determines the exact standard colors.

Apply background colors

Combine a foreground and a background to keep the label readable.

 Warning 
 Error 
 Info 
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const labels = [
  Box.annotate(Box.text(" Warning "), Ansi.combine(Ansi.black, Ansi.bgYellow)),
  Box.annotate(Box.text(" Error "), Ansi.combine(Ansi.white, Ansi.bgRed)),
  Box.annotate(Box.text(" Info "), Ansi.combine(Ansi.white, Ansi.bgBlue)),
];
const output = Box.renderPrettySync(Box.vcat(labels, Box.left));
console.log(output);

Background names use the bg prefix, such as bgRed and bgWhite. Bright backgrounds use bgBrightRed and bgBrightWhite. bgDefault selects the terminal's default background.

Apply text effects

Use text effects alongside words that communicate the meaning. Terminal support for individual effects varies.

Bold text
Dim text
Italic text
Underlined text
Strike
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const effects = [
  Box.annotate(Box.text("Bold text"), Ansi.bold),
  Box.annotate(Box.text("Dim text"), Ansi.dim),
  Box.annotate(Box.text("Italic text"), Ansi.italic),
  Box.annotate(Box.text("Underlined text"), Ansi.underlined),
  Box.annotate(Box.text("Strike"), Ansi.strikethrough),
];
const output = Box.renderPrettySync(Box.vcat(effects, Box.left));
console.log(output);

The module also provides blink, inverse, hidden, and overline. These effects depend on terminal support; hidden text is still present in the output string.

Combine styles

Ansi.combine keeps one foreground color and one background color. When colors conflict, the last color wins. Different text effects can coexist.

 CRITICAL 
 SUCCESS 
 PENDING 
Last foreground wins: green
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const labels = [
  Box.annotate(
    Box.text(" CRITICAL "),
    Ansi.combine(Ansi.bold, Ansi.white, Ansi.bgRed)
  ),
  Box.annotate(
    Box.text(" SUCCESS "),
    Ansi.combine(Ansi.bold, Ansi.white, Ansi.bgGreen)
  ),
  Box.annotate(
    Box.text(" PENDING "),
    Ansi.combine(Ansi.bold, Ansi.underlined, Ansi.yellow)
  ),
  Box.annotate(
    Box.text("Last foreground wins: green"),
    Ansi.combine(Ansi.red, Ansi.green)
  ),
];
const output = Box.renderPrettySync(Box.vcat(labels, Box.left));
console.log(output);

Calling Box.annotate again on the same box replaces its root annotation. Combine styles before attaching them to preserve both.

Use the 256-color palette

Pass a palette index from 0 through 255 to Ansi.color256 or Ansi.bgColor256. Use integer indices; out-of-range values are clamped to that range.

Purple foreground: 129
 Teal background: 30 
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const colors = [
  Box.annotate(Box.text("Purple foreground: 129"), Ansi.color256(129)),
  Box.annotate(
    Box.text(" Teal background: 30 "),
    Ansi.combine(Ansi.white, Ansi.bgColor256(30))
  ),
];
const output = Box.renderPrettySync(Box.vcat(colors, Box.left));
console.log(output);

Use RGB colors

Use Ansi.colorRGB and Ansi.bgColorRGB for 24-bit color. Pass integer red, green, and blue channels from 0 through 255. Each channel is clamped to that range.

Orange text
Custom teal
 Purple background 
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const colors = [
  Box.annotate(Box.text("Orange text"), Ansi.colorRGB(255, 165, 0)),
  Box.annotate(Box.text("Custom teal"), Ansi.colorRGB(0, 200, 180)),
  Box.annotate(
    Box.text(" Purple background "),
    Ansi.combine(Ansi.white, Ansi.bgColorRGB(80, 40, 120))
  ),
];
const output = Box.renderPrettySync(Box.vcat(colors, Box.left));
console.log(output);

Choose extended colors only when the target terminal supports them. The web previews display the requested RGB colors.

Use hex colors

Ansi.colorHex and Ansi.bgColorHex accept three or six hexadecimal digits, with an optional #. An invalid string throws an error.

Hex foreground
 Hex background 
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const colors = [
  Box.annotate(Box.text("Hex foreground"), Ansi.colorHex("#ff6600")),
  Box.annotate(
    Box.text(" Hex background "),
    Ansi.combine(Ansi.white, Ansi.bgColorHex("#333"))
  ),
];
const output = Box.renderPrettySync(Box.vcat(colors, Box.left));
console.log(output);

Nest styles and restore the parent

Style a composed parent to give its children a shared style. A child color overrides the parent color for that child. After the child, the renderer restores the parent style.

Green parent | red child | green again
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const child = Box.annotate(Box.text("red child"), Ansi.red);
const parent = Box.annotate(
  Box.hcat(
    [Box.text("Green parent | "), child, Box.text(" | green again")],
    Box.top
  ),
  Ansi.combine(Ansi.bold, Ansi.green)
);
const output = Box.renderPrettySync(parent);
console.log(output);

The child inherits bold from the parent. See Using Annotation for the difference between nested annotations and replacing an annotation.

Render styled and plain output

Box.renderPrettySync interprets ANSI styles. Box.renderPlainSync ignores styles and preserves layout whitespace. Neither function appends a newline. This example displays the styled text, its plain output, and its escaped ANSI representation.

Hello
Plain: Hello
ANSI: "\u001b[31mHello\u001b[0m"
import * as Box from "effect-boxes/Box";
import * as Ansi from "effect-boxes/Ansi";
 
const styled = Box.annotate(Box.text("Hello"), Ansi.red);
const ansi = Box.renderPrettySync(styled);
const plain = Box.renderPlainSync(styled);
const output = Box.renderPrettySync(
  Box.vcat(
    [
      styled,
      Box.text(`Plain: ${plain}`),
      Box.text(`ANSI: ${JSON.stringify(ansi)}`),
    ],
    Box.left
  )
);
console.log(output);

For Effect programs, provide Renderer.AnsiRendererLive when calling Renderer.render. See Rendering for renderer selection and whitespace options. Use Cmd for cursor movement and terminal control commands.