Expand description
ANSI terminal text formatting assistant
Some virtual-terminals (ANSI ones being the focus here) have a feature of allowing command-line programs executed in them to request that certain limited formatting effects such as colour or boldness be applied to portions of the text the program outputs for them to display. This is achieved through the program embedding special “ANSI control” byte sequences into that text output. This can prove very useful for instance in highlighting error notices.
The purpose of this crate is to offer functionality to assist in generating and using such control sequences.
Note that such formatting should only be used when the stdout
stream (or stderr
as
applicable) is connected to an actual terminal that will process and use them (you wouldn’t want
these control sequences to exist in output that the user is having the terminal redirect into a
text file or another program as then it would just be treated as a part of the text and lead to
garbled output and confusion). Some utility functions are provided within the support mod to
assist you with determining appropriateness of using formatted output.
§Predefined sequences
There is no strict need to actually understand how to construct such control sequences to make use of the formatting they offer; many predefined sequences are available for your use in the predefined mod.
To make use of these simply inject them into the right points of a to-be-printed string via format arguments, as demonstrated below. Remember not to forget to make use of a reset after the text to be formatted. For instance:
use term_ctrl::predefined::{RESET, colours::fg::RED};
println!("{}Error:{} You made an error!", RED, RESET);
In the example just given, the text “Error:” will be highlighted in red. Of course as mentioned earlier, you should only use the colour sequences if output is actually connected to a terminal (and that the terminal has not redirected it, so do not assume output to stdout is okay). Here the example is modified to use a helper to check suitability and a filter to control use:
use term_ctrl::predefined::{RESET, colours::fg::RED};
let format = term_ctrl::support::fmt_supported_stdout();
let filter = |seq| { match format { true => seq, false => "" } };
println!("{}Error:{} You made an error!", filter(RED), filter(RESET));
Note, when resetting to normal, be sure to always use the proper reset sequence. Do not make the mistake of setting text colour to black and presuming that this achieves the same thing; it does not. (Consider that some people have black text on a white background in their terminal, whilst others use the opposite! Black text on a black background does not work very well!).
§Constructing sequences
Although understanding the sequences is not required, there are however benefits to be gained from it, since for instance not every possible combination can reasonably be provided in predefined form, and you may be able to achieve greater efficiency by constructing a custom one rather than using multiple predefines in sequence.
Let’s take a quick look at the sequence itself, then we can move on to discuss a macro which helps make custom construction very easy.
§The sequence pattern
The sequence pattern consists of four component parts:
1 | ESC ('\u{1B}') | The escape (␛) char, Unicode 0x1B |
2 | '[' | An opening bracket char |
3 | One or more numbers using a semi-colon (';') as a separator | |
4 | 'm' | A lower-case letter m |
In other words a pattern of: "\u{1B}[
<codes> m"
Every control sequence fits this template, and it is the set of numbers included in it which
determine the precise formatting nature of the request. As a quick example, the sequence of
"\u{1B}[31;1m"
specifies two numbers, 31
which corresponds to red text, and 1
which
corresponds to bold text (the set of available number codes are discussed shortly).
Typically each of the individual numbers given corresponds to a particular effect, however as we will see shortly there are a couple of exceptions where multiple numbers are used.
Before we continue, understand that the effects specified are applied in sequence and remain in
effect until changed or reset by a subsequent code in the sequence or by such a code in a later
sequence. Hence in the example given earlier, a sequence is used to enable red text, this is
followed by some text to which this formatting applies, then a new sequence is given that issues
the reset-all code. There are codes available for removing specific effects or resetting just
foreground or background-highlight colours. There is also the catch-all code 0
for resetting
everything to normal.
§Macro construction
As just mentioned, a macro (called seq
) is provided to assist with constructing sequences.
All you have to do is provide it with a list of decimal numbers and it will construct a string
of the above pattern that contains them. An example:
use term_ctrl::seq;
assert_eq!("\u{1B}[1;2;3m", seq!(1, 2, 3));
Note that you are not restricted to pure numeric literals, string literals work also:
use term_ctrl::seq;
assert_eq!("\u{1B}[1;2;3m", seq!("1", "2", "3"));
assert_eq!("\u{1B}[1;2;3m", seq!("1;2", 3));
Convenience macros are also provided for constructing 256-colour and RGB colour code sets for use in a sequence (they do not generate the full sequence, just a multi-code set of numbers to use in a sequence).
§Number code chart
The following is a guide to the available number codes and what they correspond to. In most cases a single number code corresponds to a single effect, however there are also those that require a sequence of multiple numbers (with the normal semi-colon separator).
§Effects
Code | Effect | Code | Effect |
---|---|---|---|
0 | Normal (reset) | 20 | Fraktur on |
1 | Bold on (increase intensity) | 21 | Double-underline on |
2 | Dim on (faint, or decrease intensity) | 22 | Bold and dim off |
3 | Italic on | 23 | Italic and Fraktur off |
4 | Underline on | 24 | Underline (and double-underline) off |
5 | Blink on | 25 | Blink off (aka “steady”) |
6 | Rapid-blink on | 26 | unused? |
7 | “Inverse” on (swap fg/bg colours) | 27 | Inverse off (aka “positive”) |
8 | Invisible (hidden) on | 28 | Invisible off (aka “visible”) |
9 | Strike-through on | 29 | Strike-through off |
Code zero (0
) resets all effects and colour selections to defaults.
Note that codes 22
-25
and 27
-29
provide for removing specific effects.
Codes 10-19
(unlisted above) are used for font selection. Code 10
selects the primary
(default) font. Codes 11
-19
select alternate fonts 1-9.
§Basic colours
The basic colour palettes consist of a simple set of eight colours for each of foreground and background-highlight uses. There are also “bright” variants.
Colour | Foreground | Background | Bright foreground | Bright background |
---|---|---|---|---|
Black | 30 | 40 | 90 | 100 |
Red | 31 | 41 | 91 | 101 |
Green | 32 | 42 | 92 | 102 |
Yellow | 33 | 43 | 93 | 103 |
Blue | 34 | 44 | 94 | 104 |
Magenta | 35 | 45 | 95 | 105 |
Cyan | 36 | 46 | 96 | 106 |
White | 37 | 47 | 97 | 107 |
Extended | 38 | 48 | n/a | n/a |
Default (reset) | 39 | 49 | n/a | n/a |
The “extended” codes 38
and 48
are used to start multi-code “extended” colour palette
selections, as discussed shortly.
The “default” codes 39
and 49
are used to reset foreground and background-highlight colours
respectively to defaults, and apply to bright and extended colouring also.
Note that the “bright” codes are not actually a part of the official ANSI standard, apparently.
§Extended range colours
Wider choice of colours than above is made available as extended colours through use of a sequence of codes (using the normal semi-colon separator).
There are two ranges available, an 8-bit (256 colour) range, and a 24-bit RGB range. Both begin
with either 38
to choose text (foreground colouring) or 48
for background-highlight
colouring. This is followed by 5
for specifying 256-colour or 2
for specifying RGB colour.
With 256-colour a final third number with a value in the range of 0-255 selects the specific
colour. With RGB, three numbers must be given, each corresponding to red, green and blue
respectively, and each also being of a value in the range 0-255.
See here for a 256-colour chart.
As an example, seq!(38,5,238)
changes the colour of text (not background) since it starts with
38
, it is providing a 256-colour palette selection (the 5
), and is specifically selecting
colour 238
from that palette.
As another example, seq!(48,2,180,15,70)
changes the colour of the text background since it
starts with 48
, it is providing an RGB colour (the 2
), and is then followed with RGB values
of 180
for red, 15
for green and 70
for blue.
§Resources
A collection of useful or informative related resources:
- Wikipedia: ANSI_escape_code (Specifically the CSI-SGR stuff)
- Wikipedia: Xterm
- Xterm control sequence documentation
- 256-color chart
Modules§
- Code constants
- Predefined sequence strings
- Formatted output support helpers
Macros§
- Constructs a 256-colour background colour code set (to be used in a control sequence)
- Constructs a 256-colour foreground (text) colour code set (to be used in a control sequence)
- Constructs a code set (to be used in a control sequence)
- Constructs an RGB background colour code set (to be used in a control sequence)
- Constructs an RGB foreground (text) colour code set (to be used in a control sequence)
- Constructs a control sequence