From 6d747629bf889ad339d501c398728309adc4c25f Mon Sep 17 00:00:00 2001 From: BioTomateDE Date: Mon, 15 Dec 2025 19:15:16 +0000 Subject: [PATCH 1/5] Move constraint from method to trait --- src/color.rs | 1 + src/lib.rs | 223 +++++++++++---------------------------------------- 2 files changed, 50 insertions(+), 174 deletions(-) diff --git a/src/color.rs b/src/color.rs index 5326124..8f57830 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)] diff --git a/src/lib.rs b/src/lib.rs index a9b4937..603d84f 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: T) -> 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; + // 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: T) -> 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: T) -> ColoredString; + // Styles fn clear(self) -> ColoredString; fn normal(self) -> ColoredString; From 23bb3f567569a52d9e60f6a8fa52ba41e25844da Mon Sep 17 00:00:00 2001 From: BioTomateDE Date: Mon, 15 Dec 2025 19:20:49 +0000 Subject: [PATCH 2/5] Applied lints --- src/color.rs | 2 +- src/lib.rs | 2 +- src/style.rs | 7 ++++--- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/color.rs b/src/color.rs index 8f57830..ff4767d 100644 --- a/src/color.rs +++ b/src/color.rs @@ -137,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 603d84f..7d868b1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -385,7 +385,7 @@ impl ColoredString { } #[cfg(feature = "no-color")] - fn has_colors() -> bool { + const fn has_colors() -> bool { false } 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 { From e9fd51a3cbb7dfa47cd1292fae47b4c3c1ef2e74 Mon Sep 17 00:00:00 2001 From: BioTomateDE Date: Mon, 15 Dec 2025 20:31:31 +0000 Subject: [PATCH 3/5] Fixed grammar mistake in variable name --- src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 7d868b1..ba9671a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -395,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(';'); } From fd1ec4888f40f504e716b291579f4e8cdd857961 Mon Sep 17 00:00:00 2001 From: BioTomateDE Date: Tue, 16 Dec 2025 19:14:50 +0100 Subject: [PATCH 4/5] Update lib.rs: type generics "C" --- src/lib.rs | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ba9671a..4cd781c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -201,7 +201,7 @@ pub trait Colorize: 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 { + fn custom_color>(self, color: C) -> ColoredString { let color = color.into(); self.color(Color::TrueColor { @@ -210,10 +210,10 @@ pub trait Colorize: Sized { b: color.b, }) } - fn ansi_color>(self, color: T) -> ColoredString { + fn ansi_color>(self, color: C) -> 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 { @@ -273,7 +273,7 @@ pub trait Colorize: 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 { + fn on_custom_color>(self, color: C) -> ColoredString { let color = color.into(); self.on_color(Color::TrueColor { @@ -282,10 +282,10 @@ pub trait Colorize: Sized { b: color.b, }) } - fn on_ansi_color>(self, color: T) -> ColoredString { + fn on_ansi_color>(self, color: C) -> ColoredString { self.on_color(Color::AnsiColor(color.into())) } - fn on_color>(self, color: T) -> ColoredString; + fn on_color>(self, color: C) -> ColoredString; // Styles fn clear(self) -> ColoredString; From 19e9d43e111ce3ba0f0a1bf3d89dead13cab46b4 Mon Sep 17 00:00:00 2001 From: Spenser Black Date: Tue, 16 Dec 2025 14:10:10 -0500 Subject: [PATCH 5/5] Change `Into` generics back to `T` --- src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 4cd781c..fe8f3e3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -210,7 +210,7 @@ pub trait Colorize: Sized { b: color.b, }) } - fn ansi_color>(self, color: C) -> ColoredString { + fn ansi_color>(self, color: T) -> ColoredString { self.color(Color::AnsiColor(color.into())) } fn color>(self, color: C) -> ColoredString; @@ -282,7 +282,7 @@ pub trait Colorize: Sized { b: color.b, }) } - fn on_ansi_color>(self, color: C) -> ColoredString { + fn on_ansi_color>(self, color: T) -> ColoredString { self.on_color(Color::AnsiColor(color.into())) } fn on_color>(self, color: C) -> ColoredString;