io.aviso.ansi

Help with generating textual output that includes ANSI escape codes for formatting. The compose function is the best starting point.

Reference: Wikipedia.

In version 1.4, the incorrectly named bold-<color> functions and constants were deprecated in favor of the bright-<color> equivalents (correcting a day 1 naming mistake).

ansi-output-enabled?

added in 1.3

Determine if ANSI output is enabled.

The first checks are for the JVM system property io.aviso.ansi.enable and then system property ENABLE_ANSI_COLORS. If either of these is non-nil, it sets the value: the value false (matched caselessly) disables ANSI colors and fonts, otherwise they are enabled.

Next, there is an attempt to determine if execution is currently inside a REPL environment, possibly started from an IDE; a check is made to see if nrepl.core namespace is available; if so, then ANSI colors are enabled.

This has been verified to work with Cursive, with lein repl, and with clojure (or clj).

This check is necessary, because often in such cases, there is no console (the next check).

Otherwise, if the system has a console (via (System/console)) ANSI output will be enabled; when Clojure is running in a pipe, or as a background job without a terminal attached, the console will be nil and ANSI output will be disabled.

When this value is false, all the generated color and font constants return the empty string, and the color and font functions return the input string unchanged. This is decided during macro expansion when the ansi namespace is first loaded, so it can’t be changed at runtime.

black

(black text)

Wraps the provided text with ANSI codes to render text in black.

black-bg

(black-bg text)

Wraps the provided text with ANSI codes to render with a black background.

black-bg-font

Constant for ANSI code to render with a black background.

black-font

Constant for ANSI code to render text in black.

blue

(blue text)

Wraps the provided text with ANSI codes to render text in blue.

blue-bg

(blue-bg text)

Wraps the provided text with ANSI codes to render with a blue background.

blue-bg-font

Constant for ANSI code to render with a blue background.

blue-font

Constant for ANSI code to render text in blue.

bold

(bold text)

Wraps the provided text with ANSI codes to render in bold.

bold-black

deprecated in 1.4

(bold-black text)

Wraps the provided text with ANSI codes to render text in bright black.

bold-black-bg

deprecated in 1.4

(bold-black-bg text)

Wraps the provided text with ANSI codes to render with a bright black background.

bold-black-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright black background.

bold-black-font

deprecated in 1.4

Constant for ANSI code to render text in bright black.

bold-blue

deprecated in 1.4

(bold-blue text)

Wraps the provided text with ANSI codes to render text in bright blue.

bold-blue-bg

deprecated in 1.4

(bold-blue-bg text)

Wraps the provided text with ANSI codes to render with a bright blue background.

bold-blue-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright blue background.

bold-blue-font

deprecated in 1.4

Constant for ANSI code to render text in bright blue.

bold-cyan

deprecated in 1.4

(bold-cyan text)

Wraps the provided text with ANSI codes to render text in bright cyan.

bold-cyan-bg

deprecated in 1.4

(bold-cyan-bg text)

Wraps the provided text with ANSI codes to render with a bright cyan background.

bold-cyan-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright cyan background.

bold-cyan-font

deprecated in 1.4

Constant for ANSI code to render text in bright cyan.

bold-font

Constant for ANSI code to render in bold.

bold-green

deprecated in 1.4

(bold-green text)

Wraps the provided text with ANSI codes to render text in bright green.

bold-green-bg

deprecated in 1.4

(bold-green-bg text)

Wraps the provided text with ANSI codes to render with a bright green background.

bold-green-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright green background.

bold-green-font

deprecated in 1.4

Constant for ANSI code to render text in bright green.

bold-magenta

deprecated in 1.4

(bold-magenta text)

Wraps the provided text with ANSI codes to render text in bright magenta.

bold-magenta-bg

deprecated in 1.4

(bold-magenta-bg text)

Wraps the provided text with ANSI codes to render with a bright magenta background.

bold-magenta-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright magenta background.

bold-magenta-font

deprecated in 1.4

Constant for ANSI code to render text in bright magenta.

bold-red

deprecated in 1.4

(bold-red text)

Wraps the provided text with ANSI codes to render text in bright red.

bold-red-bg

deprecated in 1.4

(bold-red-bg text)

Wraps the provided text with ANSI codes to render with a bright red background.

bold-red-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright red background.

bold-red-font

deprecated in 1.4

Constant for ANSI code to render text in bright red.

bold-white

deprecated in 1.4

(bold-white text)

Wraps the provided text with ANSI codes to render text in bright white.

bold-white-bg

deprecated in 1.4

(bold-white-bg text)

Wraps the provided text with ANSI codes to render with a bright white background.

bold-white-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright white background.

bold-white-font

deprecated in 1.4

Constant for ANSI code to render text in bright white.

bold-yellow

deprecated in 1.4

(bold-yellow text)

Wraps the provided text with ANSI codes to render text in bright yellow.

bold-yellow-bg

deprecated in 1.4

(bold-yellow-bg text)

Wraps the provided text with ANSI codes to render with a bright yellow background.

bold-yellow-bg-font

deprecated in 1.4

Constant for ANSI code to render with a bright yellow background.

bold-yellow-font

deprecated in 1.4

Constant for ANSI code to render text in bright yellow.

bright-black

(bright-black text)

Wraps the provided text with ANSI codes to render text in bright black.

bright-black-bg

(bright-black-bg text)

Wraps the provided text with ANSI codes to render with a bright black background.

bright-black-bg-font

Constant for ANSI code to render with a bright black background.

bright-black-font

Constant for ANSI code to render text in bright black.

bright-blue

(bright-blue text)

Wraps the provided text with ANSI codes to render text in bright blue.

bright-blue-bg

(bright-blue-bg text)

Wraps the provided text with ANSI codes to render with a bright blue background.

bright-blue-bg-font

Constant for ANSI code to render with a bright blue background.

bright-blue-font

Constant for ANSI code to render text in bright blue.

bright-cyan

(bright-cyan text)

Wraps the provided text with ANSI codes to render text in bright cyan.

bright-cyan-bg

(bright-cyan-bg text)

Wraps the provided text with ANSI codes to render with a bright cyan background.

bright-cyan-bg-font

Constant for ANSI code to render with a bright cyan background.

bright-cyan-font

Constant for ANSI code to render text in bright cyan.

bright-green

(bright-green text)

Wraps the provided text with ANSI codes to render text in bright green.

bright-green-bg

(bright-green-bg text)

Wraps the provided text with ANSI codes to render with a bright green background.

bright-green-bg-font

Constant for ANSI code to render with a bright green background.

bright-green-font

Constant for ANSI code to render text in bright green.

bright-magenta

(bright-magenta text)

Wraps the provided text with ANSI codes to render text in bright magenta.

bright-magenta-bg

(bright-magenta-bg text)

Wraps the provided text with ANSI codes to render with a bright magenta background.

bright-magenta-bg-font

Constant for ANSI code to render with a bright magenta background.

bright-magenta-font

Constant for ANSI code to render text in bright magenta.

bright-red

(bright-red text)

Wraps the provided text with ANSI codes to render text in bright red.

bright-red-bg

(bright-red-bg text)

Wraps the provided text with ANSI codes to render with a bright red background.

bright-red-bg-font

Constant for ANSI code to render with a bright red background.

bright-red-font

Constant for ANSI code to render text in bright red.

bright-white

(bright-white text)

Wraps the provided text with ANSI codes to render text in bright white.

bright-white-bg

(bright-white-bg text)

Wraps the provided text with ANSI codes to render with a bright white background.

bright-white-bg-font

Constant for ANSI code to render with a bright white background.

bright-white-font

Constant for ANSI code to render text in bright white.

bright-yellow

(bright-yellow text)

Wraps the provided text with ANSI codes to render text in bright yellow.

bright-yellow-bg

(bright-yellow-bg text)

Wraps the provided text with ANSI codes to render with a bright yellow background.

bright-yellow-bg-font

Constant for ANSI code to render with a bright yellow background.

bright-yellow-font

Constant for ANSI code to render text in bright yellow.

compose

added in 1.4.0

(compose & input)

Given a Hiccup-inspired data structure, composes and returns a string that includes necessary ANSI codes.

The data structure may consist of literal values (strings, numbers, etc.) that are formatted with str and concatenated.

Nested sequences are composed recursively; this (for example) allows the output from map or for to be mixed into the composed string seamlessly.

Nested vectors represent blocks; the first element in the vector is a keyword that defines the font used within the block. The font def contains one or more terms, separated by periods.

The terms:

  • foreground color: e.g. red or bright-red
  • background color: e.g., green-bg or bright-green-bg
  • boldness: bold, faint, or plain
  • italics: italic or roman
  • inverse: inverse or normal
  • underline: underlined or not-underlined

e.g.

(compose [:yellow "Warning: the " [:bold.bright-white.bright-red-bg "reactor"]
  " is about to "
  [:italic.bold-red "meltdown!"]])
=> ...

Font defs apply on top of the font def of the enclosing block, and the outer block’s font def is restored at the end of the inner block, e.g. [:red " RED " [:bold "RED/BOLD"] " RED "].

compose presumes that on entry the current font is plain (default foreground and background, not bold, or inverse, or italic) and appends a reset-font to the end of the returned string to ensure that later output is also plain.

csi

The control sequence initiator: ESC [

cyan

(cyan text)

Wraps the provided text with ANSI codes to render text in cyan.

cyan-bg

(cyan-bg text)

Wraps the provided text with ANSI codes to render with a cyan background.

cyan-bg-font

Constant for ANSI code to render with a cyan background.

cyan-font

Constant for ANSI code to render text in cyan.

default-background

added in 1.4

(default-background text)

Wraps the provided text with ANSI codes to render with the default background color.

default-background-font

added in 1.4

Constant for ANSI code to render with the default background color.

default-foreground

added in 1.4

(default-foreground text)

Wraps the provided text with ANSI codes to render with the default foreground color.

default-foreground-font

added in 1.4

Constant for ANSI code to render with the default foreground color.

faint

added in 1.4.1

(faint text)

Wraps the provided text with ANSI codes to render as faint (not bold or plain).

faint-font

added in 1.4.1

Constant for ANSI code to render as faint (not bold or plain).

green

(green text)

Wraps the provided text with ANSI codes to render text in green.

green-bg

(green-bg text)

Wraps the provided text with ANSI codes to render with a green background.

green-bg-font

Constant for ANSI code to render with a green background.

green-font

Constant for ANSI code to render text in green.

inverse

(inverse text)

Wraps the provided text with ANSI codes to render as inverse (foreground and background colors reversed).

inverse-font

Constant for ANSI code to render as inverse (foreground and background colors reversed).

italic

(italic text)

Wraps the provided text with ANSI codes to render italicized.

italic-font

Constant for ANSI code to render italicized.

magenta

(magenta text)

Wraps the provided text with ANSI codes to render text in magenta.

magenta-bg

(magenta-bg text)

Wraps the provided text with ANSI codes to render with a magenta background.

magenta-bg-font

Constant for ANSI code to render with a magenta background.

magenta-font

Constant for ANSI code to render text in magenta.

normal

added in 1.4

(normal text)

Wraps the provided text with ANSI codes to render as normal (not inverse).

normal-font

added in 1.4

Constant for ANSI code to render as normal (not inverse).

not-underlined

added in 1.4.1

(not-underlined text)

Wraps the provided text with ANSI codes to render not underlined.

not-underlined-font

added in 1.4.1

Constant for ANSI code to render not underlined.

plain

added in 1.4

(plain text)

Wraps the provided text with ANSI codes to render as plain (not bold or faint).

plain-font

added in 1.4

Constant for ANSI code to render as plain (not bold or faint).

red

(red text)

Wraps the provided text with ANSI codes to render text in red.

red-bg

(red-bg text)

Wraps the provided text with ANSI codes to render with a red background.

red-bg-font

Constant for ANSI code to render with a red background.

red-font

Constant for ANSI code to render text in red.

reset-font

Resets the font, clearing all colors and other attributes.

roman

added in 1.4

(roman text)

Wraps the provided text with ANSI codes to render as romain (not italic).

roman-font

added in 1.4

Constant for ANSI code to render as romain (not italic).

sgr

The Select Graphic Rendition suffix: m

strip-ansi

(strip-ansi string)

Removes ANSI codes from a string, returning just the raw text.

underlined

added in 1.4.1

(underlined text)

Wraps the provided text with ANSI codes to render underlined.

underlined-font

added in 1.4.1

Constant for ANSI code to render underlined.

visual-length

(visual-length string)

Returns the length of the string, with ANSI codes stripped out.

white

(white text)

Wraps the provided text with ANSI codes to render text in white.

white-bg

(white-bg text)

Wraps the provided text with ANSI codes to render with a white background.

white-bg-font

Constant for ANSI code to render with a white background.

white-font

Constant for ANSI code to render text in white.

yellow

(yellow text)

Wraps the provided text with ANSI codes to render text in yellow.

yellow-bg

(yellow-bg text)

Wraps the provided text with ANSI codes to render with a yellow background.

yellow-bg-font

Constant for ANSI code to render with a yellow background.

yellow-font

Constant for ANSI code to render text in yellow.