Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for arbitrary ANSI color #166

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Unreleased
- Added methods `ansi_color` and `on_ansi_color` to `Colorize`.

# 3.0.0
- **[BREAKING CHANGE]:** Upgrade MSRV to 1.80 and remove the then unnecessary lazy_static dependency.
Expand Down
8 changes: 6 additions & 2 deletions src/color.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{borrow::Cow, cmp, env, str::FromStr};
use Color::{
Black, Blue, BrightBlack, BrightBlue, BrightCyan, BrightGreen, BrightMagenta, BrightRed,
BrightWhite, BrightYellow, Cyan, Green, Magenta, Red, TrueColor, White, Yellow,
AnsiColor, Black, Blue, BrightBlack, BrightBlue, BrightCyan, BrightGreen, BrightMagenta,
BrightRed, BrightWhite, BrightYellow, Cyan, Green, Magenta, Red, TrueColor, White, Yellow,
};
/// The 8 standard colors.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand All @@ -24,6 +24,7 @@ pub enum Color {
BrightCyan,
BrightWhite,
TrueColor { r: u8, g: u8, b: u8 },
AnsiColor(u8),
}

fn truecolor_support() -> bool {
Expand Down Expand Up @@ -56,6 +57,7 @@ impl Color {
self.closest_color_euclidean().to_fg_str()
}
Self::TrueColor { r, g, b } => format!("38;2;{r};{g};{b}").into(),
Self::AnsiColor(code) => format!("38;5;{code}").into(),
}
}

Expand All @@ -82,6 +84,7 @@ impl Color {
self.closest_color_euclidean().to_bg_str()
}
Self::TrueColor { r, g, b } => format!("48;2;{r};{g};{b}").into(),
Self::AnsiColor(code) => format!("48;5;{code}").into(),
}
}

Expand Down Expand Up @@ -192,6 +195,7 @@ impl Color {
b: 255,
},
TrueColor { r, g, b } => TrueColor { r, g, b },
AnsiColor(color) => AnsiColor(color),
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,13 @@ pub trait Colorize {
b: color.b,
})
}
fn ansi_color<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<u8>,
{
self.color(Color::AnsiColor(color.into()))
}
fn color<S: Into<Color>>(self, color: S) -> ColoredString;
// Background Colors
fn on_black(self) -> ColoredString
Expand Down Expand Up @@ -397,6 +404,13 @@ pub trait Colorize {
b: color.b,
})
}
fn on_ansi_color<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<u8>,
{
self.on_color(Color::AnsiColor(color.into()))
}
fn on_color<S: Into<Color>>(self, color: S) -> ColoredString;
// Styles
fn clear(self) -> ColoredString;
Expand Down