- Notifications
You must be signed in to change notification settings - Fork86
(Rust) Coloring terminal so simple you already know how to do it !
License
colored-rs/colored
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Coloring terminal so simple, you already know how to do it!
"this is blue".blue();"this is red".red();"this is red on blue".red().on_blue();"this is also red on blue".on_blue().red();"you can use truecolor values too!".truecolor(0,255,136);"background truecolor also works :)".on_truecolor(135,28,167);"truecolor from tuple".custom_color((0,255,136));"background truecolor from tuple".on_custom_color((0,255,136));"bright colors are welcome as well".on_bright_blue().bright_red();"you can also make bold comments".bold();println!("{} {} {}","or use".cyan(),"any".italic().yellow(),"string type".cyan());"or change advice. This is red".yellow().blue().red();"or clear things up. This is default color and style".red().bold().clear();"purple and magenta are the same".purple().magenta();"and so are normal and clear".normal().clear();"you can specify color by string".color("blue").on_color("red");"you can also use hex colors".color("#0057B7").on_color("#fd0");String::from("this also works!").green().bold();format!("{:30}","format works as expected. This will be padded".blue());format!("{:.3}","and this will be green but truncated to 3 chars".green());
Add this in yourCargo.toml
:
[dependencies]colored ="3"
and add this to yourlib.rs
ormain.rs
:
externcrate colored;// not needed in Rust 2018+use colored::Colorize;// test the example with `cargo run --example most_simple`fnmain(){// TADAA!println!("{} {} !","it".green(),"works".blue().bold());}
- Safe rust, easy to use, minimal dependencies, complete test suite
- Respect the
CLICOLOR
/CLICOLOR_FORCE
behavior (seethe specs) - Respect the
NO_COLOR
behavior (seethe specs) - Do note that
CLICOLOR_FORCE
overrulesNO_COLOR
, which overrulesCLICOLOR
- Works on Linux, MacOS, and Windows (Powershell)
- black
- red
- green
- yellow
- blue
- magenta (or purple)
- cyan
- white
Bright colors: prepend the color bybright_
. So easy.Background colors: prepend the color byon_
. Simple as that.Bright Background colors: prepend the color byon_bright_
. Not hard at all.
Colored has support for truecolors where you can specify any arbitrary rgb value.
This feature will only work correctly in terminals which support true colors (i.e. most modern terminals).
You can check if your terminal supports true color by checking the value of the environment variable$COLORTERM
on your terminal. A value oftruecolor
or24bit
indicates that it will work.
- bold
- underline
- italic
- dimmed
- reversed
- blink
- hidden
- strikethrough
You can clear colorand style anytime by usingnormal()
orclear()
AsColor
implementsFromStr
,From<&str>
, andFrom<String>
, you can easily cast a string into a color like that:
// the easy way"blue string yo".color("blue");// this will default to white"white string".color("zorglub");// the safer way via a Resultlet color_res:Result<Color,()> ="zorglub".parse();"red string".color(color_res.unwrap_or(Color::Red));
If you want to disable any coloring at compile time, you can simply do so byusing theno-color
feature.
For example, you can do this in yourCargo.toml
to disable color in tests:
[features]# this effectively enable the feature `no-color` of colored when testing with# `cargo test --features dumb_terminal`dumb_terminal = ["colored/no-color"]
You can have even finer control by using thecolored::control::set_override
method.
- More tests ?: We always welcome more tests! Please contribute!
This library wouldn't have been the same without the marvelous ruby gemcolored.
Thanks for theansi_term crate forproviding a reference implementation, which greatly helped making this crateoutput correct strings.
The current MSRV is1.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
.
Mozilla Public License 2.0. See theLICENSE file at theroot of the repository.
In non legal terms it means that:
- if you fix a bug, you MUST give me the code of the fix (it's only fair)
- if you change/extend the API, you MUST give me the code you changed in thefiles under MPL2.
- you CAN'T sue me for anything about this code
- apart from that, you can do almost whatever you want. See the LICENSE filefor details.
- Thomas Wickham:@mackwic
- Hunter Wittenborn@hwittenborn
- Corey "See More" Richardson:@cmr
- Iban Eguia:@Razican
- Alexis "Horgix" Chotard:@horgix
- Keith Yeung:@KiChjang
- Kyle Galloway:@kylegalloway
- Luke Hsiao:@lukehsiao
- kurtlawrence:@kurtlawrence
About
(Rust) Coloring terminal so simple you already know how to do it !
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.