Skip to content

Commit

Permalink
Add support for TrueColor (#78)
Browse files Browse the repository at this point in the history
* Add support for TrueColor

* alter Color interface to Cow<'static, str>

Co-authored-by: Kurt Lawrence <kurtlawrence92@gmail.com>
  • Loading branch information
MichaelAquilina and kurtlawrence authored Jul 14, 2020
1 parent 2b94a69 commit 18b5ff8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 41 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@

# 2.0.0 (July 14, 2020)
- Add support for true colours.
- Alter `Color` interface to return `Cow<'static, str>`

# 1.9.3 (February 24, 2020)
- Fix compilation regression for 1.34.0. Thanks @jlevon for reporting.

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "colored"
description = "The most simple way to add colors in your terminal"
version = "1.9.3"
version = "2.0.0"
authors = ["Thomas Wickham <mackwic@gmail.com>"]
license = "MPL-2.0"
homepage = "https://github.com/mackwic/colored"
Expand Down
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Coloring terminal so simple, you already know how to do it!
"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);
"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());
Expand All @@ -36,7 +38,7 @@ and add this to your `lib.rs` or `main.rs`:

```rust
extern crate colored; // not needed in Rust 2018

use colored::*;

// test the example with `cargo run --example most_simple`
Expand Down Expand Up @@ -68,6 +70,14 @@ Bright colors: prepend the color by `bright_`. So easy.
Background colors: prepend the color by `on_`. Simple as that.
Bright Background colors: prepend the color by `on_bright_`. Not hard at all.

#### Truecolors

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 of `truecolor` or `24bit` indicates that it will work.

#### Styles:

- bold
Expand Down Expand Up @@ -172,4 +182,3 @@ In non legal terms it means that:
- Kyle Galloway: [@kylegalloway](https://github.com/kylegalloway)
- Luke Hsiao: [@lukehsiao](https://github.com/lukehsiao)
- kurtlawrence: [@kurtlawrence](https://github.com/kurtlawrence)

74 changes: 38 additions & 36 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::convert::From;
use std::str::FromStr;
use std::{borrow::Cow, str::FromStr};

/// The 8 standard colors.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand All @@ -21,49 +20,52 @@ pub enum Color {
BrightMagenta,
BrightCyan,
BrightWhite,
TrueColor { r: u8, g: u8, b: u8 },
}

#[allow(missing_docs)]
impl Color {
pub fn to_fg_str(&self) -> &str {
pub fn to_fg_str(&self) -> Cow<'static, str> {
match *self {
Color::Black => "30",
Color::Red => "31",
Color::Green => "32",
Color::Yellow => "33",
Color::Blue => "34",
Color::Magenta => "35",
Color::Cyan => "36",
Color::White => "37",
Color::BrightBlack => "90",
Color::BrightRed => "91",
Color::BrightGreen => "92",
Color::BrightYellow => "93",
Color::BrightBlue => "94",
Color::BrightMagenta => "95",
Color::BrightCyan => "96",
Color::BrightWhite => "97",
Color::Black => "30".into(),
Color::Red => "31".into(),
Color::Green => "32".into(),
Color::Yellow => "33".into(),
Color::Blue => "34".into(),
Color::Magenta => "35".into(),
Color::Cyan => "36".into(),
Color::White => "37".into(),
Color::BrightBlack => "90".into(),
Color::BrightRed => "91".into(),
Color::BrightGreen => "92".into(),
Color::BrightYellow => "93".into(),
Color::BrightBlue => "94".into(),
Color::BrightMagenta => "95".into(),
Color::BrightCyan => "96".into(),
Color::BrightWhite => "97".into(),
Color::TrueColor { r, g, b } => format!("38;2;{};{};{}", r, g, b).into(),
}
}

pub fn to_bg_str(&self) -> &str {
pub fn to_bg_str(&self) -> Cow<'static, str> {
match *self {
Color::Black => "40",
Color::Red => "41",
Color::Green => "42",
Color::Yellow => "43",
Color::Blue => "44",
Color::Magenta => "45",
Color::Cyan => "46",
Color::White => "47",
Color::BrightBlack => "100",
Color::BrightRed => "101",
Color::BrightGreen => "102",
Color::BrightYellow => "103",
Color::BrightBlue => "104",
Color::BrightMagenta => "105",
Color::BrightCyan => "106",
Color::BrightWhite => "107",
Color::Black => "40".into(),
Color::Red => "41".into(),
Color::Green => "42".into(),
Color::Yellow => "43".into(),
Color::Blue => "44".into(),
Color::Magenta => "45".into(),
Color::Cyan => "46".into(),
Color::White => "47".into(),
Color::BrightBlack => "100".into(),
Color::BrightRed => "101".into(),
Color::BrightGreen => "102".into(),
Color::BrightYellow => "103".into(),
Color::BrightBlue => "104".into(),
Color::BrightMagenta => "105".into(),
Color::BrightCyan => "106".into(),
Color::BrightWhite => "107".into(),
Color::TrueColor { r, g, b } => format!("48;2;{};{};{}", r, g, b).into(),
}
}
}
Expand Down
21 changes: 19 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//! "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);
//! "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();
Expand Down Expand Up @@ -169,6 +171,12 @@ pub trait Colorize {
{
self.color(Color::BrightWhite)
}
fn truecolor(self, r: u8, g: u8, b: u8) -> ColoredString
where
Self: Sized,
{
self.color(Color::TrueColor { r, g, b })
}
fn color<S: Into<Color>>(self, color: S) -> ColoredString;
// Background Colors
fn on_black(self) -> ColoredString
Expand Down Expand Up @@ -279,6 +287,12 @@ pub trait Colorize {
{
self.on_color(Color::BrightWhite)
}
fn on_truecolor(self, r: u8, g: u8, b: u8) -> ColoredString
where
Self: Sized,
{
self.on_color(Color::TrueColor { r, g, b })
}
fn on_color<S: Into<Color>>(self, color: S) -> ColoredString;
// Styles
fn clear(self) -> ColoredString;
Expand Down Expand Up @@ -378,7 +392,7 @@ impl ColoredString {
res.push(';');
}

res.push_str(bgcolor.to_bg_str());
res.push_str(&bgcolor.to_bg_str());
has_wrote = true;
}

Expand All @@ -387,7 +401,7 @@ impl ColoredString {
res.push(';');
}

res.push_str(fgcolor.to_fg_str());
res.push_str(&fgcolor.to_fg_str());
}

res.push('m');
Expand Down Expand Up @@ -631,6 +645,9 @@ mod tests {
println!("{}", toto.cyan());
println!("{}", toto.white());
println!("{}", toto.white().red().blue().green());
println!("{}", toto.truecolor(255, 0, 0));
println!("{}", toto.truecolor(255, 255, 0));
println!("{}", toto.on_truecolor(0, 80, 80));
// uncomment to see term output
// assert!(false)
}
Expand Down

0 comments on commit 18b5ff8

Please sign in to comment.