rust-ansi-term 
This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals.
View the Rustdoc
Installation
It uses Cargo, Rust's package manager. You can depend on this library by adding this Git repository to your Cargo dependencies:
[]
= "https://github.com/ogham/rust-ansi-term.git"
Usage
extern crate ansi_term;
use ;
use Style;
Simple Colours
You can format strings by calling the paint
method on a Colour
or a Style object, passing in the string you want to format. For
example, to get some red text, call the paint
method on Red
:
println!;
The paint
method returns an ANSIString
object, which will get
automatically converted to the correct sequence of escape codes when
used in a println!
or format!
macro, or anything else that
supports using the Show
trait. This means that if you just want a
string of the escape codes without anything else, you can still use
the to_string
method:
let red_string: String = Red.paint.to_string;
Bold, Underline, and Background
To do anything more complex than just foreground colours, you need
to use Style objects. Calling the bold
or underline
method on
a Colour returns a Style that has the appropriate property set on
it:
println!;
These methods chain, so you can call them on existing Style objects to set more than one particular properly, like so:
Blue.underline.bold.paint
You can set the background colour of a Style by using the on
method:
Blue.on.paint
Finally, you can turn a Colour into a Style with the normal
method, though it'll produce the exact same string if you just use
the Colour. It's only useful if you're writing a method that can
return either normal or bold (or underline) styles, and need to
return a Style object from it.
Red.normal.paint
Extended Colours
You can access the extended range of 256 colours by using the Fixed constructor, which takes an argument of the colour number to use. This can be used wherever you would use a Colour:
Fixed.paint
This even works for background colours:
Fixed.on.paint
No Formatting
Finally, for the sake of completeness, the default style provides neither colours nor formatting.
.paint
default