| Crates.io | colorable |
| lib.rs | colorable |
| version | 0.1.4 |
| created_at | 2020-09-01 17:30:48.174643+00 |
| updated_at | 2020-10-16 21:44:38.97747+00 |
| description | An easy way to get colored console output |
| homepage | |
| repository | https://github.com/Sp00ph/colorable/ |
| max_upload_size | |
| id | 283482 |
| size | 20,559 |
colorable is a library that makes printing colored output to the console easy.
[dependecies]
colorable = "0.1"
That's the only dependency you need
colorable allows you to style console output using ANSI escape sequences.
It allows this for any types that implement std::fmt::Display or std::fmt::Debug.
std::fmt::DisplayBasic example:
use colorable::*;
fn main() {
println!("{}", 1.green());
println!("{}", 2.blue().bold());
println!("{}", 3.with_fg_color(124).italic());
}
This will produce a green 1, a bold blue 2 and a red italic 3. The 124 in the last line corresponds to the ANSI color code for this shade of red.
You can also set the background of the output like so:
use colorable::*;
fn main() {
println!("{}", 1.magenta().with_bg_color(3));
}
This will produce a magenta 3 on blue background. Currently setting the background
only works through explicit color codes which you can find here
std::fmt::DebugThe API for using colors and styles on objects that implement std::fmt::Debug is exactly the same as it for std::fmt::Display, except that every
method has a _dbg suffix. This is done to avoid name clashing on types that implement both std::fmt::Debug and std::fmt::Display.
The example from above could look something like this:
use colorable::*;
fn main() {
let v = vec![1, 2, 3];
println!("{:?}", v.yellow_dbg());
println!("{:?}", v.cyan_dbg().bold_dbg());
}
Neither the Colored
use colorable::*;
fn main() {
println!("{:#?}", vec![1, 2, 3].dark_cyan_dbg());
}
will simply be ignored and the output won't be pretty printed. If you want to use formatting flags, you can do something like this:
use colorable::*;
fn main() {
println!("{}", format_args!("{:#?}", vec![1, 2, 3]).dark_cyan());
}