diff --git a/src/color.rs b/src/color.rs index 5326124..ff4767d 100644 --- a/src/color.rs +++ b/src/color.rs @@ -3,6 +3,7 @@ use Color::{ 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)] #[allow(missing_docs)] @@ -136,7 +137,7 @@ impl Color { } } - fn into_truecolor(self) -> Self { + const fn into_truecolor(self) -> Self { match self { Black => TrueColor { r: 0, g: 0, b: 0 }, Red => TrueColor { r: 205, g: 0, b: 0 }, diff --git a/src/lib.rs b/src/lib.rs index a9b4937..fe8f3e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,11 +124,14 @@ pub use style::{Style, Styles}; pub struct ColoredString { /// The plain text that will have color and style applied to it. pub input: String, + /// The color of the text as it will be printed. pub fgcolor: Option, + /// The background color (if any). None means that the text will be printed /// without a special background. pub bgcolor: Option, + /// Any special styling to be applied to the text (see Styles for a list of /// available options). pub style: style::Style, @@ -139,127 +142,66 @@ pub struct ColoredString { /// You can use `colored` effectively simply by importing this trait /// and then using its methods on `String` and `&str`. #[allow(missing_docs)] -pub trait Colorize { +pub trait Colorize: Sized { // Font Colors - fn black(self) -> ColoredString - where - Self: Sized, - { + fn black(self) -> ColoredString { self.color(Color::Black) } - fn red(self) -> ColoredString - where - Self: Sized, - { + fn red(self) -> ColoredString { self.color(Color::Red) } - fn green(self) -> ColoredString - where - Self: Sized, - { + fn green(self) -> ColoredString { self.color(Color::Green) } - fn yellow(self) -> ColoredString - where - Self: Sized, - { + fn yellow(self) -> ColoredString { self.color(Color::Yellow) } - fn blue(self) -> ColoredString - where - Self: Sized, - { + fn blue(self) -> ColoredString { self.color(Color::Blue) } - fn magenta(self) -> ColoredString - where - Self: Sized, - { + fn magenta(self) -> ColoredString { self.color(Color::Magenta) } - fn purple(self) -> ColoredString - where - Self: Sized, - { + fn purple(self) -> ColoredString { self.color(Color::Magenta) } - fn cyan(self) -> ColoredString - where - Self: Sized, - { + fn cyan(self) -> ColoredString { self.color(Color::Cyan) } - fn white(self) -> ColoredString - where - Self: Sized, - { + fn white(self) -> ColoredString { self.color(Color::White) } - fn bright_black(self) -> ColoredString - where - Self: Sized, - { + fn bright_black(self) -> ColoredString { self.color(Color::BrightBlack) } - fn bright_red(self) -> ColoredString - where - Self: Sized, - { + fn bright_red(self) -> ColoredString { self.color(Color::BrightRed) } - fn bright_green(self) -> ColoredString - where - Self: Sized, - { + fn bright_green(self) -> ColoredString { self.color(Color::BrightGreen) } - fn bright_yellow(self) -> ColoredString - where - Self: Sized, - { + fn bright_yellow(self) -> ColoredString { self.color(Color::BrightYellow) } - fn bright_blue(self) -> ColoredString - where - Self: Sized, - { + fn bright_blue(self) -> ColoredString { self.color(Color::BrightBlue) } - fn bright_magenta(self) -> ColoredString - where - Self: Sized, - { + fn bright_magenta(self) -> ColoredString { self.color(Color::BrightMagenta) } - fn bright_purple(self) -> ColoredString - where - Self: Sized, - { + fn bright_purple(self) -> ColoredString { self.color(Color::BrightMagenta) } - fn bright_cyan(self) -> ColoredString - where - Self: Sized, - { + fn bright_cyan(self) -> ColoredString { self.color(Color::BrightCyan) } - fn bright_white(self) -> ColoredString - where - Self: Sized, - { + fn bright_white(self) -> ColoredString { self.color(Color::BrightWhite) } - fn truecolor(self, r: u8, g: u8, b: u8) -> ColoredString - where - Self: Sized, - { + fn truecolor(self, r: u8, g: u8, b: u8) -> ColoredString { self.color(Color::TrueColor { r, g, b }) } - fn custom_color(self, color: T) -> ColoredString - where - Self: Sized, - T: Into, - { + fn custom_color>(self, color: C) -> ColoredString { let color = color.into(); self.color(Color::TrueColor { @@ -268,134 +210,70 @@ pub trait Colorize { b: color.b, }) } - fn ansi_color(self, color: T) -> ColoredString - where - Self: Sized, - T: Into, - { + fn ansi_color>(self, color: T) -> ColoredString { self.color(Color::AnsiColor(color.into())) } - fn color>(self, color: S) -> ColoredString; + fn color>(self, color: C) -> ColoredString; + // Background Colors - fn on_black(self) -> ColoredString - where - Self: Sized, - { + fn on_black(self) -> ColoredString { self.on_color(Color::Black) } - fn on_red(self) -> ColoredString - where - Self: Sized, - { + fn on_red(self) -> ColoredString { self.on_color(Color::Red) } - fn on_green(self) -> ColoredString - where - Self: Sized, - { + fn on_green(self) -> ColoredString { self.on_color(Color::Green) } - fn on_yellow(self) -> ColoredString - where - Self: Sized, - { + fn on_yellow(self) -> ColoredString { self.on_color(Color::Yellow) } - fn on_blue(self) -> ColoredString - where - Self: Sized, - { + fn on_blue(self) -> ColoredString { self.on_color(Color::Blue) } - fn on_magenta(self) -> ColoredString - where - Self: Sized, - { + fn on_magenta(self) -> ColoredString { self.on_color(Color::Magenta) } - fn on_purple(self) -> ColoredString - where - Self: Sized, - { + fn on_purple(self) -> ColoredString { self.on_color(Color::Magenta) } - fn on_cyan(self) -> ColoredString - where - Self: Sized, - { + fn on_cyan(self) -> ColoredString { self.on_color(Color::Cyan) } - fn on_white(self) -> ColoredString - where - Self: Sized, - { + fn on_white(self) -> ColoredString { self.on_color(Color::White) } - fn on_bright_black(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_black(self) -> ColoredString { self.on_color(Color::BrightBlack) } - fn on_bright_red(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_red(self) -> ColoredString { self.on_color(Color::BrightRed) } - fn on_bright_green(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_green(self) -> ColoredString { self.on_color(Color::BrightGreen) } - fn on_bright_yellow(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_yellow(self) -> ColoredString { self.on_color(Color::BrightYellow) } - fn on_bright_blue(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_blue(self) -> ColoredString { self.on_color(Color::BrightBlue) } - fn on_bright_magenta(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_magenta(self) -> ColoredString { self.on_color(Color::BrightMagenta) } - fn on_bright_purple(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_purple(self) -> ColoredString { self.on_color(Color::BrightMagenta) } - fn on_bright_cyan(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_cyan(self) -> ColoredString { self.on_color(Color::BrightCyan) } - fn on_bright_white(self) -> ColoredString - where - Self: Sized, - { + fn on_bright_white(self) -> ColoredString { self.on_color(Color::BrightWhite) } - fn on_truecolor(self, r: u8, g: u8, b: u8) -> ColoredString - where - Self: Sized, - { + fn on_truecolor(self, r: u8, g: u8, b: u8) -> ColoredString { self.on_color(Color::TrueColor { r, g, b }) } - fn on_custom_color(self, color: T) -> ColoredString - where - Self: Sized, - T: Into, - { + fn on_custom_color>(self, color: C) -> ColoredString { let color = color.into(); self.on_color(Color::TrueColor { @@ -404,14 +282,11 @@ pub trait Colorize { b: color.b, }) } - fn on_ansi_color(self, color: T) -> ColoredString - where - Self: Sized, - T: Into, - { + fn on_ansi_color>(self, color: T) -> ColoredString { self.on_color(Color::AnsiColor(color.into())) } - fn on_color>(self, color: S) -> ColoredString; + fn on_color>(self, color: C) -> ColoredString; + // Styles fn clear(self) -> ColoredString; fn normal(self) -> ColoredString; @@ -510,7 +385,7 @@ impl ColoredString { } #[cfg(feature = "no-color")] - fn has_colors() -> bool { + const fn has_colors() -> bool { false } @@ -520,24 +395,24 @@ impl ColoredString { } let mut res = String::from("\x1B["); - let mut has_wrote = if self.style == style::CLEAR { + let mut has_written = if self.style == style::CLEAR { false } else { res.push_str(&self.style.to_str()); true }; - if let Some(ref bgcolor) = self.bgcolor { - if has_wrote { + if let Some(bgcolor) = &self.bgcolor { + if has_written { res.push(';'); } res.push_str(&bgcolor.to_bg_str()); - has_wrote = true; + has_written = true; } - if let Some(ref fgcolor) = self.fgcolor { - if has_wrote { + if let Some(fgcolor) = &self.fgcolor { + if has_written { res.push(';'); } diff --git a/src/style.rs b/src/style.rs index 557dce0..9ab2b94 100644 --- a/src/style.rs +++ b/src/style.rs @@ -207,7 +207,7 @@ pub enum Styles { } impl Styles { - fn to_str<'a>(self) -> &'a str { + const fn to_str<'a>(self) -> &'a str { match self { Self::Clear => "", // unreachable, but we don't want to panic Self::Bold => "1", @@ -221,7 +221,7 @@ impl Styles { } } - fn to_u8(self) -> u8 { + const fn to_u8(self) -> u8 { match self { Self::Clear => CLEARV, Self::Bold => BOLD, @@ -242,9 +242,10 @@ impl Styles { let res: Vec = STYLES .iter() - .filter(|&(mask, _)| (0 != (u & mask))) + .filter(|&(mask, _)| 0 != (u & mask)) .map(|&(_, value)| value) .collect(); + if res.is_empty() { None } else {