From 4c304ffe795b11f798b7bb40f8abb3fc77686f7f Mon Sep 17 00:00:00 2001 From: Joscha Date: Thu, 16 Feb 2023 20:53:57 +0100 Subject: [PATCH] Add own Style type --- src/lib.rs | 2 ++ src/style.rs | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) create mode 100644 src/style.rs diff --git a/src/lib.rs b/src/lib.rs index 34cb81b..a204e8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -12,6 +12,7 @@ mod buffer; mod coords; mod frame; +mod style; mod styled; mod terminal; mod widget; @@ -21,6 +22,7 @@ mod wrap; pub use coords::*; pub use frame::*; +pub use style::*; pub use styled::*; pub use terminal::*; pub use widget::*; diff --git a/src/style.rs b/src/style.rs new file mode 100644 index 0000000..2528572 --- /dev/null +++ b/src/style.rs @@ -0,0 +1,63 @@ +use crossterm::style::{ContentStyle, Stylize}; + +fn merge_cs(base: ContentStyle, cover: ContentStyle) -> ContentStyle { + ContentStyle { + foreground_color: cover.foreground_color.or(base.foreground_color), + background_color: cover.background_color.or(base.background_color), + underline_color: cover.underline_color.or(base.underline_color), + attributes: cover.attributes, + } +} + +#[derive(Debug, Clone, Copy, Default)] +pub struct Style { + pub content_style: ContentStyle, + pub opaque: bool, +} + +impl Style { + pub fn new() -> Self { + Self::default() + } + + pub fn transparent(mut self) -> Self { + self.opaque = false; + self + } + + pub fn opaque(mut self) -> Self { + self.opaque = true; + self + } + + pub fn cover(self, base: Self) -> Self { + if self.opaque { + return self; + } + + Self { + content_style: merge_cs(base.content_style, self.content_style), + opaque: base.opaque, + } + } +} + +impl AsRef for Style { + fn as_ref(&self) -> &ContentStyle { + &self.content_style + } +} + +impl AsMut for Style { + fn as_mut(&mut self) -> &mut ContentStyle { + &mut self.content_style + } +} + +impl Stylize for Style { + type Styled = Self; + + fn stylize(self) -> Self::Styled { + self + } +}