yansi-term
Adapted from
rust-ansi-term
Refactor for use
fmt::Display
andFnOnce(&mut fmt::Formatter) -> fmt::Result
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
This crate works with Cargo. Add the following to your Cargo.toml
dependencies section:
[]
= "0.1"
Basic usage
There are three main types in this crate that you need to be concerned with: ANSIString
, Style
, and Colour
.
A Style
holds stylistic information: foreground and background colours, whether the text should be bold, or blinking, or other properties.
The Colour
enum represents the available colours.
And an ANSIString
is a string paired with a Style
.
Color
is also available as an alias to Colour
.
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 Display
value:
use Red;
let red_string = Red.paint.to_string;
Note for Windows 10 users: On Windows 10, the application must enable ANSI support first:
let enabled = enable_ansi_support;
Bold, underline, background, and other styles
For anything more complex than plain foreground colour changes, you need to construct Style
values themselves, rather than beginning with a Colour
.
You can do this by chaining methods based on a new Style
, created with Style::new()
.
Each method creates a new style that has that specific property set.
For example:
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 Style
value:
use ;
println!;
println!;
The complete list of styles you can use are:
bold
, dimmed
, italic
, underline
, blink
, reverse
, hidden
, and 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 Colour
.
You can do this using the fg
method:
use Style;
use ;
println!;
println!;
You can turn a Colour
into a Style
with the normal
method.
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;
Extended colours
You can access the extended range of 256 colours by using the Colour::Fixed
variant, which takes an argument of the colour number to use.
This can be included wherever you would use a Colour
:
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 colour by using the Colour::RGB
variant, which takes separate u8
arguments for red, green, and blue:
use RGB;
RGB.paint;