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-bg
(black-bg text)
Wraps the provided text with ANSI codes to render with a black background.
blue-bg
(blue-bg text)
Wraps the provided text with ANSI codes to render with a blue background.
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-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-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-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-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-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-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-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-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-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-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-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-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-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.
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
orbright-red
- background color: e.g.,
green-bg
orbright-green-bg
- boldness:
bold
,faint
, orplain
- italics:
italic
orroman
- inverse:
inverse
ornormal
- underline:
underlined
ornot-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.
cyan-bg
(cyan-bg text)
Wraps the provided text with ANSI codes to render with a cyan background.
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).
green-bg
(green-bg text)
Wraps the provided text with ANSI codes to render with a green background.
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).
magenta-bg
(magenta-bg text)
Wraps the provided text with ANSI codes to render with a magenta background.
normal
added in 1.4
(normal text)
Wraps the provided text with ANSI codes 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.
plain
added in 1.4
(plain text)
Wraps the provided text with ANSI codes to render as plain (not bold or faint).
red-bg
(red-bg text)
Wraps the provided text with ANSI codes to render with a red background.
roman
added in 1.4
(roman text)
Wraps the provided text with ANSI codes to render as romain (not italic).
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.
visual-length
(visual-length string)
Returns the length of the string, with ANSI codes stripped out.
white-bg
(white-bg text)
Wraps the provided text with ANSI codes to render with a white background.
yellow-bg
(yellow-bg text)
Wraps the provided text with ANSI codes to render with a yellow background.