From 49fefd6e8f6890f73174c014aa011d74fdff6bfa Mon Sep 17 00:00:00 2001 From: Peter Hindes Date: Wed, 26 Feb 2020 22:50:29 -0700 Subject: [PATCH] Added Support For Chars Colors (#386) --- CHANGELOG.md | 3 +++ src/style.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++ src/style/macros.rs | 28 ++++++++++++++++++++++++ 3 files changed, 83 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index bfc1e56..1a06595 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +# Version 0.16.1 +- Add support for converting chars to StylizedContent + # Version 0.16.0 - Make terminal size function work on `/dev/tty` instead of `STDOUT_FILENO`. - Change attribute vector in `ContentStyle` to bitmask. diff --git a/src/style.rs b/src/style.rs index bc9c752..deecff4 100644 --- a/src/style.rs +++ b/src/style.rs @@ -197,6 +197,44 @@ impl Colorize<&'static str> for &'static str { def_str_color!(background_color: on_grey => Color::Grey); } +impl Colorize for char { + // foreground colors + def_char_color!(foreground_color: black => Color::Black); + def_char_color!(foreground_color: dark_grey => Color::DarkGrey); + def_char_color!(foreground_color: red => Color::Red); + def_char_color!(foreground_color: dark_red => Color::DarkRed); + def_char_color!(foreground_color: green => Color::Green); + def_char_color!(foreground_color: dark_green => Color::DarkGreen); + def_char_color!(foreground_color: yellow => Color::Yellow); + def_char_color!(foreground_color: dark_yellow => Color::DarkYellow); + def_char_color!(foreground_color: blue => Color::Blue); + def_char_color!(foreground_color: dark_blue => Color::DarkBlue); + def_char_color!(foreground_color: magenta => Color::Magenta); + def_char_color!(foreground_color: dark_magenta => Color::DarkMagenta); + def_char_color!(foreground_color: cyan => Color::Cyan); + def_char_color!(foreground_color: dark_cyan => Color::DarkCyan); + def_char_color!(foreground_color: white => Color::White); + def_char_color!(foreground_color: grey => Color::Grey); + + // background colors + def_char_color!(background_color: on_black => Color::Black); + def_char_color!(background_color: on_dark_grey => Color::DarkGrey); + def_char_color!(background_color: on_red => Color::Red); + def_char_color!(background_color: on_dark_red => Color::DarkRed); + def_char_color!(background_color: on_green => Color::Green); + def_char_color!(background_color: on_dark_green => Color::DarkGreen); + def_char_color!(background_color: on_yellow => Color::Yellow); + def_char_color!(background_color: on_dark_yellow => Color::DarkYellow); + def_char_color!(background_color: on_blue => Color::Blue); + def_char_color!(background_color: on_dark_blue => Color::DarkBlue); + def_char_color!(background_color: on_magenta => Color::Magenta); + def_char_color!(background_color: on_dark_magenta => Color::DarkMagenta); + def_char_color!(background_color: on_cyan => Color::Cyan); + def_char_color!(background_color: on_dark_cyan => Color::DarkCyan); + def_char_color!(background_color: on_white => Color::White); + def_char_color!(background_color: on_grey => Color::Grey); +} + impl Styler<&'static str> for &'static str { def_str_attr!(reset => Attribute::Reset); def_str_attr!(bold => Attribute::Bold); @@ -211,6 +249,20 @@ impl Styler<&'static str> for &'static str { def_str_attr!(crossed_out => Attribute::CrossedOut); } +impl Styler for char { + def_char_attr!(reset => Attribute::Reset); + def_char_attr!(bold => Attribute::Bold); + def_char_attr!(underlined => Attribute::Underlined); + def_char_attr!(reverse => Attribute::Reverse); + def_char_attr!(dim => Attribute::Dim); + def_char_attr!(italic => Attribute::Italic); + def_char_attr!(negative => Attribute::Reverse); + def_char_attr!(slow_blink => Attribute::SlowBlink); + def_char_attr!(rapid_blink => Attribute::RapidBlink); + def_char_attr!(hidden => Attribute::Hidden); + def_char_attr!(crossed_out => Attribute::CrossedOut); +} + /// Returns available color count. /// /// # Notes diff --git a/src/style/macros.rs b/src/style/macros.rs index 0babf1d..e1e2f23 100644 --- a/src/style/macros.rs +++ b/src/style/macros.rs @@ -34,6 +34,20 @@ macro_rules! def_str_color { }; } +macro_rules! def_char_color { + ($side:ident: $name:ident => $color:path) => { + fn $name(self) -> StyledContent { + StyledContent::new( + ContentStyle { + $side: Some($color), + ..Default::default() + }, + self + ) + } + }; +} + macro_rules! def_str_attr { ($name:ident => $attr:path) => { fn $name(self) -> StyledContent<&'static str> { @@ -47,3 +61,17 @@ macro_rules! def_str_attr { } } } + +macro_rules! def_char_attr { + ($name:ident => $attr:path) => { + fn $name(self) -> StyledContent { + StyledContent::new( + ContentStyle { + attributes: $attr.into(), + ..Default::default() + }, + self + ) + } + } +}