From 2cf540ebfdc63508c7012a77c4510d4d9fe7d5a0 Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Mon, 15 Jul 2024 23:08:52 +0100 Subject: [PATCH 1/9] use the rgb crate instead of own struct closes #173 --- Cargo.toml | 1 + src/customcolors.rs | 45 --------------------------------------------- src/lib.rs | 8 ++++---- 3 files changed, 5 insertions(+), 49 deletions(-) delete mode 100644 src/customcolors.rs diff --git a/Cargo.toml b/Cargo.toml index 97f9eb7..5897a73 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,6 +17,7 @@ no-color = [] [dependencies] lazy_static = "1" +rgb = "0.8" [target.'cfg(windows)'.dependencies.windows-sys] version = "0.48" diff --git a/src/customcolors.rs b/src/customcolors.rs deleted file mode 100644 index 049d211..0000000 --- a/src/customcolors.rs +++ /dev/null @@ -1,45 +0,0 @@ -/// Custom color structure, it will generate a true color in the result -#[derive(Debug, Copy, Clone, PartialEq, Eq)] -pub struct CustomColor { - /// Red - pub r: u8, - /// Green - pub g: u8, - /// Blue - pub b: u8, -} - -/// This only makes custom color creation easier. -impl CustomColor { - /// Create a new custom color - pub fn new(r: u8, g: u8, b: u8) -> Self { - Self { r, g, b } - } -} - -impl From<(u8, u8, u8)> for CustomColor { - fn from((r, g, b): (u8, u8, u8)) -> Self { - Self::new(r, g, b) - } -} - -#[cfg(test)] -mod tests { - use crate::*; - #[cfg_attr(feature = "no-color", ignore)] - #[test] - fn main() { - let my_color = CustomColor::new(0, 120, 120); - insta::assert_display_snapshot!("Greetings from Ukraine".custom_color(my_color)); - } - - #[test] - fn from_tuple() { - let tuple = (1u8, 255u8, 0u8); - let cc = CustomColor::from(tuple); - - assert_eq!(cc.r, tuple.0); - assert_eq!(cc.g, tuple.1); - assert_eq!(cc.b, tuple.2); - } -} diff --git a/src/lib.rs b/src/lib.rs index 6942868..f527a62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,10 +40,10 @@ pub mod control; mod error; mod style; -pub use self::customcolors::CustomColor; +pub use rgb::RGB; /// Custom colors support. -pub mod customcolors; +pub use rgb; pub use color::*; @@ -261,7 +261,7 @@ pub trait Colorize { fn custom_color(self, color: T) -> ColoredString where Self: Sized, - T: Into, + T: Into>, { let color = color.into(); @@ -390,7 +390,7 @@ pub trait Colorize { fn on_custom_color(self, color: T) -> ColoredString where Self: Sized, - T: Into, + T: Into>, { let color = color.into(); From 3e37a902db56fed8a89d607bc44c2d59ef00144f Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Tue, 16 Jul 2024 18:50:16 +0100 Subject: [PATCH 2/9] fix test --- examples/custom_colors.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/custom_colors.rs b/examples/custom_colors.rs index 778efdf..f1c01cb 100644 --- a/examples/custom_colors.rs +++ b/examples/custom_colors.rs @@ -1,6 +1,6 @@ use colored::*; fn main() { - let my_color = CustomColor::new(0, 120, 120); + let my_color = RGB::new(0, 120, 120); println!("{}", "Greetings from Ukraine".custom_color(my_color)); println!("{}", "Slava Ukraini!".on_custom_color(my_color)); println!("{}", "Hello World!".on_custom_color((0, 120, 120))); From 096f0e0d04103ec769221c063b7434f9520e3cb0 Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Tue, 16 Jul 2024 19:04:18 +0100 Subject: [PATCH 3/9] change from RGB to the more idiomatic Rgb --- examples/custom_colors.rs | 2 +- src/lib.rs | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/custom_colors.rs b/examples/custom_colors.rs index f1c01cb..edd1fea 100644 --- a/examples/custom_colors.rs +++ b/examples/custom_colors.rs @@ -1,6 +1,6 @@ use colored::*; fn main() { - let my_color = RGB::new(0, 120, 120); + let my_color = Rgb::new(0, 120, 120); println!("{}", "Greetings from Ukraine".custom_color(my_color)); println!("{}", "Slava Ukraini!".on_custom_color(my_color)); println!("{}", "Hello World!".on_custom_color((0, 120, 120))); diff --git a/src/lib.rs b/src/lib.rs index f527a62..e862db7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -40,7 +40,7 @@ pub mod control; mod error; mod style; -pub use rgb::RGB; +pub use rgb::Rgb; /// Custom colors support. pub use rgb; @@ -261,7 +261,7 @@ pub trait Colorize { fn custom_color(self, color: T) -> ColoredString where Self: Sized, - T: Into>, + T: Into>, { let color = color.into(); @@ -390,7 +390,7 @@ pub trait Colorize { fn on_custom_color(self, color: T) -> ColoredString where Self: Sized, - T: Into>, + T: Into>, { let color = color.into(); From f24804aca300ad3821b885777f0af0470318c930 Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Tue, 16 Jul 2024 19:04:30 +0100 Subject: [PATCH 4/9] add some documentation --- src/lib.rs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index e862db7..714593e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -20,6 +20,15 @@ //! format!("{:30}", "format works as expected. This will be padded".blue()); //! format!("{:.3}", "and this will be green but truncated to 3 chars".green()); //! +//! Custom colours are implemented using the `rgb` crate, which is re-exported for +//! convenience. +//! +//! ``` +//! use colored::*; +//! let my_color = Rgb::new(0, 120, 120); +//! println!("{}", "This is using a custom colour".custom_color(my_color)); +//! ``` +//! see `examples/custom_colors.rs` for more info //! //! See [the `Colorize` trait](./trait.Colorize.html) for all the methods. //! @@ -27,6 +36,7 @@ //! [`ColoredString`]'s. See [`ColoredString`] to learn more about them and //! what you can do with them beyond continue to use [`Colorize`] to further //! modify them. +//! #![warn(missing_docs)] #[macro_use] From 65addc59ff580a0687953ac5b9258f069dd81a21 Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Wed, 17 Jul 2024 00:10:26 +0100 Subject: [PATCH 5/9] re-add custom colours struct but deprecated --- src/customcolors.rs | 59 +++++++++++++++++++ src/lib.rs | 5 +- ...d__customcolors__tests__custom_colour.snap | 6 ++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/customcolors.rs create mode 100644 src/snapshots/colored__customcolors__tests__custom_colour.snap diff --git a/src/customcolors.rs b/src/customcolors.rs new file mode 100644 index 0000000..05d412b --- /dev/null +++ b/src/customcolors.rs @@ -0,0 +1,59 @@ +use rgb::Rgb; + +/// Custom color structure, it will generate a true color in the result. +/// You should use the [Rgb] struct instead. +#[derive(Debug, Copy, Clone, PartialEq, Eq)] +#[deprecated] +pub struct CustomColor { + /// Red + pub r: u8, + /// Green + pub g: u8, + /// Blue + pub b: u8, +} + +/// This only makes custom color creation easier. +impl CustomColor { + /// Create a new custom color + pub fn new(r: u8, g: u8, b: u8) -> Self { + Self { r, g, b } + } +} + +impl From for Rgb { + fn from(value: CustomColor) -> Self { + Rgb { + r: value.r, + g: value.g, + b: value.b, + } + } +} + +impl From<(u8, u8, u8)> for CustomColor { + fn from((r, g, b): (u8, u8, u8)) -> Self { + Self::new(r, g, b) + } +} + +#[cfg(test)] +mod tests { + use crate::*; + #[cfg_attr(feature = "no-color", ignore)] + #[test] + fn test_custom_colour() { + let my_color = CustomColor::new(0, 120, 120); + insta::assert_snapshot!("Greetings from Ukraine".custom_color(my_color)); + } + + #[test] + fn from_tuple() { + let tuple = (1u8, 255u8, 0u8); + let cc = CustomColor::from(tuple); + + assert_eq!(cc.r, tuple.0); + assert_eq!(cc.g, tuple.1); + assert_eq!(cc.b, tuple.2); + } +} diff --git a/src/lib.rs b/src/lib.rs index 714593e..9a72bf2 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -50,10 +50,13 @@ pub mod control; mod error; mod style; -pub use rgb::Rgb; + /// Custom colors support. +mod customcolors; +pub use customcolors::CustomColor; pub use rgb; +pub use rgb::Rgb; pub use color::*; diff --git a/src/snapshots/colored__customcolors__tests__custom_colour.snap b/src/snapshots/colored__customcolors__tests__custom_colour.snap new file mode 100644 index 0000000..ad3bc90 --- /dev/null +++ b/src/snapshots/colored__customcolors__tests__custom_colour.snap @@ -0,0 +1,6 @@ +--- +source: src/customcolors.rs +assertion_line: 47 +expression: "\"Greetings from Ukraine\".custom_color(my_color)" +--- +Greetings from Ukraine From 60ffb82fec607ad0369f02417f8f8b88849d0750 Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Sat, 20 Jul 2024 19:33:16 +0100 Subject: [PATCH 6/9] allow deprecated for custom color impls. This just makes sure that clippy doesn't complain about the impls for Custom Color struct --- src/customcolors.rs | 5 +++++ src/lib.rs | 1 + 2 files changed, 6 insertions(+) diff --git a/src/customcolors.rs b/src/customcolors.rs index 05d412b..a4ba05d 100644 --- a/src/customcolors.rs +++ b/src/customcolors.rs @@ -13,6 +13,7 @@ pub struct CustomColor { pub b: u8, } +#[allow(deprecated)] /// This only makes custom color creation easier. impl CustomColor { /// Create a new custom color @@ -21,6 +22,7 @@ impl CustomColor { } } +#[allow(deprecated)] impl From for Rgb { fn from(value: CustomColor) -> Self { Rgb { @@ -31,6 +33,7 @@ impl From for Rgb { } } +#[allow(deprecated)] impl From<(u8, u8, u8)> for CustomColor { fn from((r, g, b): (u8, u8, u8)) -> Self { Self::new(r, g, b) @@ -41,6 +44,7 @@ impl From<(u8, u8, u8)> for CustomColor { mod tests { use crate::*; #[cfg_attr(feature = "no-color", ignore)] + #[allow(deprecated)] #[test] fn test_custom_colour() { let my_color = CustomColor::new(0, 120, 120); @@ -48,6 +52,7 @@ mod tests { } #[test] + #[allow(deprecated)] fn from_tuple() { let tuple = (1u8, 255u8, 0u8); let cc = CustomColor::from(tuple); diff --git a/src/lib.rs b/src/lib.rs index 9a72bf2..401a015 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,7 @@ mod style; /// Custom colors support. mod customcolors; +#[allow(deprecated)] pub use customcolors::CustomColor; pub use rgb; pub use rgb::Rgb; From d40d7294bf58a6869ccb4309704e03b94dd4f7cf Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Sat, 20 Jul 2024 19:33:59 +0100 Subject: [PATCH 7/9] run cargo fmt --- src/lib.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 401a015..9027a4a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -22,7 +22,7 @@ //! //! Custom colours are implemented using the `rgb` crate, which is re-exported for //! convenience. -//! +//! //! ``` //! use colored::*; //! let my_color = Rgb::new(0, 120, 120); @@ -36,7 +36,7 @@ //! [`ColoredString`]'s. See [`ColoredString`] to learn more about them and //! what you can do with them beyond continue to use [`Colorize`] to further //! modify them. -//! +//! #![warn(missing_docs)] #[macro_use] @@ -50,8 +50,6 @@ pub mod control; mod error; mod style; - - /// Custom colors support. mod customcolors; #[allow(deprecated)] From f909c54bdc5ea4fcde075f743b4b2f517cb87ecd Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Sat, 20 Jul 2024 19:40:21 +0100 Subject: [PATCH 8/9] fix major semver errors by making customcolors pub --- src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 9027a4a..4c67720 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -51,7 +51,7 @@ mod error; mod style; /// Custom colors support. -mod customcolors; +pub mod customcolors; #[allow(deprecated)] pub use customcolors::CustomColor; pub use rgb; From 54295dfb9f3bec2a202581a5965d16834b0aed92 Mon Sep 17 00:00:00 2001 From: RuboGubo Date: Sat, 20 Jul 2024 21:03:11 +0100 Subject: [PATCH 9/9] bump version to fix final semver error --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 5897a73..1e0a601 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "colored" description = "The most simple way to add colors in your terminal" -version = "2.1.0" +version = "2.2.0" edition = "2021" authors = ["Thomas Wickham "] license = "MPL-2.0"