Skip to content
Merged
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
4 changes: 2 additions & 2 deletions crates/anstyle-ansi-term/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand All @@ -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)
}

Expand Down
4 changes: 2 additions & 2 deletions crates/anstyle-crossterm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand All @@ -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)
}

Expand Down
46 changes: 23 additions & 23 deletions crates/anstyle-git/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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]
Expand Down Expand Up @@ -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));
}
}
18 changes: 9 additions & 9 deletions crates/anstyle-lossy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,23 @@ 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),
}
}

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),
}
}
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions crates/anstyle-lossy/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down Expand Up @@ -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
Expand Down
30 changes: 16 additions & 14 deletions crates/anstyle-ls/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn parse(code: &str) -> Option<anstyle::Style> {
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())
Expand All @@ -113,7 +113,7 @@ pub fn parse(code: &str) -> Option<anstyle::Style> {
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())
Expand All @@ -128,7 +128,9 @@ pub fn parse(code: &str) -> Option<anstyle::Style> {
},
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())
Expand Down Expand Up @@ -198,7 +200,7 @@ mod tests {
);
assert_style(
"32;40",
anstyle::AnsiColor::Green | anstyle::AnsiColor::Black,
anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Black),
);
}

Expand Down Expand Up @@ -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,
);
}
Expand All @@ -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())),
);
Expand Down
4 changes: 2 additions & 2 deletions crates/anstyle-owo-colors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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)
}

Expand Down
10 changes: 5 additions & 5 deletions crates/anstyle-roff/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -114,9 +114,9 @@ fn add_color_to_roff(doc: &mut Roff, control_request: &str, color: &Option<Color
Some(Color::Ansi(c)) => {
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)))
}
Expand All @@ -128,7 +128,7 @@ fn add_color_to_roff(doc: &mut Roff, control_request: &str, color: &Option<Color
}

/// Non Lossy Conversion of Xterm color to one that Roff can handle
fn xterm_to_ansi_or_rgb(color: XTermColor) -> 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())),
Expand Down
14 changes: 10 additions & 4 deletions crates/anstyle-stream/src/adapter/wincon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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!",
),
];
Expand Down
2 changes: 1 addition & 1 deletion crates/anstyle-stream/src/wincon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ where
fn cap_wincon_color(color: anstyle::Color) -> Option<anstyle::AnsiColor> {
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,
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/anstyle-termcolor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
}
}
Expand All @@ -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)
}

Expand Down
Loading