diff --git a/Cargo.lock b/Cargo.lock index 496b64a..91a5e43 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,13 @@ # It is not intended for manual editing. version = 3 +[[package]] +name = "example" +version = "0.1.0" +dependencies = [ + "term_tools", +] + [[package]] name = "term_tools" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 3b92ea3..fcd17d6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,3 +1,6 @@ +[workspace] +members = [".", "example"] + [package] name = "term_tools" version = "0.1.0" @@ -7,7 +10,7 @@ authors = ["Siavash Siamhb7@protonmail.com"] homepage = "https://crates.io/crates/term_tools" repository = "https://github.com/Syaw0/term_tools" license-file = "./LICENSE" -keywords = ["cli","terminal","rust","tool","coloize"] -categories = ["command-line-interface","command-line-utilities"] +keywords = ["cli", "terminal", "rust", "tool", "coloize"] +categories = ["command-line-interface", "command-line-utilities"] [dependencies] diff --git a/example/Cargo.toml b/example/Cargo.toml new file mode 100644 index 0000000..3f87fe3 --- /dev/null +++ b/example/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "example" +version = "0.1.0" +edition = "2021" + +[dependencies] +term_tools = { path = ".." } diff --git a/example/src/main.rs b/example/src/main.rs new file mode 100644 index 0000000..c03c208 --- /dev/null +++ b/example/src/main.rs @@ -0,0 +1,23 @@ +use term_tools::prelude::*; + +fn main() { + println!( + "{}{}{}", + "In the name of ", + "GOD".styled().bold().cyan(), + "." + ); + + let mut styled = "Hello World!".styled(); + styled + .color(colors::RED) + .format(formats::BOLD) + .underline() + .fg() + .white() + .bg(); + + println!("{}", styled); + + println!("{}", "Walking dead!".styled().italic().red()) +} diff --git a/src/ansi_code.rs b/src/ansi_code.rs index 27fdef4..460eb5a 100644 --- a/src/ansi_code.rs +++ b/src/ansi_code.rs @@ -1,9 +1,9 @@ -/// A module for creating ansi escape code. -/// -/// This module provides an struct `ANSIEscapeCode` -/// that represents ansi escape code, with `parameter` field. -/// It also implements the `ANSIEscapeCode`, with `new` and `code` method -/// which allows for generating anis escape code and getting a code. +//! A module for creating ansi escape code. +//! +//! This module provides an struct `ANSIEscapeCode` +//! that represents ansi escape code, with `parameter` field. +//! It also implements the `ANSIEscapeCode`, with `new` and `code` method +//! which allows for generating anis escape code and getting a code. // ======================================================================= @@ -13,10 +13,13 @@ pub struct ANSIEscapeCode { parameter: String, } + impl ANSIEscapeCode { /// Returns a ANSIEscapeCode instance with parameter . pub fn new(parameter: &str) -> Self { - ANSIEscapeCode { parameter: parameter.to_string() } + ANSIEscapeCode { + parameter: parameter.to_string(), + } } /// Returns a String that represent the ansi code. diff --git a/src/helpers.rs b/src/helpers.rs new file mode 100644 index 0000000..b95bb8f --- /dev/null +++ b/src/helpers.rs @@ -0,0 +1,98 @@ +//! +//! + +// ======================================================================= + +use crate::{styles::Styles, StyledText}; + +// ======================================================================= + +/// Converts everything that can convert into [`String`], to an [`StyledText`]. +pub trait IntoStyled { + /// Creates a styled text string with the given text and styles. + /// + /// This function returns a `StyledText` object that can be customized with various styles, + /// colors, and effects. The resulting styled text can be printed or used in other contexts. + /// + /// # Examples + /// + /// ``` + /// use term_tools::styled; + /// + /// let txt = "Happy Day!" + /// .styled() + /// .rgb(204, 182, 122) + /// .italic() + /// .rapid_blink() + /// .bg() + /// .black() + /// .fg() + /// .paint(); + /// + /// println!("{txt}"); + /// ``` + /// + /// # Styles + /// + /// The following styles can be applied to the text: + /// + /// * `rgb(r, g, b)`: Sets the text color to the given RGB values. + /// * `italic()`: Sets the text to italic. + /// * `rapid_blink()`: Sets the text to rapidly blink. + /// * `bg()`: Sets the background color. + /// * `fg()`: Sets the foreground color. + /// * basic colors like : `red`,`black` and etc. + /// + /// # Colors + /// + /// The following colors can be used with the `rgb` method: + /// + /// * `rgb(r, g, b)`: Sets the color to the given RGB values. + /// + /// # Effects + /// + /// The following effects can be applied to the text: + /// + /// * `rapid_blink()`: Sets the text to rapidly blink. + /// + /// # Returns + /// + /// A `StyledText` object that can be printed or used in other contexts. + fn styled(self) -> StyledText; +} + +impl> IntoStyled for T { + fn styled(self) -> StyledText { + StyledText::new(self.into()) + } +} + +pub trait IntoStyle { + fn into_style(self) -> Styles; +} + +impl> IntoStyle for T { + fn into_style(self) -> Styles { + self.into() + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_into_styles() { + let txt = "Hi Father" + .styled() + .rgb(204, 182, 122) + .italic() + .rapid_blink() + .bg() + .black() + .fg() + .paint(); + + println!("{txt}"); + } +} diff --git a/src/lib.rs b/src/lib.rs index d5914c5..7ff3484 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,18 +4,21 @@ //! It allows you to create styled text strings with various colors, effects, and formatters. mod ansi_code; +mod helpers; +pub mod prelude; mod styles; // ======================================================================= + +use std::fmt::Display; + +use helpers::{IntoStyle, IntoStyled}; +use styles::{basic_color::BasicColor, formatter::Formatter}; + use crate::{ ansi_code::ANSIEscapeCode, styles::{ - basic_color, - formatter, - paint_type::PaintType, - palette::PaletteColor, - rgb::Rgb, - Styles, + basic_color, formatter, paint_type::PaintType, palette::PaletteColor, rgb::Rgb, Styles, }, }; @@ -68,8 +71,8 @@ use crate::{ /// # Returns /// /// A `StyledText` object that can be printed or used in other contexts. -pub fn styled(text: &str) -> StyledText { - StyledText::new(text.to_string()) +pub fn styled(text: S) -> StyledText { + text.styled() } /// A struct representing a styled text string. @@ -78,6 +81,12 @@ pub struct StyledText { start_styles: Vec, } +impl Display for StyledText { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + self._fmt(f) + } +} + impl StyledText { /// Creates a new `StyledText` object with the given text. fn new(text: String) -> Self { @@ -87,17 +96,15 @@ impl StyledText { } } - /// Paints the styled text string with the given styles. - /// - /// This method returns a string representing the styled text. - pub fn paint(&mut self) -> String { + fn _fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let mut default_paint_type = PaintType::FG; - let start_codes_list: Vec = self.start_styles + let start_codes_list: Vec = self + .start_styles .iter() .rev() .filter_map(|s| { - if let Styles::StylePaintType(p) = s { + if let Styles::PaintType(p) = s { default_paint_type = p.clone(); return None; } @@ -110,10 +117,24 @@ impl StyledText { .collect(); let start_codes = start_codes_list.join(""); let end_codes = ANSIEscapeCode::new( - &formatter::RESET.make_styles(Some(&default_paint_type)) - ).code(); + &Styles::Formatter(formatter::RESET).make_styles(Some(&default_paint_type)), + ) + .code(); - format!("{}{}{}", start_codes, self.text, end_codes) + write!(f, "{}{}{}", start_codes, self.text, end_codes) + } + + /// Paints the styled text string with the given styles. + /// + /// This method returns a string representing the styled text. + pub fn paint(&self) -> String { + self.to_string() + } + + /// Pushes an style. + pub fn push(&mut self, style: S) -> &mut Self { + self.start_styles.push(style.into_style()); + self } /// Sets the foreground color of the colors you have called. @@ -128,8 +149,7 @@ impl StyledText { /// as background color /// **if the one `fg` call, all the colors will paint as foreground no matter there is before or after `fg`** pub fn fg(&mut self) -> &mut Self { - self.start_styles.push(Styles::StylePaintType(PaintType::FG)); - self + self.push(PaintType::FG) } /// Sets the background color of the colors you have called. @@ -144,8 +164,7 @@ impl StyledText { /// as foreground color /// **if the one `bg` call, all the colors will paint as background no matter there is before or after `bg`** pub fn bg(&mut self) -> &mut Self { - self.start_styles.push(Styles::StylePaintType(PaintType::BG)); - self + self.push(PaintType::BG) } // Colors @@ -158,8 +177,7 @@ impl StyledText { /// let styled_text = styled("Our life is what our thoughts make it.").rgb(48,118,230).paint(); /// ``` pub fn rgb(&mut self, r: u8, g: u8, b: u8) -> &mut Self { - self.start_styles.push(Styles::StyleRgb(Rgb { r, g, b })); - self + self.push(Rgb { r, g, b }) } /// Sets the `palette` color to the input text. @@ -172,8 +190,12 @@ impl StyledText { /// /// the index should be 8 bit color between 0 to 255. pub fn palette(&mut self, index: u8) -> &mut Self { - self.start_styles.push(Styles::StylePaletteColor(PaletteColor { index })); - self + self.push(PaletteColor { index }) + } + + /// Sets the given color to the input text. + pub fn color>(&mut self, color: C) -> &mut Self { + self.push(color.into()) } /// Sets the `black` color to the input text. @@ -184,8 +206,7 @@ impl StyledText { /// let styled_text = styled("The best revenge is to not be like your enemies.").black().paint(); /// ``` pub fn black(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BLACK); - self + self.color(basic_color::BLACK) } /// Sets the `red` color to the input text. @@ -196,8 +217,7 @@ impl StyledText { /// let styled_text = styled("To love only what happens, what was destined. No greater harmony.").red().paint(); /// ``` pub fn red(&mut self) -> &mut Self { - self.start_styles.push(basic_color::RED); - self + self.color(basic_color::RED) } /// Sets the `green` color to the input text. @@ -208,8 +228,7 @@ impl StyledText { /// let styled_text = styled("Everything we hear is opinion, not a fact. Everything we see is a perspective, not the truth.").green().paint(); /// ``` pub fn green(&mut self) -> &mut Self { - self.start_styles.push(basic_color::GREEN); - self + self.color(basic_color::GREEN) } /// Sets the `yellow` color to the input text. @@ -220,8 +239,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").yellow().paint(); /// ``` pub fn yellow(&mut self) -> &mut Self { - self.start_styles.push(basic_color::YELLOW); - self + self.color(basic_color::YELLOW) } /// Sets the `blue` color to the input text. @@ -232,8 +250,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").blue().paint(); /// ``` pub fn blue(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BLUE); - self + self.color(basic_color::BLUE) } /// Sets the `magenta` color to the input text. @@ -244,8 +261,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").magenta().paint(); /// ``` pub fn magenta(&mut self) -> &mut Self { - self.start_styles.push(basic_color::MAGENTA); - self + self.color(basic_color::MAGENTA) } /// Sets the `cyan` color to the input text. @@ -256,8 +272,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").cyan().paint(); /// ``` pub fn cyan(&mut self) -> &mut Self { - self.start_styles.push(basic_color::CYAN); - self + self.color(basic_color::CYAN) } /// Sets the `white` color to the input text. @@ -268,8 +283,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").white().paint(); /// ``` pub fn white(&mut self) -> &mut Self { - self.start_styles.push(basic_color::WHITE); - self + self.color(basic_color::WHITE) } /// Sets the `gray` color to the input text. @@ -280,8 +294,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").gray().paint(); /// ``` pub fn gray(&mut self) -> &mut Self { - self.start_styles.push(basic_color::GRAY); - self + self.color(basic_color::GRAY) } /// Sets the `bright_red` color to the input text. @@ -292,8 +305,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").bright_red().paint(); /// ``` pub fn bright_red(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BRIGHT_RED); - self + self.color(basic_color::BRIGHT_RED) } /// Sets the `bright_green` color to the input text. @@ -304,8 +316,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").bright_green().paint(); /// ``` pub fn bright_green(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BRIGHT_GREEN); - self + self.color(basic_color::BRIGHT_GREEN) } /// Sets the `bright_yellow` color to the input text. @@ -316,8 +327,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").bright_yellow().paint(); /// ``` pub fn bright_yellow(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BRIGHT_YELLOW); - self + self.color(basic_color::BRIGHT_YELLOW) } /// Sets the `bright_blue` color to the input text. @@ -328,8 +338,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").bright_blue().paint(); /// ``` pub fn bright_blue(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BRIGHT_BLUE); - self + self.color(basic_color::BRIGHT_BLUE) } /// Sets the `bright_magenta` color to the input text. @@ -341,8 +350,7 @@ impl StyledText { /// ``` /// use term_tools::styled; pub fn bright_magenta(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BRIGHT_MAGENTA); - self + self.color(basic_color::BRIGHT_MAGENTA) } /// Sets the `bright_cyan` color to the input text. @@ -353,8 +361,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").bright_cyan().paint(); /// ``` pub fn bright_cyan(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BRIGHT_CYAN); - self + self.color(basic_color::BRIGHT_CYAN) } /// Sets the `bright_white` color to the input text. @@ -365,12 +372,16 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").bright_white().paint(); /// ``` pub fn bright_white(&mut self) -> &mut Self { - self.start_styles.push(basic_color::BRIGHT_WHITE); - self + self.color(basic_color::BRIGHT_WHITE) } // Formatters + /// Sets the given format to the input text. + pub fn format>(&mut self, format: F) -> &mut Self { + self.push(format.into()) + } + /// Sets the `reset` effect to the input text. /// /// # Example: @@ -381,8 +392,7 @@ impl StyledText { /// ** this will reset all the effects, colors and formatters that are called before this** /// so in the top example the red color will never applied to the input text pub fn reset(&mut self) -> &mut Self { - self.start_styles.push(formatter::RESET); - self + self.format(formatter::RESET) } /// Sets the `bold` format to the input text. @@ -393,8 +403,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").bold().paint(); /// ``` pub fn bold(&mut self) -> &mut Self { - self.start_styles.push(formatter::BOLD); - self + self.format(formatter::BOLD) } /// Sets the `faint` format to the input text. @@ -405,8 +414,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").faint().paint(); /// ``` pub fn faint(&mut self) -> &mut Self { - self.start_styles.push(formatter::FAINT); - self + self.format(formatter::FAINT) } /// Sets the `italic` format to the input text. @@ -417,8 +425,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").italic().paint(); /// ``` pub fn italic(&mut self) -> &mut Self { - self.start_styles.push(formatter::ITALIC); - self + self.format(formatter::ITALIC) } /// Sets the `underline` format to the input text. @@ -429,8 +436,7 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").underline().paint(); /// ``` pub fn underline(&mut self) -> &mut Self { - self.start_styles.push(formatter::UNDERLINE); - self + self.format(formatter::UNDERLINE) } /// Sets the `slow_blink` effect to the input text. @@ -443,8 +449,7 @@ impl StyledText { /// /// **base on the terminal you are using this could not be applied** pub fn slow_blink(&mut self) -> &mut Self { - self.start_styles.push(formatter::SLOW_BLINK); - self + self.format(formatter::SLOW_BLINK) } /// Sets the `rapid_blink` effect to the input text. @@ -457,8 +462,7 @@ impl StyledText { /// /// **base on the terminal you are using this could not be applied** pub fn rapid_blink(&mut self) -> &mut Self { - self.start_styles.push(formatter::RAPID_BLINK); - self + self.format(formatter::RAPID_BLINK) } /// Sets the `overline` effect to the input text. @@ -469,7 +473,6 @@ impl StyledText { /// let styled_text = styled("The present is all we have to live in . . . or to lose.").overline().paint(); /// ``` pub fn overline(&mut self) -> &mut Self { - self.start_styles.push(formatter::OVERLINE); - self + self.format(formatter::OVERLINE) } } diff --git a/src/prelude.rs b/src/prelude.rs new file mode 100644 index 0000000..e916fea --- /dev/null +++ b/src/prelude.rs @@ -0,0 +1,37 @@ +pub use crate::helpers::{IntoStyle, IntoStyled}; +pub use crate::styles::basic_color::BasicColor; +pub use crate::styles::formatter::Formatter; + +pub mod colors { + use crate::styles::basic_color; + + pub use basic_color::BLACK; + pub use basic_color::BLUE; + pub use basic_color::BRIGHT_BLUE; + pub use basic_color::BRIGHT_CYAN; + pub use basic_color::BRIGHT_GREEN; + pub use basic_color::BRIGHT_MAGENTA; + pub use basic_color::BRIGHT_RED; + pub use basic_color::BRIGHT_WHITE; + pub use basic_color::BRIGHT_YELLOW; + pub use basic_color::CYAN; + pub use basic_color::GRAY; + pub use basic_color::GREEN; + pub use basic_color::MAGENTA; + pub use basic_color::RED; + pub use basic_color::WHITE; + pub use basic_color::YELLOW; +} + +pub mod formats { + use crate::styles::formatter; + + pub use formatter::BOLD; + pub use formatter::FAINT; + pub use formatter::ITALIC; + pub use formatter::OVERLINE; + pub use formatter::RAPID_BLINK; + pub use formatter::RESET; + pub use formatter::SLOW_BLINK; + pub use formatter::UNDERLINE; +} diff --git a/src/styles/basic_color.rs b/src/styles/basic_color.rs index 7e7fc34..ae5442a 100644 --- a/src/styles/basic_color.rs +++ b/src/styles/basic_color.rs @@ -1,4 +1,9 @@ -use super::{ paint_type::PaintType, Styles, Stylify }; +//! +//! + +// ======================================================================= + +use super::{paint_type::PaintType, Stylify}; // ======================================================================= @@ -17,17 +22,20 @@ impl Stylify for BasicColor { /// If `paint_type` is `None`, the foreground color is assumed. fn make_styles(&self, paint_type: Option<&PaintType>) -> String { let paint_type = paint_type.unwrap_or(&PaintType::FG); - format!("{}", match paint_type { - PaintType::FG => self.fg, - PaintType::BG => self.bg, - }) + format!( + "{}", + match paint_type { + PaintType::FG => self.fg, + PaintType::BG => self.bg, + } + ) } } /// A macro for generating color constants. macro_rules! color_code { ($name:ident, { fg: $fg:expr, bg: $bg:expr }) => { - pub const $name:Styles = Styles::StyleBasicColor(BasicColor { fg: $fg, bg: $bg }); + pub const $name: BasicColor = BasicColor { fg: $fg, bg: $bg }; }; } @@ -95,25 +103,21 @@ mod tests { fn test_color_code_macro() { color_code!(TEST_COLOR,{fg:100,bg:200}); match TEST_COLOR { - Styles::StyleBasicColor(BasicColor { fg, bg }) => { + BasicColor { fg, bg } => { assert_eq!(fg, 100); assert_eq!(bg, 200); } - _ => panic!("TEST_COLOR is not a BasicColor"), } } macro_rules! color_test { ($test_name:ident, $color_name:ident, $color:expr, $fg:expr, $bg:expr) => { #[test] - fn $test_name(){ - match $color_name{ - Styles::StyleBasicColor(BasicColor{fg,bg})=>{ - assert_eq!(fg,$fg); - assert_eq!(bg,$bg); - }, - _=>{ - panic!("This color is not a basic color!"); + fn $test_name() { + match $color_name { + BasicColor { fg, bg } => { + assert_eq!(fg, $fg); + assert_eq!(bg, $bg); } } } @@ -133,7 +137,13 @@ mod tests { color_test!(bright_green_color, BRIGHT_GREEN, "Bright Green", 92, 102); color_test!(bright_yellow_color, BRIGHT_YELLOW, "Bright Yellow", 93, 103); color_test!(bright_blue_color, BRIGHT_BLUE, "Bright Blue", 94, 104); - color_test!(bright_magenta_color, BRIGHT_MAGENTA, "Bright Magenta", 95, 105); + color_test!( + bright_magenta_color, + BRIGHT_MAGENTA, + "Bright Magenta", + 95, + 105 + ); color_test!(bright_cyan_color, BRIGHT_CYAN, "Bright Cyan", 96, 106); color_test!(bright_white_color, BRIGHT_WHITE, "Bright White", 97, 107); } diff --git a/src/styles/formatter.rs b/src/styles/formatter.rs index 07ded3d..386c680 100644 --- a/src/styles/formatter.rs +++ b/src/styles/formatter.rs @@ -1,4 +1,9 @@ -use super::{ paint_type::PaintType, Styles, Stylify }; +//! +//! + +// ======================================================================= + +use super::{paint_type::PaintType, Stylify}; // ======================================================================= @@ -20,7 +25,7 @@ impl Stylify for Formatter { /// A macro for generating formatter constants. macro_rules! formatter_code { ($name:ident, $code:expr) => { - pub const $name: Styles = Styles::StyleFormatter(Formatter { code: $code }); + pub const $name: Formatter = Formatter { code: $code }; }; } @@ -80,10 +85,9 @@ mod tests { fn test_formatter_code_macro() { formatter_code!(TEST_FORMATTER, 103); match TEST_FORMATTER { - Styles::StyleFormatter(Formatter { code }) => { + Formatter { code } => { assert_eq!(code, 103); } - _ => panic!("TEST_FORMATTER is not a Formatter"), } } @@ -91,13 +95,10 @@ mod tests { macro_rules! formatter_test { ($test_name:ident, $formatter_name:ident, $code:expr) => { #[test] - fn $test_name(){ - match $formatter_name{ - Styles::StyleFormatter(Formatter{code})=>{ - assert_eq!(code,$code); - }, - _=>{ - panic!("This formatter is not a Formatter!"); + fn $test_name() { + match $formatter_name { + Formatter { code } => { + assert_eq!(code, $code); } } } diff --git a/src/styles.rs b/src/styles/mod.rs similarity index 69% rename from src/styles.rs rename to src/styles/mod.rs index 955349f..187c316 100644 --- a/src/styles.rs +++ b/src/styles/mod.rs @@ -1,8 +1,6 @@ -/// A module for working with styles and colors. -/// -/// This module provides a set of types and traits for representing -/// different styles and colors, -/// as well as a way to generate styles based on a given paint type. +//! A module for working with styles and colors. +//! This module provides a set of types and traits for representing different styles and colors, +//! as well as a way to generate styles based on a given paint type. // ======================================================================= @@ -13,11 +11,12 @@ use palette::PaletteColor; use rgb::Rgb; // ======================================================================= -pub mod paint_type; -pub mod formatter; + pub mod basic_color; -pub mod rgb; +pub mod formatter; +pub mod paint_type; pub mod palette; +pub mod rgb; // ======================================================================= @@ -28,15 +27,15 @@ pub mod palette; #[derive(Debug, Clone)] pub enum Styles { /// A style represented by an RGB color. - StyleRgb(Rgb), + Rgb(Rgb), /// A style represented by a basic color. - StyleBasicColor(BasicColor), + BasicColor(BasicColor), /// A style represented by a palette color. - StylePaletteColor(PaletteColor), + PaletteColor(PaletteColor), /// A style represented by a paint type. - StylePaintType(PaintType), + PaintType(PaintType), /// A style represented by a formatter. - StyleFormatter(Formatter), + Formatter(Formatter), } impl Styles { @@ -46,15 +45,45 @@ impl Styles { /// It returns a string representing the generated styles. pub fn make_styles(&self, paint_type: Option<&PaintType>) -> String { match self { - Styles::StyleBasicColor(c) => c.make_styles(paint_type), - Styles::StylePaintType(c) => c.make_styles(paint_type), - Styles::StylePaletteColor(c) => c.make_styles(paint_type), - Styles::StyleRgb(c) => c.make_styles(paint_type), - Styles::StyleFormatter(c) => c.make_styles(paint_type), + Styles::BasicColor(c) => c.make_styles(paint_type), + Styles::PaintType(c) => c.make_styles(paint_type), + Styles::PaletteColor(c) => c.make_styles(paint_type), + Styles::Rgb(c) => c.make_styles(paint_type), + Styles::Formatter(c) => c.make_styles(paint_type), } } } +impl Into for Rgb { + fn into(self) -> Styles { + Styles::Rgb(self) + } +} + +impl Into for BasicColor { + fn into(self) -> Styles { + Styles::BasicColor(self) + } +} + +impl Into for PaletteColor { + fn into(self) -> Styles { + Styles::PaletteColor(self) + } +} + +impl Into for PaintType { + fn into(self) -> Styles { + Styles::PaintType(self) + } +} + +impl Into for Formatter { + fn into(self) -> Styles { + Styles::Formatter(self) + } +} + /// A trait for types that can generate styles based on a given paint type. pub trait Stylify { /// Generates String styles based on the given paint type. @@ -70,7 +99,7 @@ mod tests { #[test] fn test_make_styles() { - let style = Styles::StyleRgb(Rgb { r: 255, g: 0, b: 0 }); + let style = Styles::Rgb(Rgb { r: 255, g: 0, b: 0 }); let styles_default = style.make_styles(None); let styles_fg = style.make_styles(Some(&PaintType::FG)); let styles_bg = style.make_styles(Some(&PaintType::BG)); @@ -79,7 +108,7 @@ mod tests { assert_eq!(styles_bg, "48;2;255;0;0"); // - let style = Styles::StyleBasicColor(BasicColor { fg: 34, bg: 44 }); + let style = Styles::BasicColor(BasicColor { fg: 34, bg: 44 }); let styles_default = style.make_styles(None); let styles_fg = style.make_styles(Some(&PaintType::FG)); let styles_bg = style.make_styles(Some(&PaintType::BG)); @@ -88,7 +117,7 @@ mod tests { assert_eq!(styles_bg, "44"); // - let style = Styles::StylePaletteColor(PaletteColor { index: 44 }); + let style = Styles::PaletteColor(PaletteColor { index: 44 }); let styles_default = style.make_styles(None); let styles_fg = style.make_styles(Some(&PaintType::FG)); let styles_bg = style.make_styles(Some(&PaintType::BG)); @@ -97,7 +126,7 @@ mod tests { assert_eq!(styles_bg, "48;5;44"); // - let style = Styles::StylePaintType(PaintType::BG); + let style = Styles::PaintType(PaintType::BG); let styles_default = style.make_styles(None); let styles_fg = style.make_styles(Some(&PaintType::FG)); let styles_bg = style.make_styles(Some(&PaintType::BG)); @@ -106,7 +135,7 @@ mod tests { assert_eq!(styles_bg, ""); // - let style = Styles::StyleFormatter(Formatter { code: 3 }); + let style = Styles::Formatter(Formatter { code: 3 }); let styles_default = style.make_styles(None); let styles_fg = style.make_styles(Some(&PaintType::FG)); let styles_bg = style.make_styles(Some(&PaintType::BG)); diff --git a/src/styles/paint_type.rs b/src/styles/paint_type.rs index 39f29d7..fec7173 100644 --- a/src/styles/paint_type.rs +++ b/src/styles/paint_type.rs @@ -1,7 +1,7 @@ -/// A module for working with paint types. -/// -/// This module provides an enum `PaintType` that represents different types of paint. -/// It also implements the `Stylify` trait for `PaintType`, which allows for generating styles based on the paint type. +//! A module for working with paint types. +//! +//! This module provides an enum `PaintType` that represents different types of paint. +//! It also implements the `Stylify` trait for `PaintType`, which allows for generating styles based on the paint type. // ======================================================================= diff --git a/src/styles/palette.rs b/src/styles/palette.rs index c124519..3059c10 100644 --- a/src/styles/palette.rs +++ b/src/styles/palette.rs @@ -1,13 +1,13 @@ -/// A module for creating palette color. -/// -/// This module provides an struct `PaletteColor` -/// that represents palette color, index should be between 0 to 255 mean u8. -/// It also implements the `Stylify` trait for `PaletteColor`, -/// which allows for generating styles based on the paint type. +//! A module for creating palette color. +//! +//! This module provides an struct `PaletteColor` +//! that represents palette color, index should be between 0 to 255 mean u8. +//! It also implements the `Stylify` trait for `PaletteColor`, +//! which allows for generating styles based on the paint type. // ======================================================================= -use super::{ paint_type::PaintType, Stylify }; +use super::{paint_type::PaintType, Stylify}; // ======================================================================= diff --git a/src/styles/rgb.rs b/src/styles/rgb.rs index f8a6b3b..e98fb37 100644 --- a/src/styles/rgb.rs +++ b/src/styles/rgb.rs @@ -1,13 +1,13 @@ -/// A module for creating rgb color. -/// -/// This module provides an struct `Rgb` -/// that represents rgb color, with r,g,b field. -/// It also implements the `Stylify` trait for `Rgb`, -/// which allows for generating styles based on the paint type. +//! A module for creating rgb color. +//! +//! This module provides an struct `Rgb` +//! that represents rgb color, with r,g,b field. +//! It also implements the `Stylify` trait for `Rgb`, +//! which allows for generating styles based on the paint type. // ======================================================================= -use super::{ paint_type::PaintType, Stylify }; +use super::{paint_type::PaintType, Stylify}; // ======================================================================= @@ -46,28 +46,44 @@ mod tests { #[test] fn test_make_style_fg() { - let color = Rgb { r: 102, g: 23, b: 240 }; + let color = Rgb { + r: 102, + g: 23, + b: 240, + }; let styles = color.make_styles(Some(&PaintType::FG)); assert_eq!(styles, "38;2;102;23;240") } #[test] fn test_make_style_default_fg() { - let color = Rgb { r: 2, g: 55, b: 100 }; + let color = Rgb { + r: 2, + g: 55, + b: 100, + }; let styles = color.make_styles(None); assert_eq!(styles, "38;2;2;55;100") } #[test] fn test_make_style_bg() { - let color = Rgb { r: 255, g: 255, b: 43 }; + let color = Rgb { + r: 255, + g: 255, + b: 43, + }; let styles = color.make_styles(Some(&PaintType::BG)); assert_eq!(styles, "48;2;255;255;43") } #[test] fn test_fg_and_bg_values() { - let color = Rgb { r: 78, g: 32, b: 210 }; + let color = Rgb { + r: 78, + g: 32, + b: 210, + }; let styles_fg = color.make_styles(Some(&PaintType::FG)); let styles_bg = color.make_styles(Some(&PaintType::BG)); assert_eq!(styles_fg, "38;2;78;32;210");