From f038dbc1ab2194fe5bc29c8084850f009a4d7530 Mon Sep 17 00:00:00 2001 From: immmdreza Date: Thu, 14 Nov 2024 11:36:44 +0330 Subject: [PATCH 1/4] Reformat files. --- src/ansi_code.rs | 17 ++++++++------- src/lib.rs | 25 +++++++++++----------- src/styles.rs | 15 +++++++------ src/styles/basic_color.rs | 44 ++++++++++++++++++++++++++------------- src/styles/formatter.rs | 21 ++++++++++++------- src/styles/paint_type.rs | 8 +++---- src/styles/palette.rs | 14 ++++++------- src/styles/rgb.rs | 38 +++++++++++++++++++++++---------- 8 files changed, 109 insertions(+), 73 deletions(-) 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/lib.rs b/src/lib.rs index d5914c5..788c5a8 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -7,15 +7,11 @@ mod ansi_code; mod styles; // ======================================================================= + 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, }, }; @@ -93,7 +89,8 @@ impl StyledText { pub fn paint(&mut self) -> String { 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| { @@ -109,9 +106,8 @@ impl StyledText { .rev() .collect(); let start_codes = start_codes_list.join(""); - let end_codes = ANSIEscapeCode::new( - &formatter::RESET.make_styles(Some(&default_paint_type)) - ).code(); + let end_codes = + ANSIEscapeCode::new(&formatter::RESET.make_styles(Some(&default_paint_type))).code(); format!("{}{}{}", start_codes, self.text, end_codes) } @@ -128,7 +124,8 @@ 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.start_styles + .push(Styles::StylePaintType(PaintType::FG)); self } @@ -144,7 +141,8 @@ 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.start_styles + .push(Styles::StylePaintType(PaintType::BG)); self } @@ -172,7 +170,8 @@ 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.start_styles + .push(Styles::StylePaletteColor(PaletteColor { index })); self } diff --git a/src/styles.rs b/src/styles.rs index 955349f..f4a96b9 100644 --- a/src/styles.rs +++ b/src/styles.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; // ======================================================================= diff --git a/src/styles/basic_color.rs b/src/styles/basic_color.rs index 7e7fc34..4494947 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, Styles, 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: Styles = Styles::StyleBasicColor(BasicColor { fg: $fg, bg: $bg }); }; } @@ -106,14 +114,14 @@ mod tests { 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 { + Styles::StyleBasicColor(BasicColor { fg, bg }) => { + assert_eq!(fg, $fg); + assert_eq!(bg, $bg); + } + _ => { + panic!("This color is not a basic color!"); } } } @@ -133,7 +141,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..883a448 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, Styles, Stylify}; // ======================================================================= @@ -91,13 +96,13 @@ 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 { + Styles::StyleFormatter(Formatter { code }) => { + assert_eq!(code, $code); + } + _ => { + panic!("This formatter is not a Formatter!"); } } } 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"); From 8ee8a160b553023bd679fba25a14cc9138e4b699 Mon Sep 17 00:00:00 2001 From: immmdreza Date: Thu, 14 Nov 2024 11:39:31 +0330 Subject: [PATCH 2/4] styles.rs file can be safely replaced with styles/mod.rs. --- src/into_styles.rs | 0 src/lib.rs | 1 + src/{styles.rs => styles/mod.rs} | 0 3 files changed, 1 insertion(+) create mode 100644 src/into_styles.rs rename src/{styles.rs => styles/mod.rs} (100%) diff --git a/src/into_styles.rs b/src/into_styles.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/lib.rs b/src/lib.rs index 788c5a8..4e90f08 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ //! It allows you to create styled text strings with various colors, effects, and formatters. mod ansi_code; +mod into_styles; mod styles; // ======================================================================= diff --git a/src/styles.rs b/src/styles/mod.rs similarity index 100% rename from src/styles.rs rename to src/styles/mod.rs From 05fda8472451f8e85d6ed5f2ce5de6a5fcc5187a Mon Sep 17 00:00:00 2001 From: immmdreza Date: Thu, 14 Nov 2024 11:53:41 +0330 Subject: [PATCH 3/4] Introduced a new trait IntoStyled, so that you can simply do "Hi Father".styled() on anything that can be converted to String not just &str! --- src/into_styles.rs | 88 ++++++++++++++++++++++++++++++++++++++++++++++ src/lib.rs | 6 ++-- 2 files changed, 92 insertions(+), 2 deletions(-) diff --git a/src/into_styles.rs b/src/into_styles.rs index e69de29..d906fc8 100644 --- a/src/into_styles.rs +++ b/src/into_styles.rs @@ -0,0 +1,88 @@ +//! +//! + +// ======================================================================= + +use crate::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()) + } +} + +#[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 4e90f08..2a07bfa 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -9,6 +9,8 @@ mod styles; // ======================================================================= +use into_styles::IntoStyled; + use crate::{ ansi_code::ANSIEscapeCode, styles::{ @@ -65,8 +67,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. From ae739554f679f9b4a3e72502ae204ff7163fb27e Mon Sep 17 00:00:00 2001 From: immmdreza Date: Thu, 14 Nov 2024 13:09:25 +0330 Subject: [PATCH 4/4] May users feel friendly. --- Cargo.lock | 7 ++ Cargo.toml | 7 +- example/Cargo.toml | 7 ++ example/src/main.rs | 23 +++++ src/{into_styles.rs => helpers.rs} | 12 ++- src/lib.rs | 139 +++++++++++++++-------------- src/prelude.rs | 37 ++++++++ src/styles/basic_color.rs | 12 +-- src/styles/formatter.rs | 12 +-- src/styles/mod.rs | 60 +++++++++---- 10 files changed, 213 insertions(+), 103 deletions(-) create mode 100644 example/Cargo.toml create mode 100644 example/src/main.rs rename src/{into_styles.rs => helpers.rs} (91%) create mode 100644 src/prelude.rs 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/into_styles.rs b/src/helpers.rs similarity index 91% rename from src/into_styles.rs rename to src/helpers.rs index d906fc8..b95bb8f 100644 --- a/src/into_styles.rs +++ b/src/helpers.rs @@ -3,7 +3,7 @@ // ======================================================================= -use crate::StyledText; +use crate::{styles::Styles, StyledText}; // ======================================================================= @@ -67,6 +67,16 @@ impl> IntoStyled for T { } } +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::*; diff --git a/src/lib.rs b/src/lib.rs index 2a07bfa..7ff3484 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,12 +4,16 @@ //! It allows you to create styled text strings with various colors, effects, and formatters. mod ansi_code; -mod into_styles; +mod helpers; +pub mod prelude; mod styles; // ======================================================================= -use into_styles::IntoStyled; +use std::fmt::Display; + +use helpers::{IntoStyle, IntoStyled}; +use styles::{basic_color::BasicColor, formatter::Formatter}; use crate::{ ansi_code::ANSIEscapeCode, @@ -77,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 { @@ -86,10 +96,7 @@ 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 @@ -97,7 +104,7 @@ impl StyledText { .iter() .rev() .filter_map(|s| { - if let Styles::StylePaintType(p) = s { + if let Styles::PaintType(p) = s { default_paint_type = p.clone(); return None; } @@ -109,10 +116,25 @@ impl StyledText { .rev() .collect(); let start_codes = start_codes_list.join(""); - let end_codes = - ANSIEscapeCode::new(&formatter::RESET.make_styles(Some(&default_paint_type))).code(); + let end_codes = ANSIEscapeCode::new( + &Styles::Formatter(formatter::RESET).make_styles(Some(&default_paint_type)), + ) + .code(); + + 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() + } - format!("{}{}{}", start_codes, self.text, end_codes) + /// 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. @@ -127,9 +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,9 +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 @@ -159,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. @@ -173,9 +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. @@ -186,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. @@ -198,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. @@ -210,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. @@ -222,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. @@ -234,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. @@ -246,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. @@ -258,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. @@ -270,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. @@ -282,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. @@ -294,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. @@ -306,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. @@ -318,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. @@ -330,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. @@ -343,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. @@ -355,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. @@ -367,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: @@ -383,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. @@ -395,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. @@ -407,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. @@ -419,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. @@ -431,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. @@ -445,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. @@ -459,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. @@ -471,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 4494947..ae5442a 100644 --- a/src/styles/basic_color.rs +++ b/src/styles/basic_color.rs @@ -3,7 +3,7 @@ // ======================================================================= -use super::{paint_type::PaintType, Styles, Stylify}; +use super::{paint_type::PaintType, Stylify}; // ======================================================================= @@ -35,7 +35,7 @@ impl Stylify for BasicColor { /// 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 }; }; } @@ -103,11 +103,10 @@ 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"), } } @@ -116,13 +115,10 @@ mod tests { #[test] fn $test_name() { match $color_name { - Styles::StyleBasicColor(BasicColor { fg, bg }) => { + BasicColor { fg, bg } => { assert_eq!(fg, $fg); assert_eq!(bg, $bg); } - _ => { - panic!("This color is not a basic color!"); - } } } }; diff --git a/src/styles/formatter.rs b/src/styles/formatter.rs index 883a448..386c680 100644 --- a/src/styles/formatter.rs +++ b/src/styles/formatter.rs @@ -3,7 +3,7 @@ // ======================================================================= -use super::{paint_type::PaintType, Styles, Stylify}; +use super::{paint_type::PaintType, Stylify}; // ======================================================================= @@ -25,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 }; }; } @@ -85,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"), } } @@ -98,12 +97,9 @@ mod tests { #[test] fn $test_name() { match $formatter_name { - Styles::StyleFormatter(Formatter { code }) => { + Formatter { code } => { assert_eq!(code, $code); } - _ => { - panic!("This formatter is not a Formatter!"); - } } } }; diff --git a/src/styles/mod.rs b/src/styles/mod.rs index f4a96b9..187c316 100644 --- a/src/styles/mod.rs +++ b/src/styles/mod.rs @@ -27,15 +27,15 @@ pub mod rgb; #[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 { @@ -45,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. @@ -69,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)); @@ -78,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)); @@ -87,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)); @@ -96,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)); @@ -105,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));