| 1 | # Colored |
| 2 | |
| 3 | [](https://crates.io/crates/colored) [](https://github.com/mackwic/colored/blob/master/LICENSE) |
| 4 | |
| 5 | Coloring terminal so simple, you already know how to do it! |
| 6 | |
| 7 | ```rust |
| 8 | "this is blue".blue(); |
| 9 | "this is red".red(); |
| 10 | "this is red on blue".red().on_blue(); |
| 11 | "this is also red on blue".on_blue().red(); |
| 12 | "you can use truecolor values too!".truecolor(0, 255, 136); |
| 13 | "background truecolor also works :)".on_truecolor(135, 28, 167); |
| 14 | "truecolor from tuple".custom_color((0, 255, 136)); |
| 15 | "background truecolor from tuple".on_custom_color((0, 255, 136)); |
| 16 | "bright colors are welcome as well".on_bright_blue().bright_red(); |
| 17 | "you can also make bold text".bold(); |
| 18 | println!("{} {} {}", "or use".cyan(), "any".italic().yellow(), "string type".cyan()); |
| 19 | "or change advice. This is red".yellow().blue().red(); |
| 20 | "or clear things up. This is default color and style".red().bold().clear(); |
| 21 | "purple and magenta are the same".purple().magenta(); |
| 22 | "and so are normal and clear".normal().clear(); |
| 23 | "you can specify color by string".color("blue").on_color("red"); |
| 24 | "you can also use hex colors".color("#0057B7").on_color("#fd0"); |
| 25 | String::from("this also works!").green().bold(); |
| 26 | format!("{:30}", "format works as expected. This will be padded".blue()); |
| 27 | format!("{:.3}", "and this will be green but truncated to 3 chars".green()); |
| 28 | ``` |
| 29 | |
| 30 | ## How to use |
| 31 | |
| 32 | Add this in your `Cargo.toml`: |
| 33 | |
| 34 | ```toml |
| 35 | [dependencies] |
| 36 | colored = "3" |
| 37 | ``` |
| 38 | |
| 39 | and add this to your `lib.rs` or `main.rs`: |
| 40 | |
| 41 | ```rust |
| 42 | extern crate colored; // not needed in Rust 2018+ |
| 43 | |
| 44 | use colored::Colorize; |
| 45 | |
| 46 | // test the example with `cargo run --example most_simple` |
| 47 | fn main() { |
| 48 | // TADAA! |
| 49 | println!("{} {} !", "it".green(), "works".blue().bold()); |
| 50 | } |
| 51 | ``` |
| 52 | |
| 53 | ## Features |
| 54 | |
| 55 | - Safe rust, easy to use, minimal dependencies, complete test suite |
| 56 | - Respect the `CLICOLOR`/`CLICOLOR_FORCE` behavior (see [the specs](http://bixense.com/clicolors/)) |
| 57 | - Respect the `NO_COLOR` behavior (see [the specs](https://no-color.org/)) |
| 58 | - Do note that `CLICOLOR_FORCE` overrules `NO_COLOR`, which overrules `CLICOLOR` |
| 59 | - Works on Linux, MacOS, and Windows (Powershell) |
| 60 | |
| 61 | #### Colors: |
| 62 | |
| 63 | - black |
| 64 | - red |
| 65 | - green |
| 66 | - yellow |
| 67 | - blue |
| 68 | - magenta (or purple) |
| 69 | - cyan |
| 70 | - white |
| 71 | |
| 72 | Bright colors: prepend the color by `bright_`. So easy. |
| 73 | Background colors: prepend the color by `on_`. Simple as that. |
| 74 | Bright Background colors: prepend the color by `on_bright_`. Not hard at all. |
| 75 | |
| 76 | #### Truecolors |
| 77 | |
| 78 | Colored has support for truecolors where you can specify any arbitrary rgb value. |
| 79 | |
| 80 | This feature will only work correctly in terminals which support true colors (i.e. most modern terminals). |
| 81 | |
| 82 | You can check if your terminal supports true color by checking the value of the environment variable `$COLORTERM` on your terminal. A value of `truecolor` or `24bit` indicates that it will work. |
| 83 | |
| 84 | #### Styles: |
| 85 | |
| 86 | - bold |
| 87 | - underline |
| 88 | - italic |
| 89 | - dimmed |
| 90 | - reversed |
| 91 | - blink |
| 92 | - hidden |
| 93 | - strikethrough |
| 94 | |
| 95 | You can clear color _and_ style anytime by using `normal()` or `clear()` |
| 96 | |
| 97 | #### Advanced Control: |
| 98 | |
| 99 | ##### Dynamic color from str |
| 100 | |
| 101 | As `Color` implements `FromStr`, `From<&str>`, and `From<String>`, you can easily cast a string into a color like that: |
| 102 | |
| 103 | ```rust |
| 104 | // the easy way |
| 105 | "blue string yo".color("blue"); |
| 106 | |
| 107 | // this will default to white |
| 108 | "white string".color("zorglub"); |
| 109 | |
| 110 | // the safer way via a Result |
| 111 | let color_res : Result<Color, ()> = "zorglub".parse(); |
| 112 | "red string".color(color_res.unwrap_or(Color::Red)); |
| 113 | ``` |
| 114 | |
| 115 | |
| 116 | ##### Colorization control |
| 117 | |
| 118 | If you want to disable any coloring at compile time, you can simply do so by |
| 119 | using the `no-color` feature. |
| 120 | |
| 121 | For example, you can do this in your `Cargo.toml` to disable color in tests: |
| 122 | |
| 123 | ```toml |
| 124 | [features] |
| 125 | # this effectively enable the feature `no-color` of colored when testing with |
| 126 | # `cargo test --features dumb_terminal` |
| 127 | dumb_terminal = ["colored/no-color"] |
| 128 | ``` |
| 129 | |
| 130 | You can have even finer control by using the |
| 131 | `colored::control::set_override` method. |
| 132 | |
| 133 | ## Todo |
| 134 | |
| 135 | - **More tests ?**: We always welcome more tests! Please contribute! |
| 136 | |
| 137 | ## Credits |
| 138 | |
| 139 | This library wouldn't have been the same without the marvelous ruby gem [colored](https://github.com/defunkt/colored). |
| 140 | |
| 141 | Thanks for the [ansi\_term crate](https://github.com/ogham/rust-ansi-term) for |
| 142 | providing a reference implementation, which greatly helped making this crate |
| 143 | output correct strings. |
| 144 | |
| 145 | ## Minimum Supported Rust Version (MSRV) |
| 146 | The current MSRV is `1.80`, which is checked and enforced automatically via CI. This version may change in the future in minor version bumps, so if you require a specific Rust version you should use a restricted version requirement such as `~X.Y`. |
| 147 | |
| 148 | ## License |
| 149 | |
| 150 | [Mozilla Public License 2.0](https://www.mozilla.org/en-US/MPL/2.0/). See the |
| 151 | [LICENSE](https://github.com/mackwic/colored/blob/master/LICENSE) file at the |
| 152 | root of the repository. |
| 153 | |
| 154 | In non legal terms it means that: |
| 155 | - if you fix a bug, you MUST give me the code of the fix (it's only fair) |
| 156 | - if you change/extend the API, you MUST give me the code you changed in the |
| 157 | files under MPL2. |
| 158 | - you CAN'T sue me for anything about this code |
| 159 | - apart from that, you can do almost whatever you want. See the LICENSE file |
| 160 | for details. |
| 161 | |
| 162 | ## Contributors |
| 163 | |
| 164 | - Thomas Wickham: [@mackwic](https://github.com/mackwic) |
| 165 | - Hunter Wittenborn [@hwittenborn](https://github.com/hwittenborn) |
| 166 | - Corey "See More" Richardson: [@cmr](https://github.com/cmr) |
| 167 | - Iban Eguia: [@Razican](https://github.com/Razican) |
| 168 | - Alexis "Horgix" Chotard: [@horgix](https://github.com/horgix) |
| 169 | - Keith Yeung: [@KiChjang](https://github.com/KiChjang) |
| 170 | - Kyle Galloway: [@kylegalloway](https://github.com/kylegalloway) |
| 171 | - Luke Hsiao: [@lukehsiao](https://github.com/lukehsiao) |
| 172 | - kurtlawrence: [@kurtlawrence](https://github.com/kurtlawrence) |