Help with generating textual output that includes ANSI escape codes for formatting.
The compose
function is the best starting point.
Reference: Wikipedia.
Help with generating textual output that includes ANSI escape codes for formatting. The [[compose]] function is the best starting point. Reference: [Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code#SGR).
Determines if ANSI colors are enabled; color is a deliberate misnomer, as we lump other font characteristics (bold, underline, italic, etc.) along with colors.
This will be false if the environment variable NO_COLOR is non-blank.
Otherwise, the JVM system property clj-commons.ansi.enabled
(if present) determines
the value; "true" enables colors, any other value disables colors.
If the property is null, then the default is a best guess based on the environment:
if either the nrepl.core
namespace is present, or the JVM has a console (via (System/console)
),
then color will be enabled.
The nrepl.core check has been verified to work with Cursive, with lein repl
, and with clojure
(or clj
).
Determines if ANSI colors are enabled; color is a deliberate misnomer, as we lump other font characteristics (bold, underline, italic, etc.) along with colors. This will be false if the environment variable NO_COLOR is non-blank. Otherwise, the JVM system property `clj-commons.ansi.enabled` (if present) determines the value; "true" enables colors, any other value disables colors. If the property is null, then the default is a best guess based on the environment: if either the `nrepl.core` namespace is present, or the JVM has a console (via `(System/console)`), then color will be enabled. The nrepl.core check has been verified to work with Cursive, with `lein repl`, and with `clojure` (or `clj`).
(compose & inputs)
Given a Hiccup-inspired data structure, composes and returns a string that includes ANSI formatting codes for font color and other characteristics.
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 spans, a sequence of values with a specific visual representation. The first element in a span vector declares the visual properties of the span: the color (including other characteristics such as bold or underline), and the width and padding (described later).
The declaration is usually a keyword, to define just the font. The font def contains one or more terms, separated by periods.
The terms:
red
or bright-red
green-bg
or bright-green-bg
bold
, faint
, or plain
italic
or roman
inverse
or normal
underlined
or not-underlined
e.g.
(compose [:yellow "Warning: the " [:bold.bright-white.bright-red-bg "reactor"]
" is about to "
[:italic.bold.red "meltdown!"]])
=> ...
The order of the terms does not matter. Behavior for conflicting terms (:blue.green.black
)
is not defined.
Font defs apply on top of the font def of the enclosing span, and the outer span's font def
is restored at the end of the inner span, e.g. [:red " RED " [:bold "RED/BOLD"] " RED "]
.
A font def may also be nil, to indicate no change in font.
compose
presumes that on entry the current font is plain (default foreground and background, not bold,
or inverse, or italic, or underlined) and appends a reset sequence to the end of the returned string to
ensure that later output is also plain.
The core colors are black
, red
, green
, yellow
, blue
, magenta
, cyan
, and white
.
When *color-enabled*
is false, then any font defs are validated, then ignored (no ANSI codes
will be included).
The span's font declaration may also be a map with the following keys:
The map form of the font declaration is typically only used when a span width is specified.
The span will be padded with spaces to ensure that it is the specified width. compose
tracks the number
of characters inside the span, excluding any ANSI code sequences injected by compose
.
compose
doesn't consider the characters; if the strings contain tabs, newlines, or ANSI code sequences
not generated by compose
, the calculation of the span width will be incorrect.
Only one span at a time can be tracked for width; if a nested span also specifies a width, compose
will
throw an exception.
Example:
[{:font :red
:width 20} message]
This will output the value of message
in red text, padded with spaces on the left to be 20 characters.
At this time, the placement of the spaces may be a bit haphazard with respect to ANSI codes; the spaces may be visible if the font def sets inverse, underlined, or colored backgrounds.
Given a Hiccup-inspired data structure, composes and returns a string that includes ANSI formatting codes for font color and other characteristics. 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 _spans_, a sequence of values with a specific visual representation. The first element in a span vector declares the visual properties of the span: the color (including other characteristics such as bold or underline), and the width and padding (described later). The declaration is usually a keyword, to define just the font. 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!"]]) => ... ``` The order of the terms does not matter. Behavior for conflicting terms (`:blue.green.black`) is not defined. Font defs apply on top of the font def of the enclosing span, and the outer span's font def is restored at the end of the inner span, e.g. `[:red " RED " [:bold "RED/BOLD"] " RED "]`. A font def may also be nil, to indicate no change in font. `compose` presumes that on entry the current font is plain (default foreground and background, not bold, or inverse, or italic, or underlined) and appends a reset sequence to the end of the returned string to ensure that later output is also plain. The core colors are `black`, `red`, `green`, `yellow`, `blue`, `magenta`, `cyan`, and `white`. When [[*color-enabled*]] is false, then any font defs are validated, then ignored (no ANSI codes will be included). The span's font declaration may also be a map with the following keys: - :font - keyword; the font declaration - :width - number, the visual width of the span - :pad - where to pad the span (:left or :right); defaults to :left The map form of the font declaration is typically only used when a span width is specified. The span will be padded with spaces to ensure that it is the specified width. `compose` tracks the number of characters inside the span, excluding any ANSI code sequences injected by `compose`. `compose` doesn't consider the characters; if the strings contain tabs, newlines, or ANSI code sequences not generated by `compose`, the calculation of the span width will be incorrect. Only one span at a time can be tracked for width; if a nested span also specifies a width, `compose` will throw an exception. Example: [{:font :red :width 20} message] This will output the value of `message` in red text, padded with spaces on the left to be 20 characters. At this time, the placement of the spaces may be a bit haphazard with respect to ANSI codes; the spaces may be visible if the font def sets inverse, underlined, or colored backgrounds.
(when-color-enabled & body)
Evaluates its body only when *color-enabled*
is true.
Evaluates its body only when [[*color-enabled*]] is true.
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close