(chibi term ansi)

A library to use ANSI escape codes to format text and background color, font weigh, and underlining.

Library

(black-escape)

(red-escape)

(green-escape)

(yellow-escape)

(blue-escape)

(magenta-escape)

(cyan-escape)

(white-escape)

Return a string consisting of an ANSI escape code to select the specified text color.

(rgb-escape red-level green-level blue-level)

Return a string consisting of an ANSI escape code to select the text color specified by the red-level, green-level, and blue-level arguments, each of which must be an exact integer in the range [0, 5]. The caller is resonsible for verifying that the terminal supports 256 colors.

(gray-escape gray-level)

Return a string consisting of an ANSI escape code to select the text color specified by the gray-level argument, which must be an exact integer in the range [0, 23]. The caller is resonsible for verifying that the terminal supports 256 colors.

(rgb24-escape red-level green-level blue-level)

The true-color equivalent of rgb-escape. Return a string consisting of an ANSI escape code to select the text color specified by the red-level, green-level, and blue-level arguments, each of which must be an exact integer in the range [0, 255].

(reset-color-escape)

Return a string consisting of an ANSI escape code to select the default text color.

(black str)

(red str)

(green str)

(yellow str)

(blue str)

(magenta str)

(cyan str)

(white str)

If ANSI escapes are enabled, return a string consisting of the string str with a prefix that selects specified text color and a suffix that selects the default text color. If ANSI escapes are not enabled, return str.

(rgb red-level green-level blue-level)

Returns a procedure which takes a single argument, a string, and which when called behaves as follows. If ANSI escapes are enabled, the procedure returns a string consisting of its argument with a prefix that selects specified text color (obtained by calling the rgb-escape procedure with the values of the red-level, green-level, and blue-level arguments) and a suffix that selects the default text color. If ANSI escapes are not enabled, the procedure returns its argument. The caller is resonsible for verifying that the terminal supports 256 colors.

(gray gray-level)

Returns a procedure which takes a single argument, a string, and which when called behaves as follows. If ANSI escapes are enabled, the procedure returns a string consisting of its argument with a prefix that selects specified text color (obtained by calling the gray-escape procedure with the values of the gray-level argument) and a suffix that selects the default text color. If ANSI escapes are not enabled, the procedure returns its argument. The caller is resonsible for verifying that the terminal supports 256 colors.

(rgb24 red-level green-level blue-level)

The true-color equivalent of rbg, extending the ranges to [0, 255].

(black-background-escape)

(red-background-escape)

(green-background-escape)

(yellow-background-escape)

(blue-background-escape)

(magenta-background-escape)

(cyan-background-escape)

(white-background-escape)

Return a string consisting of an ANSI escape code to select the specified background color.

(rgb-background-escape red-level green-level blue-level)

Return a string consisting of an ANSI escape code to select the background color specified by the red-level, green-level, and blue-level arguments, each of which must be an exact integer in the range [0, 5]. The caller is resonsible for verifying that the terminal supports 256 colors.

(gray-background-escape gray-level)

Return a string consisting of an ANSI escape code to select the background color specified by the gray-level argument, which must be an exact integer in the range [0, 23]. The caller is resonsible for verifying that the terminal supports 256 colors.

(rgb24-background-escape red-level green-level blue-level)

The true-color equivalent of rgb-background-escape. Return a string consisting of an ANSI escape code to select the text color specified by the red-level, green-level, and blue-level arguments, each of which must be an exact integer in the range [0, 255].

(reset-background-color-escape)

Return a string consisting of an ANSI escape code to select the default background color.

(black-background str)

(red-background str)

(green-background str)

(yellow-background str)

(blue-background str)

(magenta-background str)

(cyan-background str)

(white-background str)

If ANSI escapes are enabled, return a string consisting of the string str with a prefix that selects specified background color and a suffix that selects the default background color. If ANSI escapes are not enabled, return str.

(rgb-background red-level green-level blue-level)

Returns a procedure which takes a single argument, a string, and which when called behaves as follows. If ANSI escapes are enabled, the procedure returns a string consisting of its argument with a prefix that selects specified background color (obtained by calling the rgb-background-escape procedure with the values of the red-level, green-level, and blue-level arguments) and a suffix that selects the default background color. If ANSI escapes are not enabled, the procedure returns its argument. The caller is resonsible for verifying that the terminal supports 256 colors.

(gray-background gray-level)

Returns a procedure which takes a single argument, a string, and which when called behaves as follows. If ANSI escapes are enabled, the procedure returns a string consisting of its argument with a prefix that selects specified background color (obtained by calling the gray-background-escape procedure with the values of the gray-level argument) and a suffix that selects the default background color. If ANSI escapes are not enabled, the procedure returns its argument. The caller is resonsible for verifying that the terminal supports 256 colors.

(rgb24-background red-level green-level blue-level)

The true-color equivalent of rbg-background, extending the ranges to [0, 255].

(bold-escape)

Return a string consisting of an ANSI escape code to select bold style.

(reset-bold-escape)

Return a string consisting of an ANSI escape code to select non-bold style.

(bold str)

If ANSI escapes are enabled, return a string consisting of the string str with a prefix that selects bold style and a suffix that selects non-bold style. If ANSI escapes are not enabled, return str.

(underline-escape)

Return a string consisting of an ANSI escape code to select underlined style.

(reset-underline-escape)

Return a string consisting of an ANSI escape code to select non-underlined style.

(underline str)

If ANSI escapes are enabled, return a string consisting of the string str with a prefix that selects underlined style and a suffix that selects non-underlined style. If ANSI escapes are not enabled, return str.

(italic-escape)

Return a string consisting of an ANSI escape code to select italic style.

(reset-italic-escape)

Return a string consisting of an ANSI escape code to select non-italic style.

(italic str)

Returns str optionally wrapped in italic escapes.

(strikethrough-escape)

Return a string consisting of an ANSI escape code to select strikethrough style.

(reset-strikethrough-escape)

Return a string consisting of an ANSI escape code to select non-strikethrough style.

(strikethrough str)

Returns str optionally wrapped in strikethrough escapes.

(negative-escape)

Return a string consisting of an ANSI escape code to select negative style (text in the background color and background in the text color).

(reset-negative-escape)

Return a string consisting of an ANSI escape code to select positive style (text in the text color and background in the background color).

(negative str)

If ANSI escapes are enabled, return a string consisting of the string str with a prefix that selects negative style (text in the background color and background in the text color) and a suffix that selects positive style (text in the text color and background in the background color). If ANSI escapes are not enabled, return str.

ansi-escapes-enabled?

A parameter object that determines whether ANSI escapes are enabled in some of the preceding procedures. They are disabled if (ansi-escapes-enabled?) returns #f, and otherwise they are enabled. The initial value returned by (ansi-escapes-enabled?) is determined by the environment. If the environment variable ANSI_ESCAPES_ENABLED is set, its value determines the initial value returned by (ansi-escapes-enabled?). If the value of ANSI_ESCAPES_ENABLED is "0", the initial value is #f, otherwise the initial value is #t. If the environment variable ANSI_ESCAPES_ENABLED is not set, but the environment variable TERM is set, the value of the latter determines the initial value returned by (ansi-escapes-enabled?). If the value of TERM is "xterm", "xterm-color", "xterm-256color", "rxvt", "rxvt-unicode-256color", "kterm", "linux", "screen", "screen-256color", or "vt100", the initial value is #t, otherwise the initial value is #f. If neither of the environment variables ANSI_ESCAPES_ENABLED and TERM are set, the initial value returned by (ansi-escapes-enabled?) is #f.

Notes

It is important to remember that the formatting procedures apply a prefix to set a particular graphics parameter and a suffix to reset the parameter to its default value. This can lead to surprises. For example, on an ANSI terminal, one might mistakenly expect the following to display GREEN in green text and then RED in red text:
(display (red (string-append (green "GREEN") "RED")))
However, it will actually display GREEN in green text and then RED in the default text color. This is a limitation of ANSI control codes; graphics attributes are not saved to and restored from a stack, but instead are simply set. One way to display GREEN in green text and then RED in red text is:
(display (string-append (green "GREEN") (red "RED")))
On the other hand, text color, background color, font weight (bold or default), underline (on or off), image (positive or negative) are orthogonal. So, for example, on an ANSI terminal the following should display GREEN in green text and then RED in red text, with both in bold and GREEN underlined.
(display (bold (string-append (underline (green "GREEN")) (red "RED"))))