Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use the rgb crate instead of own struct #174

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 Cargo.toml
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>"]
license = "MPL-2.0"
Expand All @@ -17,6 +17,7 @@ no-color = []

[dependencies]
lazy_static = "1"
rgb = "0.8"

[target.'cfg(windows)'.dependencies.windows-sys]
version = "0.48"
Expand Down
2 changes: 1 addition & 1 deletion examples/custom_colors.rs
Original file line number Diff line number Diff line change
@@ -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)));
Expand Down
25 changes: 22 additions & 3 deletions src/customcolors.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand All @@ -17,6 +22,18 @@ impl CustomColor {
}
}

#[allow(deprecated)]
impl From<CustomColor> for Rgb<u8> {
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)
Expand All @@ -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);
Expand Down
20 changes: 16 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,23 @@
//! 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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Are we using UK English or American English in the rest of our docs? 😆

Copy link
Author

@RuboGubo RuboGubo Jul 16, 2024

Choose a reason for hiding this comment

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

well if UK English is an option....

//! 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.
//!
//! Note: The methods of [`Colorize`], when used on [`str`]'s, return
//! [`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]
Expand All @@ -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::*;

Expand Down Expand Up @@ -261,7 +273,7 @@ pub trait Colorize {
fn custom_color<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<CustomColor>,
T: Into<Rgb<u8>>,
{
let color = color.into();

Expand Down Expand Up @@ -390,7 +402,7 @@ pub trait Colorize {
fn on_custom_color<T>(self, color: T) -> ColoredString
where
Self: Sized,
T: Into<CustomColor>,
T: Into<Rgb<u8>>,
{
let color = color.into();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
source: src/customcolors.rs
assertion_line: 47
expression: "\"Greetings from Ukraine\".custom_color(my_color)"
---
Greetings from Ukraine
Loading