This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals.
This crate works with Cargo. Add the following to your
Cargo.toml dependencies section:
 = "0.7"
There are two main data structures in this crate that you need to be concerned with:
Style holds stylistic information: colours, whether the text should be bold, or blinking, or whatever.
There are also
Colour variants that represent simple foreground colour styles.
ANSIString is a string paired with a
(Yes, it’s British English, but you won’t have to write “colour” very often.
Style is used the majority of the time.)
To format a string, call the
paint method on a
Style or a
Colour, passing in the string you want to format as the argument.
For example, here’s how to get some red text:
use Red; println!;
It’s important to note that the
paint method does not actually return a string with the ANSI control characters surrounding it.
Instead, it returns an
ANSIString value that has a
Display implementation that, when formatted, returns the characters.
This allows strings to be printed with a minimum of
String allocations being performed behind the scenes.
If you do want to get at the escape codes, then you can convert the
ANSIString to a string as you would any other
use Red; use ToString; let red_string = Red.paint.to_string;
Bold, underline, background, and other styles
For anything more complex than plain foreground colour changes, you need to construct
Style objects themselves, rather than beginning with a
You can do this by chaining methods based on a new
Style, created with
Each method creates a new style that has that specific property set.
use Style; println!;
For brevity, these methods have also been implemented for
Colour values, so you can give your styles a foreground colour without having to begin with an empty
use ; println!; println!;
The complete list of styles you can use are:
on for background colours.
In some cases, you may find it easier to change the foreground on an existing
Style rather than starting from the appropriate
You can do this using the
use Style; use ; println!; println!;
Finally, you can turn a
Colour into a
Style with the
This will produce the exact same
ANSIString as if you just used the
paint method on the
Colour directly, but it’s useful in certain cases: for example, you may have a method that returns
Styles, and need to represent both the “red bold” and “red, but not bold” styles with values of the same type. The
Style struct also has a
Default implementation if you want to have a style with nothing set.
use Style; use Red; Red.normal.paint; default.paint;
You can access the extended range of 256 colours by using the
Fixed colour variant, which takes an argument of the colour number to use.
This can be included wherever you would use a
use Fixed; Fixed.paint; Fixed.on.paint;
The first sixteen of these values are the same as the normal and bold standard colour variants.
There’s nothing stopping you from using these as
Fixed colours instead, but there’s nothing to be gained by doing so either.
You can also access full 24-bit color by using the
RGB colour variant, which takes separate
u8 arguments for red, green, and blue:
use RGB; RGB.paint;
Combining successive coloured strings
The benefit of writing ANSI escape codes to the terminal is that they stack: you do not need to end every coloured string with a reset code if the text that follows it is of a similar style. For example, if you want to have some blue text followed by some blue bold text, it’s possible to send the ANSI code for blue, followed by the ANSI code for bold, and finishing with a reset code without having to have an extra one between the two strings.
This crate can optimise the ANSI codes that get printed in situations like this, making life easier for your terminal renderer.
ANSIStrings struct takes a slice of several
ANSIString values, and will iterate over each of them, printing only the codes for the styles that need to be updated as part of its formatting routine.
The following code snippet uses this to enclose a binary number displayed in red bold text inside some red, but not bold, brackets:
use Red; use ; let some_value = format!; let strings: & = &; println!;
There are several things to note here.
paint method can take either an owned
String or a borrowed
ANSIString holds a copy-on-write (
Cow) string value to deal with both owned and borrowed strings at the same time.
This is used here to display a
String, the result of the
format! call, using the same mechanism as some statically-available
Secondly, that the
ANSIStrings value works in the same way as its singular counterpart, with a
Display implementation that only performs the formatting when required.