README.md
1# yansi-term [![Latest version](https://img.shields.io/crates/v/yansi-term.svg)](https://crates.io/crates/yansi-term) [![Build Status](https://travis-ci.org/botika/yansi-term.svg?branch=master)](https://travis-ci.org/botika/yansi-term)
2> Adapted from [`rust-ansi-term`](https://github.com/ogham/rust-ansi-term)
3
4Refactor for use [`fmt::Display`](https://doc.rust-lang.org/std/fmt/trait.Display.html) and `FnOnce(&mut fmt::Formatter) -> fmt::Result`
5
6
7This is a library for controlling colours and formatting, such as red bold text or blue underlined text, on ANSI terminals.
8
9### [View the Rustdoc](https://docs.rs/yansi-term)
10
11
12# Installation
13
14This crate works with [Cargo](http://crates.io). Add the following to your `Cargo.toml` dependencies section:
15
16```toml
17[dependencies]
18yansi-term = "0.1"
19```
20
21
22## Basic usage
23
24There are two main types in this crate that you need to be concerned with: `Style`, and `Colour`.
25
26A `Style` holds stylistic information: foreground and background colours, whether the text should be bold, or blinking, or other properties.
27The `Colour` enum represents the available colours.
28
29`Color` is also available as an alias to `Colour`.
30
31To format a string, call the `paint` method on a `Style` or a `Colour`, passing in the string you want to format as the argument.
32For example, here’s how to get some red text:
33
34```rust
35use yansi_term::Colour::Red;
36
37println!("This is in red: {}", Red.paint("a red string"));
38```
39
40**Note for Windows 10 users:** On Windows 10, the application must enable ANSI support first:
41
42```rust,ignore
43let enabled = yansi_term::enable_ansi_support();
44```
45
46## Bold, underline, background, and other styles
47
48For anything more complex than plain foreground colour changes, you need to construct `Style` values themselves, rather than beginning with a `Colour`.
49You can do this by chaining methods based on a new `Style`, created with `Style::new()`.
50Each method creates a new style that has that specific property set.
51For example:
52
53```rust
54use yansi_term::Style;
55
56println!("How about some {} and {}?",
57 Style::new().bold().paint("bold"),
58 Style::new().underline().paint("underline"));
59```
60
61For 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:
62
63```rust
64use yansi_term::Colour::{Blue, Yellow};
65
66println!("Demonstrating {} and {}!",
67 Blue.bold().paint("blue bold"),
68 Yellow.underline().paint("yellow underline"));
69
70println!("Yellow on blue: {}", Yellow.on(Blue).paint("wow!"));
71```
72
73The complete list of styles you can use are:
74`bold`, `dimmed`, `italic`, `underline`, `blink`, `reverse`, `hidden`, and `on` for background colours.
75
76In some cases, you may find it easier to change the foreground on an existing `Style` rather than starting from the appropriate `Colour`.
77You can do this using the `fg` method:
78
79```rust
80use yansi_term::Style;
81use yansi_term::Colour::{Blue, Cyan, Yellow};
82
83println!("Yellow on blue: {}", Style::new().on(Blue).fg(Yellow).paint("yow!"));
84println!("Also yellow on blue: {}", Cyan.on(Blue).fg(Yellow).paint("zow!"));
85```
86
87You can turn a `Colour` into a `Style` with the `normal` method.
88
89```rust
90use yansi_term::Style;
91use yansi_term::Colour::Red;
92
93Red.normal().paint("yet another red string");
94Style::default().paint("a completely regular string");
95```
96
97
98## Extended colours
99
100You can access the extended range of 256 colours by using the `Colour::Fixed` variant, which takes an argument of the colour number to use.
101This can be included wherever you would use a `Colour`:
102
103```rust
104use yansi_term::Colour::Fixed;
105
106Fixed(134).paint("A sort of light purple");
107Fixed(221).on(Fixed(124)).paint("Mustard in the ketchup");
108```
109
110The first sixteen of these values are the same as the normal and bold standard colour variants.
111There’s nothing stopping you from using these as `Fixed` colours instead, but there’s nothing to be gained by doing so either.
112
113You can also access full 24-bit colour by using the `Colour::RGB` variant, which takes separate `u8` arguments for red, green, and blue:
114
115```rust
116use yansi_term::Colour::RGB;
117
118RGB(70, 130, 180).paint("Steel blue");
119```
120