#colored #terminal #ansi

colorable

An easy way to get colored console output

5 releases

0.1.4 Oct 16, 2020
0.1.3 Oct 8, 2020
0.1.2 Oct 8, 2020
0.1.1 Oct 8, 2020
0.1.0 Sep 1, 2020

#13 in #colored

Download history 44/week @ 2024-04-15 31/week @ 2024-04-22 19/week @ 2024-04-29 37/week @ 2024-05-06 92/week @ 2024-05-13 37/week @ 2024-05-20 23/week @ 2024-05-27 27/week @ 2024-06-03 33/week @ 2024-06-10 8/week @ 2024-06-17 23/week @ 2024-06-24 7/week @ 2024-07-01 10/week @ 2024-07-08 28/week @ 2024-07-15 23/week @ 2024-07-22 24/week @ 2024-07-29

85 downloads per month
Used in 4 crates (2 directly)

Custom license

20KB
563 lines

colorable

colorable is a library that makes printing colored output to the console easy.

Get started

Adding colorable as a dependency

[dependecies]
colorable = "0.1"

That's the only dependency you need

Usage

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.

How to use on types that implement std::fmt::Display

Basic 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

How to use on types that implement std::fmt::Debug

The 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());
}

NOTE

Neither the Colored nor the ColoredDbg type use any kind of formatting flags. This means that the formatting flags on something like this:

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());
}

No runtime deps