use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not}; macro_rules! auto_impl_ref_binop_trait { (impl $trait_name:ident, $method:ident for $t:ty, $u:ty) => { impl $trait_name<&$u> for $t { type Output = <$t as $trait_name<$t>>::Output; #[inline] fn $method(self, rhs: &$u) -> Self::Output { $trait_name::$method(self, *rhs) } } impl $trait_name<$u> for &$t { type Output = <$t as $trait_name<$t>>::Output; #[inline] fn $method(self, rhs: $u) -> Self::Output { $trait_name::$method(*self, rhs) } } impl $trait_name<&$u> for &$t { type Output = <$t as $trait_name<$t>>::Output; #[inline] fn $method(self, rhs: &$u) -> Self::Output { $trait_name::$method(*self, *rhs) } } }; } macro_rules! impl_assign_op_trait { ( $trait:ident, $method:ident for $t:ty, $u:ty, using $used_trait:ident::$used_method:ident ) => { impl $trait<$u> for $t { #[inline] fn $method(&mut self, other: $u) { *self = $used_trait::$used_method(&*self, other); } } impl $trait<&$u> for $t { #[inline] fn $method(&mut self, other: &$u) { *self = $used_trait::$used_method(&*self, other); } } }; } const CLEARV: u8 = 0b0000_0000; const BOLD: u8 = 0b0000_0001; const UNDERLINE: u8 = 0b0000_0010; const REVERSED: u8 = 0b0000_0100; const ITALIC: u8 = 0b0000_1000; const BLINK: u8 = 0b0001_0000; const HIDDEN: u8 = 0b0010_0000; const DIMMED: u8 = 0b0100_0000; const STRIKETHROUGH: u8 = 0b1000_0000; static STYLES: [(u8, Styles); 8] = [ (BOLD, Styles::Bold), (DIMMED, Styles::Dimmed), (UNDERLINE, Styles::Underline), (REVERSED, Styles::Reversed), (ITALIC, Styles::Italic), (BLINK, Styles::Blink), (HIDDEN, Styles::Hidden), (STRIKETHROUGH, Styles::Strikethrough), ]; pub static CLEAR: Style = Style(CLEARV); /// A combinatorial style such as bold, italics, dimmed, etc. /// /// ## Creation /// /// `Style::default()` returns a `Style` with no style switches /// activated and is the default method of creating a plain `Style`. /// /// ## `Style` from a set of `Styles`s / `Styles` iterator /// /// `Style` implements `FromIter` which means that it is /// possible to do the following: /// /// ```rust /// # use colored::*; /// let style = Style::from_iter([Styles::Bold, Styles::Italic, Styles::Strikethrough]); /// for styles in [Styles::Bold, Styles::Italic, Styles::Strikethrough] { /// assert!(style.contains(styles)); /// } /// ``` /// /// As you can see, this is a good thing to keep in mind, although for /// most cases, where you're not setting styles dynamically and are /// simply creating a pre-defined set of styles, using [`Default`] and /// then using the builder-style methods is likely prettier. /// /// ```rust /// # use colored::*; /// let many_styles = Style::default() /// .bold() /// .underline() /// .italic() /// .blink(); /// ``` /// /// ## Implementation of logical bitwise operators /// /// `Style` implements bitwise logical operations that operate on /// the held style switches collectively. By far the most common /// and useful is the bitwise 'or' operator `|` which combines two /// styles, merging their combined styles into one. Example: /// /// ```rust /// # use colored::*; /// let only_bold = Style::from(Styles::Bold); /// // This line is actually an example of `Styles`'s bitwise logic impls but still. /// let underline_and_italic = Styles::Underline | Styles::Italic; /// let all_three = only_bold | underline_and_italic; /// /// assert!(all_three.contains(Styles::Bold) /// && all_three.contains(Styles::Underline) /// && all_three.contains(Styles::Italic)); /// ``` /// /// This functionality also allows for easily turning off styles /// of one `Styles` using another by combining the `&` and `!` /// operators. /// /// ```rust /// # use colored::*; /// let mut very_loud_style = Style::default() /// .bold() /// .underline() /// .italic() /// .strikethrough() /// .hidden(); /// /// // Oops! Some of those should not be in there! /// // This Style now has all styles _except_ the two we don't want /// // (hidden and strikethough). /// let remove_mask = /// !Style::from_iter([Styles::Hidden, Styles::Strikethrough]); /// very_loud_style &= remove_mask; /// /// // `very_loud_style` no longer contains the undesired style /// // switches... /// assert!(!very_loud_style.contains(Styles::Hidden) /// && !very_loud_style.contains(Styles::Strikethrough)); /// // ...but it retains everything else! /// assert!(very_loud_style.contains(Styles::Bold)); /// ``` #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub struct Style(u8); /// Enum containing all of the available style settings that can be /// applied to a [`Styles`] and by extension, a colrized type. /// /// ## Implementation of bitwise logical operators /// /// The implementations of [`BitAnd`], [`BitOr`], [`BitXor`], and /// [`Not`] are really extensions of [`Style`]'s implementations of /// the same. [`BitOr`] is great for starting chains of `Styles`'s /// for creating [`Style`]'s. /// /// ``` /// # use colored::*; /// let my_styles = /// // BitOr for Styles (Styles | Styles) = Style /// Styles::Bold | Styles::Underline /// // BitOr for Style (Style | Styles) = Style /// | Styles::Italic; /// /// for s in [Styles::Bold, Styles::Underline, Styles::Italic] { /// assert!(my_styles.contains(s)); /// } /// ``` /// /// [`Not`] has far fewer use cases but can still find use in /// turning a `Styles` into a [`Style`] with all styles activated /// except that `Styles`. /// /// ``` /// # use colored::*; /// let everything_but_bold = !Styles::Bold; /// /// assert!(everything_but_bold.contains(Styles::Underline)); /// assert!(everything_but_bold.contains(Styles::Strikethrough)); /// assert!(!everything_but_bold.contains(Styles::Bold)); /// ``` #[derive(Clone, Copy, PartialEq, Eq, Debug)] #[allow(missing_docs)] pub enum Styles { Clear, Bold, Dimmed, Underline, Reversed, Italic, Blink, Hidden, Strikethrough, } impl Styles { const fn to_str<'a>(self) -> &'a str { match self { Self::Clear => "", // unreachable, but we don't want to panic Self::Bold => "1", Self::Dimmed => "2", Self::Italic => "3", Self::Underline => "4", Self::Blink => "5", Self::Reversed => "7", Self::Hidden => "8", Self::Strikethrough => "9", } } const fn to_u8(self) -> u8 { match self { Self::Clear => CLEARV, Self::Bold => BOLD, Self::Dimmed => DIMMED, Self::Italic => ITALIC, Self::Underline => UNDERLINE, Self::Blink => BLINK, Self::Reversed => REVERSED, Self::Hidden => HIDDEN, Self::Strikethrough => STRIKETHROUGH, } } fn from_u8(u: u8) -> Option> { if u == CLEARV { return None; } let res: Vec = STYLES .iter() .filter(|&(mask, _)| 0 != (u & mask)) .map(|&(_, value)| value) .collect(); if res.is_empty() { None } else { Some(res) } } } impl BitAnd for Styles { type Output = Style; fn bitand(self, rhs: Self) -> Self::Output { Style(self.to_u8() & rhs.to_u8()) } } auto_impl_ref_binop_trait!(impl BitAnd, bitand for Styles, Styles); impl BitAnd