Crate yansi [] [src]

A dead simple ANSI terminal color painting library.


Usage is best illustrated via a quick example:

use yansi::Paint;
use yansi::Color::White;

println!("Testing, {}, {}, {}!", Paint::red(1),


The main entry point into this library is the Paint type. Paint encapsulates a value of any type that implements the Display or Debug trait. When a Paint is Displayed or Debuged, the appropriate ANSI escape characters are emitted before and after the wrapped type's fmt implementation.

Paint can be constructed via any of following methods: black, red, green, yellow, blue, purple, cyan, white. You can also use the paint method on a given Color value to construct a Paint type. Both of these methods are shown below:

use yansi::Paint;
use yansi::Color::Red;

println!("I'm {}!", Paint::red("red").bold());
println!("I'm also {}!", Red.paint("red").underline());

Each of these methods sets the foreground color of the item to be displayed according to the name of the method. Additionally, rgb and fixed allow you to customize the foreground color to your liking.

Finally, new creates a Paint item without a foreground color applied.


Modifications to the styling of the item can be added via the followiing chainable builder methods: fg, bg, bold, dimmed, italic, underline, blink, invert, hidden, strikethrough.


On Rust nightly and with the nightly feature enabled, painting can be disabled globally via the Paint::disable() method. When painting is disabled, the Display implementation for Paint will emit the Display of the contained object and nothing else. Painting can be reenabled via the Paint::enable() method.

One potential use of this feature is to allow users to control color ouput via an environment variable. For instance, to disable coloring if the CLICOLOR variable is set to 0, you might write:

use yansi::Paint;

if let Ok(true) = std::env::var("CLICOLOR").map(|v| v == "0") {


This is an ANSI terminal coloring library. Unless the Windows terminal supports ANSI colors, colors won't display properly on Windows. This is a bummer, I know. If you'd like, yansi makes it easy to disable coloring on Windows:

use yansi::Paint;

if cfg!(windows) {


Several terminal coloring libraries exist (ansi_term, colored, term_painter, to name a few), begging the question: why yet another? Here are a few reasons:

  • This library is much simpler: there are two types! The complete implementation is under 250 lines of code.
  • Like term_painter, but unlike ansi_term, any type implementing Display can be stylized, not only strings.
  • Styling can be enabled and disabled on the fly.
  • Typically, only one type needs to be imported: Paint.
  • Zero dependencies. It really is simple.
  • The name yansi is pretty short.

All that being said, this library borrows the general API from the three libraries as well as plenty of code from ansi_term.



A structure encapsulating all of the styling for a given item.



An enum representing an ANSI color code.