diff --git a/crates/anstyle-ansi-term/src/lib.rs b/crates/anstyle-ansi-term/src/lib.rs index b3461186..10ed7e80 100644 --- a/crates/anstyle-ansi-term/src/lib.rs +++ b/crates/anstyle-ansi-term/src/lib.rs @@ -58,7 +58,7 @@ pub fn to_ansi_term(astyle: anstyle::Style) -> ansi_term::Style { fn to_ansi_color(color: anstyle::Color) -> (ansi_term::Color, bool) { match color { anstyle::Color::Ansi(ansi) => ansi_to_ansi_color(ansi), - anstyle::Color::XTerm(xterm) => (xterm_to_ansi_color(xterm), false), + anstyle::Color::Ansi256(xterm) => (xterm_to_ansi_color(xterm), false), anstyle::Color::Rgb(rgb) => (rgb_to_ansi_color(rgb), false), } } @@ -84,7 +84,7 @@ fn ansi_to_ansi_color(color: anstyle::AnsiColor) -> (ansi_term::Color, bool) { } } -fn xterm_to_ansi_color(color: anstyle::XTermColor) -> ansi_term::Color { +fn xterm_to_ansi_color(color: anstyle::Ansi256Color) -> ansi_term::Color { ansi_term::Color::Fixed(color.0) } diff --git a/crates/anstyle-crossterm/src/lib.rs b/crates/anstyle-crossterm/src/lib.rs index a0372857..7793caa5 100644 --- a/crates/anstyle-crossterm/src/lib.rs +++ b/crates/anstyle-crossterm/src/lib.rs @@ -58,7 +58,7 @@ pub fn to_crossterm(astyle: anstyle::Style) -> crossterm::style::ContentStyle { fn to_ansi_color(color: anstyle::Color) -> crossterm::style::Color { match color { anstyle::Color::Ansi(ansi) => ansi_to_ansi_color(ansi), - anstyle::Color::XTerm(xterm) => xterm_to_ansi_color(xterm), + anstyle::Color::Ansi256(xterm) => xterm_to_ansi_color(xterm), anstyle::Color::Rgb(rgb) => rgb_to_ansi_color(rgb), } } @@ -84,7 +84,7 @@ fn ansi_to_ansi_color(color: anstyle::AnsiColor) -> crossterm::style::Color { } } -fn xterm_to_ansi_color(color: anstyle::XTermColor) -> crossterm::style::Color { +fn xterm_to_ansi_color(color: anstyle::Ansi256Color) -> crossterm::style::Color { crossterm::style::Color::AnsiValue(color.0) } diff --git a/crates/anstyle-git/src/lib.rs b/crates/anstyle-git/src/lib.rs index bf266d35..0abd1095 100644 --- a/crates/anstyle-git/src/lib.rs +++ b/crates/anstyle-git/src/lib.rs @@ -5,7 +5,7 @@ //! //! ```rust //! let style = anstyle_git::parse("bold red blue").unwrap(); -//! assert_eq!(style, anstyle::AnsiColor::Red | anstyle::AnsiColor::Blue | anstyle::Effects::BOLD); +//! assert_eq!(style, anstyle::AnsiColor::Red.on(anstyle::AnsiColor::Blue) | anstyle::Effects::BOLD); //! //! let hyperlink_style = anstyle_git::parse("#0000ee ul").unwrap(); //! assert_eq!(hyperlink_style, anstyle::RgbColor(0x00, 0x00, 0xee) | anstyle::Effects::UNDERLINE); @@ -196,32 +196,32 @@ mod tests { test!("normal normal" => Style::new()); test!("-1 normal" => Style::new()); test!("red" => Red); - test!("red blue" => Red | Blue); - test!(" red blue " => Red | Blue); - test!("red\tblue" => Red | Blue); - test!("red\n blue" => Red | Blue); - test!("red\r\n blue" => Red | Blue); - test!("blue red" => Blue | Red); - test!("yellow green" => Yellow | Green); - test!("white magenta" => White | Magenta); - test!("black cyan" => Black | Cyan); + test!("red blue" => Red.on(Blue)); + test!(" red blue " => Red.on(Blue)); + test!("red\tblue" => Red.on(Blue)); + test!("red\n blue" => Red.on(Blue)); + test!("red\r\n blue" => Red.on(Blue)); + test!("blue red" => Blue.on(Red)); + test!("yellow green" => Yellow.on(Green)); + test!("white magenta" => White.on(Magenta)); + test!("black cyan" => Black.on(Cyan)); test!("red normal" => Red); test!("normal red" => Style::new().bg_color(Some(Red.into()))); - test!("0" => XTermColor(0)); - test!("8 3" => XTermColor(8) | XTermColor(3)); - test!("255" => XTermColor(255)); - test!("255 -1" => XTermColor(255)); + test!("0" => Ansi256Color(0)); + test!("8 3" => Ansi256Color(8).on(Ansi256Color(3))); + test!("255" => Ansi256Color(255)); + test!("255 -1" => Ansi256Color(255)); test!("#000000" => RgbColor(0,0,0)); test!("#204060" => RgbColor(0x20,0x40,0x60)); - test!("bold cyan white" => (Cyan | White).bold()); - test!("bold cyan nobold white" => Cyan | White); - test!("bold cyan reverse white nobold" => (Cyan | White).invert()); - test!("bold cyan ul white dim" => (Cyan | White).bold().underline().dimmed()); - test!("ul cyan white no-ul" => Cyan | White); - test!("italic cyan white" => (Cyan | White).italic()); - test!("strike cyan white" => (Cyan | White).strikethrough()); - test!("blink #050505 white" => (RgbColor(5,5,5) | White).blink()); + test!("bold cyan white" => Cyan.on(White).bold()); + test!("bold cyan nobold white" => Cyan.on(White)); + test!("bold cyan reverse white nobold" => Cyan.on(White).invert()); + test!("bold cyan ul white dim" => Cyan.on(White).bold().underline().dimmed()); + test!("ul cyan white no-ul" => Cyan.on(White)); + test!("italic cyan white" => Cyan.on(White).italic()); + test!("strike cyan white" => Cyan.on(White).strikethrough()); + test!("blink #050505 white" => RgbColor(5,5,5).on(White).blink()); } #[test] @@ -276,6 +276,6 @@ mod tests { #[test] fn test_extension_trait() { let style = anstyle::Style::parse_git("red blue"); - assert_eq!(style.unwrap(), Red | Blue); + assert_eq!(style.unwrap(), Red.on(Blue)); } } diff --git a/crates/anstyle-lossy/src/lib.rs b/crates/anstyle-lossy/src/lib.rs index a937c371..e2916fa8 100644 --- a/crates/anstyle-lossy/src/lib.rs +++ b/crates/anstyle-lossy/src/lib.rs @@ -5,15 +5,15 @@ use anstyle::RgbColor as Rgb; pub const fn color_to_rgb(color: anstyle::Color, palette: palette::Palette) -> anstyle::RgbColor { match color { anstyle::Color::Ansi(color) => ansi_to_rgb(color, palette), - anstyle::Color::XTerm(color) => xterm_to_rgb(color, palette), + anstyle::Color::Ansi256(color) => xterm_to_rgb(color, palette), anstyle::Color::Rgb(color) => color, } } -pub const fn color_to_xterm(color: anstyle::Color) -> anstyle::XTermColor { +pub const fn color_to_xterm(color: anstyle::Color) -> anstyle::Ansi256Color { match color { - anstyle::Color::Ansi(color) => anstyle::XTermColor::from_ansi(color), - anstyle::Color::XTerm(color) => color, + anstyle::Color::Ansi(color) => anstyle::Ansi256Color::from_ansi(color), + anstyle::Color::Ansi256(color) => color, anstyle::Color::Rgb(color) => rgb_to_xterm(color), } } @@ -21,7 +21,7 @@ pub const fn color_to_xterm(color: anstyle::Color) -> anstyle::XTermColor { pub const fn color_to_ansi(color: anstyle::Color, palette: palette::Palette) -> anstyle::AnsiColor { match color { anstyle::Color::Ansi(color) => color, - anstyle::Color::XTerm(color) => xterm_to_ansi(color, palette), + anstyle::Color::Ansi256(color) => xterm_to_ansi(color, palette), anstyle::Color::Rgb(color) => rgb_to_ansi(color, palette), } } @@ -34,7 +34,7 @@ pub const fn ansi_to_rgb( } pub const fn xterm_to_rgb( - color: anstyle::XTermColor, + color: anstyle::Ansi256Color, palette: palette::Palette, ) -> anstyle::RgbColor { match palette.rgb_from_index(color.0) { @@ -44,7 +44,7 @@ pub const fn xterm_to_rgb( } pub const fn xterm_to_ansi( - color: anstyle::XTermColor, + color: anstyle::Ansi256Color, palette: palette::Palette, ) -> anstyle::AnsiColor { match color.0 { @@ -78,10 +78,10 @@ pub const fn rgb_to_ansi( palette.find_match(color) } -pub const fn rgb_to_xterm(color: anstyle::RgbColor) -> anstyle::XTermColor { +pub const fn rgb_to_xterm(color: anstyle::RgbColor) -> anstyle::Ansi256Color { // Skip placeholders let index = find_xterm_match(color); - anstyle::XTermColor(index as u8) + anstyle::Ansi256Color(index as u8) } const fn find_xterm_match(color: anstyle::RgbColor) -> usize { diff --git a/crates/anstyle-lossy/src/palette.rs b/crates/anstyle-lossy/src/palette.rs index 4e9b0edc..a41f193a 100644 --- a/crates/anstyle-lossy/src/palette.rs +++ b/crates/anstyle-lossy/src/palette.rs @@ -7,7 +7,7 @@ pub struct Palette([anstyle::RgbColor; 16]); impl Palette { pub(crate) const fn rgb_from_ansi(&self, color: anstyle::AnsiColor) -> anstyle::RgbColor { - let color = anstyle::XTermColor::from_ansi(color); + let color = anstyle::Ansi256Color::from_ansi(color); self.0[color.index() as usize] } @@ -35,7 +35,7 @@ impl Palette { index += 1; } - match anstyle::XTermColor(best_index as u8).into_ansi() { + match anstyle::Ansi256Color(best_index as u8).into_ansi() { Some(color) => color, None => { // Panic diff --git a/crates/anstyle-ls/src/lib.rs b/crates/anstyle-ls/src/lib.rs index 546ced2a..38bd53b6 100644 --- a/crates/anstyle-ls/src/lib.rs +++ b/crates/anstyle-ls/src/lib.rs @@ -90,7 +90,7 @@ pub fn parse(code: &str) -> Option { 36 => fg_color = Some(anstyle::AnsiColor::Cyan.into()), 37 => fg_color = Some(anstyle::AnsiColor::White.into()), 38 => match (parts.pop_front(), parts.pop_front()) { - (Some(5), Some(color)) => fg_color = Some(anstyle::XTermColor(color).into()), + (Some(5), Some(color)) => fg_color = Some(anstyle::Ansi256Color(color).into()), (Some(2), Some(red)) => match (parts.pop_front(), parts.pop_front()) { (Some(green), Some(blue)) => { fg_color = Some(anstyle::RgbColor(red, green, blue).into()) @@ -113,7 +113,7 @@ pub fn parse(code: &str) -> Option { 46 => bg_color = Some(anstyle::AnsiColor::Cyan.into()), 47 => bg_color = Some(anstyle::AnsiColor::White.into()), 48 => match (parts.pop_front(), parts.pop_front()) { - (Some(5), Some(color)) => bg_color = Some(anstyle::XTermColor(color).into()), + (Some(5), Some(color)) => bg_color = Some(anstyle::Ansi256Color(color).into()), (Some(2), Some(red)) => match (parts.pop_front(), parts.pop_front()) { (Some(green), Some(blue)) => { bg_color = Some(anstyle::RgbColor(red, green, blue).into()) @@ -128,7 +128,9 @@ pub fn parse(code: &str) -> Option { }, 49 => bg_color = None, 58 => match (parts.pop_front(), parts.pop_front()) { - (Some(5), Some(color)) => underline_color = Some(anstyle::XTermColor(color).into()), + (Some(5), Some(color)) => { + underline_color = Some(anstyle::Ansi256Color(color).into()) + } (Some(2), Some(red)) => match (parts.pop_front(), parts.pop_front()) { (Some(green), Some(blue)) => { underline_color = Some(anstyle::RgbColor(red, green, blue).into()) @@ -198,7 +200,7 @@ mod tests { ); assert_style( "32;40", - anstyle::AnsiColor::Green | anstyle::AnsiColor::Black, + anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Black), ); } @@ -238,27 +240,27 @@ mod tests { #[test] fn parse_8_bit_colors() { - assert_style("38;5;115", anstyle::XTermColor(115)); - assert_style("00;38;5;115", anstyle::XTermColor(115)); + assert_style("38;5;115", anstyle::Ansi256Color(115)); + assert_style("00;38;5;115", anstyle::Ansi256Color(115)); assert_style( "01;38;5;119", - anstyle::XTermColor(119) | anstyle::Effects::BOLD, + anstyle::Ansi256Color(119) | anstyle::Effects::BOLD, ); assert_style( "38;5;119;01", - anstyle::XTermColor(119) | anstyle::Effects::BOLD, + anstyle::Ansi256Color(119) | anstyle::Effects::BOLD, ); assert_style( "58;5;115", - anstyle::Style::new().underline_color(Some(anstyle::XTermColor(115).into())), + anstyle::Style::new().underline_color(Some(anstyle::Ansi256Color(115).into())), ); assert_style( "00;58;5;115", - anstyle::Style::new().underline_color(Some(anstyle::XTermColor(115).into())), + anstyle::Style::new().underline_color(Some(anstyle::Ansi256Color(115).into())), ); assert_style( "01;58;5;119", - anstyle::Style::new().underline_color(Some(anstyle::XTermColor(119).into())) + anstyle::Style::new().underline_color(Some(anstyle::Ansi256Color(119).into())) | anstyle::Effects::BOLD, ); } @@ -272,12 +274,12 @@ mod tests { ); assert_style( "48;2;100;200;0;1;38;2;0;10;20", - anstyle::RgbColor(0, 10, 20) | anstyle::RgbColor(100, 200, 0) | anstyle::Effects::BOLD, + anstyle::RgbColor(0, 10, 20).on(anstyle::RgbColor(100, 200, 0)) + | anstyle::Effects::BOLD, ); assert_style( "48;2;100;200;0;1;38;2;0;10;20;58;2;64;64;64", - (anstyle::RgbColor(0, 10, 20) - | anstyle::RgbColor(100, 200, 0) + (anstyle::RgbColor(0, 10, 20).on(anstyle::RgbColor(100, 200, 0)) | anstyle::Effects::BOLD) .underline_color(Some(anstyle::RgbColor(64, 64, 64).into())), ); diff --git a/crates/anstyle-owo-colors/src/lib.rs b/crates/anstyle-owo-colors/src/lib.rs index 1619a6e4..b9c95048 100644 --- a/crates/anstyle-owo-colors/src/lib.rs +++ b/crates/anstyle-owo-colors/src/lib.rs @@ -56,7 +56,7 @@ pub fn to_owo_style(style: anstyle::Style) -> owo_colors::Style { pub fn to_owo_colors(color: anstyle::Color) -> owo_colors::DynColors { match color { anstyle::Color::Ansi(ansi) => owo_colors::DynColors::Ansi(ansi_to_owo_colors_color(ansi)), - anstyle::Color::XTerm(xterm) => { + anstyle::Color::Ansi256(xterm) => { owo_colors::DynColors::Xterm(xterm_to_owo_colors_color(xterm)) } anstyle::Color::Rgb(rgb) => { @@ -87,7 +87,7 @@ fn ansi_to_owo_colors_color(color: anstyle::AnsiColor) -> owo_colors::colored::C } } -fn xterm_to_owo_colors_color(color: anstyle::XTermColor) -> owo_colors::XtermColors { +fn xterm_to_owo_colors_color(color: anstyle::Ansi256Color) -> owo_colors::XtermColors { owo_colors::XtermColors::from(color.0) } diff --git a/crates/anstyle-roff/src/lib.rs b/crates/anstyle-roff/src/lib.rs index 686ba217..db830a0d 100644 --- a/crates/anstyle-roff/src/lib.rs +++ b/crates/anstyle-roff/src/lib.rs @@ -3,7 +3,7 @@ //! roff output. mod styled_str; -use anstyle::{AnsiColor, Color, RgbColor, Style, XTermColor}; +use anstyle::{Ansi256Color, AnsiColor, Color, RgbColor, Style}; use anstyle_lossy::palette::Palette; use roff::{bold, italic, Roff}; use styled_str::StyledStr; @@ -114,9 +114,9 @@ fn add_color_to_roff(doc: &mut Roff, control_request: &str, color: &Option { doc.control(control_request, vec![ansi_color_to_roff(c)]); } - Some(Color::XTerm(c)) => { - // Adding Support for XTerm colors, however cansi does not support - // XTerm Colors, so this is not executed. If we switch to a provider + Some(Color::Ansi256(c)) => { + // Adding Support for Ansi256 colors, however cansi does not support + // Ansi256 Colors, so this is not executed. If we switch to a provider // That has Xterm support we will also get it for Roff add_color_to_roff(doc, control_request, &Some(xterm_to_ansi_or_rgb(*c))) } @@ -128,7 +128,7 @@ fn add_color_to_roff(doc: &mut Roff, control_request: &str, color: &Option Color { +fn xterm_to_ansi_or_rgb(color: Ansi256Color) -> Color { match color.into_ansi() { Some(ansi_color) => Color::Ansi(ansi_color), None => Color::Rgb(anstyle_lossy::xterm_to_rgb(color, Palette::default())), diff --git a/crates/anstyle-stream/src/adapter/wincon.rs b/crates/anstyle-stream/src/adapter/wincon.rs index 1911cb55..71edc4cf 100644 --- a/crates/anstyle-stream/src/adapter/wincon.rs +++ b/crates/anstyle-stream/src/adapter/wincon.rs @@ -169,7 +169,7 @@ impl anstyle_parse::Perform for WinconCapture { g = None; } (State::Ansi256, n) => { - let color = anstyle::XTermColor(n as u8); + let color = anstyle::Ansi256Color(n as u8); if is_bg { style = style.bg_color(Some(color.into())); } else { @@ -251,7 +251,10 @@ mod test { fn start() { let input = format!("{} world!", "Hello".green().on_red()); let expected = vec![ - (anstyle::AnsiColor::Green | anstyle::AnsiColor::Red, "Hello"), + ( + anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red), + "Hello", + ), (anstyle::Style::default(), " world!"), ]; verify(&input, expected); @@ -262,7 +265,10 @@ mod test { let input = format!("Hello {}!", "world".green().on_red()); let expected = vec![ (anstyle::Style::default(), "Hello "), - (anstyle::AnsiColor::Green | anstyle::AnsiColor::Red, "world"), + ( + anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red), + "world", + ), (anstyle::Style::default(), "!"), ]; verify(&input, expected); @@ -274,7 +280,7 @@ mod test { let expected = vec![ (anstyle::Style::default(), "Hello "), ( - anstyle::AnsiColor::Green | anstyle::AnsiColor::Red, + anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red), "world!", ), ]; diff --git a/crates/anstyle-stream/src/wincon.rs b/crates/anstyle-stream/src/wincon.rs index 0d96bc27..bff374f3 100644 --- a/crates/anstyle-stream/src/wincon.rs +++ b/crates/anstyle-stream/src/wincon.rs @@ -77,7 +77,7 @@ where fn cap_wincon_color(color: anstyle::Color) -> Option { match color { anstyle::Color::Ansi(c) => Some(c), - anstyle::Color::XTerm(c) => c.into_ansi(), + anstyle::Color::Ansi256(c) => c.into_ansi(), anstyle::Color::Rgb(_) => None, } } diff --git a/crates/anstyle-termcolor/src/lib.rs b/crates/anstyle-termcolor/src/lib.rs index 672a84b9..65568ee5 100644 --- a/crates/anstyle-termcolor/src/lib.rs +++ b/crates/anstyle-termcolor/src/lib.rs @@ -40,7 +40,7 @@ pub fn to_termcolor_spec(style: anstyle::Style) -> termcolor::ColorSpec { pub fn to_termcolor_color(color: anstyle::Color) -> termcolor::Color { match color { anstyle::Color::Ansi(ansi) => ansi_to_termcolor_color(ansi), - anstyle::Color::XTerm(xterm) => xterm_to_termcolor_color(xterm), + anstyle::Color::Ansi256(xterm) => xterm_to_termcolor_color(xterm), anstyle::Color::Rgb(rgb) => rgb_to_termcolor_color(rgb), } } @@ -66,7 +66,7 @@ fn ansi_to_termcolor_color(color: anstyle::AnsiColor) -> termcolor::Color { } } -fn xterm_to_termcolor_color(color: anstyle::XTermColor) -> termcolor::Color { +fn xterm_to_termcolor_color(color: anstyle::Ansi256Color) -> termcolor::Color { termcolor::Color::Ansi256(color.0) } diff --git a/crates/anstyle-wincon/examples/dump.rs b/crates/anstyle-wincon/examples/dump.rs index 6f219886..5ba2e2fd 100644 --- a/crates/anstyle-wincon/examples/dump.rs +++ b/crates/anstyle-wincon/examples/dump.rs @@ -36,7 +36,7 @@ fn main() -> Result<(), lexopt::Error> { } fn style(fixed: u8, layer: Layer, effects: anstyle::Effects) -> anstyle::Style { - let color = anstyle::XTermColor(fixed).into(); + let color = anstyle::Ansi256Color(fixed).into(); (match layer { Layer::Fg => anstyle::Style::new().fg_color(Some(color)), Layer::Bg => anstyle::Style::new().bg_color(Some(color)), @@ -51,12 +51,12 @@ fn print_number( ) -> std::io::Result<()> { let fg = style.get_fg_color().and_then(|c| match c { anstyle::Color::Ansi(c) => Some(c), - anstyle::Color::XTerm(c) => c.into_ansi(), + anstyle::Color::Ansi256(c) => c.into_ansi(), anstyle::Color::Rgb(_) => None, }); let bg = style.get_bg_color().and_then(|c| match c { anstyle::Color::Ansi(c) => Some(c), - anstyle::Color::XTerm(c) => c.into_ansi(), + anstyle::Color::Ansi256(c) => c.into_ansi(), anstyle::Color::Rgb(_) => None, }); diff --git a/crates/anstyle-wincon/examples/set.rs b/crates/anstyle-wincon/examples/set.rs index 6cfe6bc3..b4674cda 100644 --- a/crates/anstyle-wincon/examples/set.rs +++ b/crates/anstyle-wincon/examples/set.rs @@ -23,8 +23,8 @@ fn main() -> Result<(), lexopt::Error> { #[derive(Default)] struct Args { - fg: Option, - bg: Option, + fg: Option, + bg: Option, } impl Args { @@ -39,13 +39,13 @@ impl Args { Long("fg") => { res.fg = Some( args.value()? - .parse_with(|s| s.parse::().map(anstyle::XTermColor))?, + .parse_with(|s| s.parse::().map(anstyle::Ansi256Color))?, ); } Long("bg") => { res.fg = Some( args.value()? - .parse_with(|s| s.parse::().map(anstyle::XTermColor))?, + .parse_with(|s| s.parse::().map(anstyle::Ansi256Color))?, ); } _ => return Err(arg.unexpected()), diff --git a/crates/anstyle-yansi/src/lib.rs b/crates/anstyle-yansi/src/lib.rs index 5244ef40..814c0af5 100644 --- a/crates/anstyle-yansi/src/lib.rs +++ b/crates/anstyle-yansi/src/lib.rs @@ -60,7 +60,7 @@ pub fn to_yansi_color(color: anstyle::Color) -> yansi::Color { fn to_yansi_color_with_bold(color: anstyle::Color) -> (yansi::Color, bool) { match color { anstyle::Color::Ansi(ansi) => ansi_to_yansi_color(ansi), - anstyle::Color::XTerm(xterm) => (xterm_to_yansi_color(xterm), false), + anstyle::Color::Ansi256(xterm) => (xterm_to_yansi_color(xterm), false), anstyle::Color::Rgb(rgb) => (rgb_to_yansi_color(rgb), false), } } @@ -86,7 +86,7 @@ fn ansi_to_yansi_color(color: anstyle::AnsiColor) -> (yansi::Color, bool) { } } -fn xterm_to_yansi_color(color: anstyle::XTermColor) -> yansi::Color { +fn xterm_to_yansi_color(color: anstyle::Ansi256Color) -> yansi::Color { yansi::Color::Fixed(color.0) } diff --git a/crates/anstyle/examples/dump.rs b/crates/anstyle/examples/dump.rs index e5671b77..a37a0489 100644 --- a/crates/anstyle/examples/dump.rs +++ b/crates/anstyle/examples/dump.rs @@ -38,7 +38,7 @@ fn main() -> Result<(), lexopt::Error> { } fn style(fixed: u8, layer: Layer, effects: anstyle::Effects) -> anstyle::Style { - let color = anstyle::XTermColor(fixed).into(); + let color = anstyle::Ansi256Color(fixed).into(); (match layer { Layer::Fg => anstyle::Style::new().fg_color(Some(color)), Layer::Bg => anstyle::Style::new().bg_color(Some(color)), diff --git a/crates/anstyle/src/color.rs b/crates/anstyle/src/color.rs index a7fe836e..6f615485 100644 --- a/crates/anstyle/src/color.rs +++ b/crates/anstyle/src/color.rs @@ -2,11 +2,19 @@ #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] pub enum Color { Ansi(AnsiColor), - XTerm(XTermColor), + Ansi256(Ansi256Color), Rgb(RgbColor), } impl Color { + /// Create a [`Style`][crate::Style] with this as the foregroun + #[inline] + pub fn on(self, background: impl Into) -> crate::Style { + crate::Style::new() + .fg_color(Some(self)) + .bg_color(Some(background.into())) + } + /// Render the ANSI code for a foreground color #[inline] pub fn render_fg(self) -> impl core::fmt::Display { @@ -33,7 +41,7 @@ impl Color { ) -> core::fmt::Result { match self { Self::Ansi(color) => color.ansi_fmt(f, context), - Self::XTerm(color) => color.ansi_fmt(f, context), + Self::Ansi256(color) => color.ansi_fmt(f, context), Self::Rgb(color) => color.ansi_fmt(f, context), } } @@ -53,10 +61,10 @@ impl From for Color { } } -impl From for Color { +impl From for Color { #[inline] - fn from(inner: XTermColor) -> Self { - Self::XTerm(inner) + fn from(inner: Ansi256Color) -> Self { + Self::Ansi256(inner) } } @@ -70,7 +78,7 @@ impl From for Color { impl From for Color { #[inline] fn from(inner: u8) -> Self { - Self::XTerm(inner.into()) + Self::Ansi256(inner.into()) } } @@ -81,26 +89,6 @@ impl From<(u8, u8, u8)> for Color { } } -/// Define style with specified foreground and background colors -/// -/// # Examples -/// -/// ```rust -/// let fg = anstyle::Color::from((0, 0, 0)); -/// let bg = anstyle::Color::from((0xff, 0xff, 0xff)); -/// let style = fg | bg; -/// ``` -impl> core::ops::BitOr for Color { - type Output = crate::Style; - - #[inline(always)] - fn bitor(self, rhs: C) -> Self::Output { - crate::Style::new() - .fg_color(Some(self)) - .bg_color(Some(rhs.into())) - } -} - /// Define style with specified foreground color and effects /// /// # Examples @@ -174,6 +162,14 @@ pub enum AnsiColor { } impl AnsiColor { + /// Create a [`Style`][crate::Style] with this as the foregroun + #[inline] + pub fn on(self, background: impl Into) -> crate::Style { + crate::Style::new() + .fg_color(Some(self.into())) + .bg_color(Some(background.into())) + } + /// Render the ANSI code for a foreground color #[inline] pub fn render_fg(self) -> impl core::fmt::Display { @@ -290,32 +286,12 @@ impl AnsiColorFmt for AnsiColor { (ColorContext::Foreground, true) => write!(f, "9{}", self.suffix()), (ColorContext::Background, false) => write!(f, "4{}", self.suffix()), (ColorContext::Foreground, false) => write!(f, "3{}", self.suffix()), - // No per-color codes; must delegate to `XTermColor` - (ColorContext::Underline, _) => XTermColor::from(*self).ansi_fmt(f, context), + // No per-color codes; must delegate to `Ansi256Color` + (ColorContext::Underline, _) => Ansi256Color::from(*self).ansi_fmt(f, context), } } } -/// Define style with specified foreground and background colors -/// -/// # Examples -/// -/// ```rust -/// let fg = anstyle::AnsiColor::Black; -/// let bg = anstyle::AnsiColor::White; -/// let style = fg | bg; -/// ``` -impl> core::ops::BitOr for AnsiColor { - type Output = crate::Style; - - #[inline(always)] - fn bitor(self, rhs: C) -> Self::Output { - crate::Style::new() - .fg_color(Some(self.into())) - .bg_color(Some(rhs.into())) - } -} - /// Define style with specified foreground color and effects /// /// # Examples @@ -340,9 +316,17 @@ impl core::ops::BitOr for AnsiColor { /// - `232..` map to [`RgbColor`] gray-scale values #[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[repr(transparent)] -pub struct XTermColor(pub u8); +pub struct Ansi256Color(pub u8); + +impl Ansi256Color { + /// Create a [`Style`][crate::Style] with this as the foregroun + #[inline] + pub fn on(self, background: impl Into) -> crate::Style { + crate::Style::new() + .fg_color(Some(self.into())) + .bg_color(Some(background.into())) + } -impl XTermColor { #[inline] pub const fn index(self) -> u8 { self.0 @@ -412,7 +396,7 @@ impl XTermColor { } } -impl AnsiColorFmt for XTermColor { +impl AnsiColorFmt for Ansi256Color { fn ansi_fmt(&self, f: &mut dyn core::fmt::Write, context: ColorContext) -> core::fmt::Result { match context { ColorContext::Background => { @@ -429,49 +413,29 @@ impl AnsiColorFmt for XTermColor { } } -impl From for XTermColor { +impl From for Ansi256Color { #[inline] fn from(inner: u8) -> Self { Self(inner) } } -impl From for XTermColor { +impl From for Ansi256Color { #[inline] fn from(inner: AnsiColor) -> Self { Self::from_ansi(inner) } } -/// Define style with specified foreground and background colors -/// -/// # Examples -/// -/// ```rust -/// let fg = anstyle::XTermColor(16); -/// let bg = anstyle::XTermColor(231); -/// let style = fg | bg; -/// ``` -impl> core::ops::BitOr for XTermColor { - type Output = crate::Style; - - #[inline(always)] - fn bitor(self, rhs: C) -> Self::Output { - crate::Style::new() - .fg_color(Some(self.into())) - .bg_color(Some(rhs.into())) - } -} - /// Define style with specified foreground color and effects /// /// # Examples /// /// ```rust -/// let fg = anstyle::XTermColor(0); +/// let fg = anstyle::Ansi256Color(0); /// let style = fg | anstyle::Effects::BOLD | anstyle::Effects::UNDERLINE; /// ``` -impl core::ops::BitOr for XTermColor { +impl core::ops::BitOr for Ansi256Color { type Output = crate::Style; #[inline(always)] @@ -485,6 +449,14 @@ impl core::ops::BitOr for XTermColor { pub struct RgbColor(pub u8, pub u8, pub u8); impl RgbColor { + /// Create a [`Style`][crate::Style] with this as the foregroun + #[inline] + pub fn on(self, background: impl Into) -> crate::Style { + crate::Style::new() + .fg_color(Some(self.into())) + .bg_color(Some(background.into())) + } + #[inline] pub const fn r(self) -> u8 { self.0 @@ -544,26 +516,6 @@ impl From<(u8, u8, u8)> for RgbColor { } } -/// Define style with specified foreground and background colors -/// -/// # Examples -/// -/// ```rust -/// let fg = anstyle::RgbColor(0, 0, 0); -/// let bg = anstyle::RgbColor(0xff, 0xff, 0xff); -/// let style = fg | bg; -/// ``` -impl> core::ops::BitOr for RgbColor { - type Output = crate::Style; - - #[inline(always)] - fn bitor(self, rhs: C) -> Self::Output { - crate::Style::new() - .fg_color(Some(self.into())) - .bg_color(Some(rhs.into())) - } -} - /// Define style with specified foreground color and effects /// /// # Examples diff --git a/crates/anstyle/src/style.rs b/crates/anstyle/src/style.rs index 9000ea8e..dfd25169 100644 --- a/crates/anstyle/src/style.rs +++ b/crates/anstyle/src/style.rs @@ -283,11 +283,11 @@ impl From for Style { /// # Examples /// /// ```rust -/// let style: anstyle::Style = anstyle::XTermColor(0).into(); +/// let style: anstyle::Style = anstyle::Ansi256Color(0).into(); /// ``` -impl From for Style { +impl From for Style { #[inline] - fn from(color: crate::XTermColor) -> Self { + fn from(color: crate::Ansi256Color) -> Self { Self::new().fg_color(Some(color.into())) } }