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 4 commits into
base: v3
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 .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ jobs:
run: cargo test
check-semver:
name: Check semver compatibility
if: github.base_ref == 'master'
runs-on: ubuntu-latest
steps:
- name: Checkout repository
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- Changes to `Styles`:
- Implemented bitwise operators `BitAnd`, `BitOr`, `BitXor`, and `Not` which all combine `Styles`\'s and output `Style`\'s. These can also take a `Style` as an operand.
- Added additional testing for all of the above changes.
- Added methods `with_style` and `with_color_and_style` to `Colorize`.
- Added methods `with_style`, `with_color_and_style`, `ansi_color` and `on_ansi_color` to `Colorize`.
- Replaced lazy_static with `std::sync::OnceLock`. Soft breaking change by removing the global `SHOULD_COLORIZE`.

# 2.1.0
Expand Down
4 changes: 4 additions & 0 deletions src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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()
}
Color::TrueColor { r, g, b } => format!("38;2;{};{};{}", r, g, b).into(),
Color::AnsiColor(code) => format!("38;5;{}", code).into(),
}
}

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

Expand Down Expand Up @@ -195,6 +198,7 @@ impl Color {
b: 255,
},
TrueColor { r, g, b } => TrueColor { r, g, b },
AnsiColor(color) => AnsiColor(color),
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,14 @@ 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 +405,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