Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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 },
Expand Down
237 changes: 56 additions & 181 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Color>,

/// The background color (if any). None means that the text will be printed
/// without a special background.
pub bgcolor: Option<Color>,

/// Any special styling to be applied to the text (see Styles for a list of
/// available options).
pub style: style::Style,
Expand All @@ -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<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<CustomColor>,
{
fn custom_color<T: Into<CustomColor>>(self, color: T) -> ColoredString {
let color = color.into();

self.color(Color::TrueColor {
Expand All @@ -268,134 +210,70 @@ pub trait Colorize {
b: color.b,
})
}
fn ansi_color<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<u8>,
{
fn ansi_color<T: Into<u8>>(self, color: T) -> ColoredString {
self.color(Color::AnsiColor(color.into()))
}
fn color<S: Into<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<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<CustomColor>,
{
fn on_custom_color<T: Into<CustomColor>>(self, color: T) -> ColoredString {
let color = color.into();

self.on_color(Color::TrueColor {
Expand All @@ -404,14 +282,11 @@ pub trait Colorize {
b: color.b,
})
}
fn on_ansi_color<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<u8>,
{
fn on_ansi_color<T: Into<u8>>(self, color: T) -> ColoredString {
self.on_color(Color::AnsiColor(color.into()))
}
fn on_color<S: Into<Color>>(self, color: S) -> ColoredString;
fn on_color<T: Into<Color>>(self, color: T) -> ColoredString;

Copilot AI Dec 15, 2025

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The generic parameter was renamed from S to T, creating inconsistency with the color method on line 216 which still uses S. Both methods should use the same generic parameter name for consistency.

Suggested change
fn on_color<T: Into<Color>>(self, color: T) -> ColoredString;
fn on_color<S: Into<Color>>(self, color: S) -> ColoredString;

Copilot uses AI. Check for mistakes.
Comment thread
BioTomateDE marked this conversation as resolved.
Outdated

// Styles
fn clear(self) -> ColoredString;
fn normal(self) -> ColoredString;
Expand Down Expand Up @@ -510,7 +385,7 @@ impl ColoredString {
}

#[cfg(feature = "no-color")]
fn has_colors() -> bool {
const fn has_colors() -> bool {
false
}

Expand All @@ -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(';');
}

Expand Down
Loading
Loading