Add own Style type

This commit is contained in:
Joscha 2023-02-16 20:53:57 +01:00
parent 67f703cf68
commit 4c304ffe79
2 changed files with 65 additions and 0 deletions

View file

@ -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::*;

63
src/style.rs Normal file
View file

@ -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<ContentStyle> for Style {
fn as_ref(&self) -> &ContentStyle {
&self.content_style
}
}
impl AsMut<ContentStyle> 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
}
}