diff --git a/Cargo.toml b/Cargo.toml index 97f9eb7..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" @@ -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/examples/custom_colors.rs b/examples/custom_colors.rs index 778efdf..edd1fea 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))); diff --git a/src/customcolors.rs b/src/customcolors.rs index 049d211..a4ba05d 100644 --- a/src/customcolors.rs +++ b/src/customcolors.rs @@ -1,5 +1,9 @@ -/// Custom color structure, it will generate a true color in the result +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, @@ -9,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 @@ -17,6 +22,18 @@ impl CustomColor { } } +#[allow(deprecated)] +impl From for Rgb { + fn from(value: CustomColor) -> Self { + Rgb { + r: value.r, + g: value.g, + b: value.b, + } + } +} + +#[allow(deprecated)] impl From<(u8, u8, u8)> for CustomColor { fn from((r, g, b): (u8, u8, u8)) -> Self { Self::new(r, g, b) @@ -27,13 +44,15 @@ impl From<(u8, u8, u8)> for CustomColor { mod tests { use crate::*; #[cfg_attr(feature = "no-color", ignore)] + #[allow(deprecated)] #[test] - fn main() { + fn test_custom_colour() { let my_color = CustomColor::new(0, 120, 120); - insta::assert_display_snapshot!("Greetings from Ukraine".custom_color(my_color)); + insta::assert_snapshot!("Greetings from Ukraine".custom_color(my_color)); } #[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 6942868..4c67720 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] @@ -40,10 +50,12 @@ pub mod control; mod error; mod style; -pub use self::customcolors::CustomColor; - /// Custom colors support. pub mod customcolors; +#[allow(deprecated)] +pub use customcolors::CustomColor; +pub use rgb; +pub use rgb::Rgb; pub use color::*; @@ -261,7 +273,7 @@ pub trait Colorize { fn custom_color(self, color: T) -> ColoredString where Self: Sized, - T: Into, + T: Into>, { let color = color.into(); @@ -390,7 +402,7 @@ pub trait Colorize { fn on_custom_color(self, color: T) -> ColoredString where Self: Sized, - T: Into, + T: Into>, { let color = color.into(); 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