Add own Style type
This commit is contained in:
parent
67f703cf68
commit
4c304ffe79
2 changed files with 65 additions and 0 deletions
|
|
@ -12,6 +12,7 @@
|
||||||
mod buffer;
|
mod buffer;
|
||||||
mod coords;
|
mod coords;
|
||||||
mod frame;
|
mod frame;
|
||||||
|
mod style;
|
||||||
mod styled;
|
mod styled;
|
||||||
mod terminal;
|
mod terminal;
|
||||||
mod widget;
|
mod widget;
|
||||||
|
|
@ -21,6 +22,7 @@ mod wrap;
|
||||||
|
|
||||||
pub use coords::*;
|
pub use coords::*;
|
||||||
pub use frame::*;
|
pub use frame::*;
|
||||||
|
pub use style::*;
|
||||||
pub use styled::*;
|
pub use styled::*;
|
||||||
pub use terminal::*;
|
pub use terminal::*;
|
||||||
pub use widget::*;
|
pub use widget::*;
|
||||||
|
|
|
||||||
63
src/style.rs
Normal file
63
src/style.rs
Normal 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
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue