use crate::view::Size;
use crate::{
Align, Border, Color, Hyperlink, Length, Overflow, Sides, TextAttribute, TextAttributes,
TextStyle, Underline, UnderlineStyle, VerticalAlign,
};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BlockStyle {
text_style: TextStyle,
padding: Sides,
margin: Sides,
border: Option<Border>,
border_top: bool,
border_right: bool,
border_bottom: bool,
border_left: bool,
border_text_style: TextStyle,
border_foreground: Option<Color>,
border_background: Option<Color>,
width: Option<Length>,
height: Option<Length>,
min_width: Option<u16>,
min_height: Option<u16>,
max_width: Option<u16>,
max_height: Option<u16>,
overflow: Overflow,
align: Align,
vertical_align: VerticalAlign,
}
impl Default for BlockStyle {
fn default() -> Self {
Self {
text_style: TextStyle::new(),
padding: Sides::default(),
margin: Sides::default(),
border: None,
border_top: true,
border_right: true,
border_bottom: true,
border_left: true,
border_text_style: TextStyle::new(),
border_foreground: None,
border_background: None,
width: None,
height: None,
min_width: None,
min_height: None,
max_width: None,
max_height: None,
overflow: Overflow::default(),
align: Align::default(),
vertical_align: VerticalAlign::default(),
}
}
}
impl BlockStyle {
pub fn new() -> Self {
Self::default()
}
pub fn from_text_style(text_style: TextStyle) -> Self {
Self {
text_style,
..Self::default()
}
}
pub const fn text_style(&self) -> &TextStyle {
&self.text_style
}
pub fn foreground(mut self, color: impl Into<Color>) -> Self {
self.text_style = self.text_style.foreground(color);
self
}
pub fn reset_foreground(mut self) -> Self {
self.text_style = self.text_style.reset_foreground();
self
}
pub fn background(mut self, color: impl Into<Color>) -> Self {
self.text_style = self.text_style.background(color);
self
}
pub fn reset_background(mut self) -> Self {
self.text_style = self.text_style.reset_background();
self
}
pub fn add_attribute(mut self, attribute: TextAttribute) -> Self {
self.text_style = self.text_style.add_attribute(attribute);
self
}
pub fn add_attributes(mut self, attributes: TextAttributes) -> Self {
self.text_style = self.text_style.add_attributes(attributes);
self
}
pub fn remove_attribute(mut self, attribute: TextAttribute) -> Self {
self.text_style = self.text_style.remove_attribute(attribute);
self
}
pub fn remove_attributes(mut self, attributes: TextAttributes) -> Self {
self.text_style = self.text_style.remove_attributes(attributes);
self
}
pub fn reset_attributes(mut self) -> Self {
self.text_style = self.text_style.reset_attributes();
self
}
pub fn bold(self) -> Self {
self.add_attribute(TextAttribute::Bold)
}
pub fn dim(self) -> Self {
self.add_attribute(TextAttribute::Dim)
}
pub fn italic(self) -> Self {
self.add_attribute(TextAttribute::Italic)
}
pub fn underlined(mut self) -> Self {
self.text_style = self.text_style.underlined();
self
}
pub fn underline_style(mut self, style: UnderlineStyle) -> Self {
self.text_style = self.text_style.underline_style(style);
self
}
pub fn underline_color(mut self, color: impl Into<Color>) -> Self {
self.text_style = self.text_style.underline_color(color);
self
}
pub fn underline(mut self, underline: Underline) -> Self {
self.text_style = self.text_style.underline(underline);
self
}
pub fn reset_underline(mut self) -> Self {
self.text_style = self.text_style.reset_underline();
self
}
pub fn hyperlink(mut self, hyperlink: impl Into<Hyperlink>) -> Self {
self.text_style = self.text_style.hyperlink(hyperlink);
self
}
pub fn reset_hyperlink(mut self) -> Self {
self.text_style = self.text_style.reset_hyperlink();
self
}
pub fn blink(self) -> Self {
self.add_attribute(TextAttribute::SlowBlink)
}
pub fn reverse(self) -> Self {
self.add_attribute(TextAttribute::Reversed)
}
pub fn hide(self) -> Self {
self.add_attribute(TextAttribute::Hidden)
}
pub fn strikethrough(self) -> Self {
self.add_attribute(TextAttribute::CrossedOut)
}
pub fn padding(mut self, sides: impl Into<Sides>) -> Self {
self.padding = sides.into();
self
}
pub fn reset_padding(mut self) -> Self {
self.padding = Sides::default();
self
}
pub fn margin(mut self, sides: impl Into<Sides>) -> Self {
self.margin = sides.into();
self
}
pub fn reset_margin(mut self) -> Self {
self.margin = Sides::default();
self
}
pub fn border(mut self, border: Border) -> Self {
self.border = Some(border);
self
}
pub fn reset_border(mut self) -> Self {
self.border = None;
self
}
pub fn border_top(mut self, enabled: bool) -> Self {
self.border_top = enabled;
self
}
pub fn reset_border_top(mut self) -> Self {
self.border_top = true;
self
}
pub fn border_right(mut self, enabled: bool) -> Self {
self.border_right = enabled;
self
}
pub fn reset_border_right(mut self) -> Self {
self.border_right = true;
self
}
pub fn border_bottom(mut self, enabled: bool) -> Self {
self.border_bottom = enabled;
self
}
pub fn reset_border_bottom(mut self) -> Self {
self.border_bottom = true;
self
}
pub fn border_left(mut self, enabled: bool) -> Self {
self.border_left = enabled;
self
}
pub fn reset_border_left(mut self) -> Self {
self.border_left = true;
self
}
pub fn border_text_style(mut self, style: TextStyle) -> Self {
self.border_text_style = style;
self
}
pub fn reset_border_text_style(mut self) -> Self {
self.border_text_style = TextStyle::default();
self
}
pub fn border_foreground(mut self, color: impl Into<Color>) -> Self {
self.border_foreground = Some(color.into());
self
}
pub fn reset_border_foreground(mut self) -> Self {
self.border_foreground = None;
self
}
pub fn border_background(mut self, color: impl Into<Color>) -> Self {
self.border_background = Some(color.into());
self
}
pub fn reset_border_background(mut self) -> Self {
self.border_background = None;
self
}
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = Some(width.into());
self
}
pub fn reset_width(mut self) -> Self {
self.width = None;
self
}
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = Some(height.into());
self
}
pub fn reset_height(mut self) -> Self {
self.height = None;
self
}
pub fn min_width(mut self, width: u16) -> Self {
self.min_width = Some(width);
self
}
pub fn reset_min_width(mut self) -> Self {
self.min_width = None;
self
}
pub fn min_height(mut self, height: u16) -> Self {
self.min_height = Some(height);
self
}
pub fn reset_min_height(mut self) -> Self {
self.min_height = None;
self
}
pub fn max_width(mut self, width: u16) -> Self {
self.max_width = Some(width);
self
}
pub fn reset_max_width(mut self) -> Self {
self.max_width = None;
self
}
pub fn max_height(mut self, height: u16) -> Self {
self.max_height = Some(height);
self
}
pub fn reset_max_height(mut self) -> Self {
self.max_height = None;
self
}
pub fn overflow(mut self, overflow: Overflow) -> Self {
self.overflow = overflow;
self
}
pub fn reset_overflow(mut self) -> Self {
self.overflow = Overflow::default();
self
}
pub fn align(mut self, align: Align) -> Self {
self.align = align;
self
}
pub fn reset_align(mut self) -> Self {
self.align = Align::default();
self
}
pub fn vertical_align(mut self, align: VerticalAlign) -> Self {
self.vertical_align = align;
self
}
pub fn reset_vertical_align(mut self) -> Self {
self.vertical_align = VerticalAlign::default();
self
}
pub const fn get_foreground(&self) -> Option<Color> {
self.text_style.get_foreground()
}
pub const fn get_background(&self) -> Option<Color> {
self.text_style.get_background()
}
pub const fn get_underline(&self) -> Option<Underline> {
self.text_style.get_underline()
}
pub const fn get_attributes(&self) -> TextAttributes {
self.text_style.get_attributes()
}
pub const fn get_padding(&self) -> Sides {
self.padding
}
pub const fn get_margin(&self) -> Sides {
self.margin
}
pub const fn get_border(&self) -> Option<Border> {
self.border
}
pub const fn get_border_top(&self) -> bool {
self.border_top
}
pub const fn get_border_right(&self) -> bool {
self.border_right
}
pub const fn get_border_bottom(&self) -> bool {
self.border_bottom
}
pub const fn get_border_left(&self) -> bool {
self.border_left
}
pub const fn get_border_text_style(&self) -> &TextStyle {
&self.border_text_style
}
pub const fn get_border_foreground(&self) -> Option<Color> {
self.border_foreground
}
pub const fn get_border_background(&self) -> Option<Color> {
self.border_background
}
pub const fn get_width(&self) -> Option<Length> {
self.width
}
pub const fn get_height(&self) -> Option<Length> {
self.height
}
pub const fn get_min_width(&self) -> Option<u16> {
self.min_width
}
pub const fn get_min_height(&self) -> Option<u16> {
self.min_height
}
pub const fn get_max_width(&self) -> Option<u16> {
self.max_width
}
pub const fn get_max_height(&self) -> Option<u16> {
self.max_height
}
pub const fn get_overflow(&self) -> &Overflow {
&self.overflow
}
pub fn frame_size(&self) -> Size {
let padding = self.padding;
let (left, right, top, bottom) = match self.border {
Some(_) => (
usize::from(self.border_left),
usize::from(self.border_right),
usize::from(self.border_top),
usize::from(self.border_bottom),
),
None => (0, 0, 0, 0),
};
Size::new(
left + right + usize::from(padding.left) + usize::from(padding.right),
top + bottom + usize::from(padding.top) + usize::from(padding.bottom),
)
}
pub const fn get_align(&self) -> Align {
self.align
}
pub const fn get_vertical_align(&self) -> VerticalAlign {
self.vertical_align
}
pub(crate) fn border_style(&self) -> TextStyle {
let mut style = self.border_text_style.clone();
if let Some(color) = self.border_foreground {
style = style.foreground(color);
}
if let Some(color) = self.border_background {
style = style.background(color);
}
style
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn reset_builders_restore_every_property_default() {
let style = BlockStyle::new()
.foreground(Color::RED)
.background(Color::BLUE)
.add_attribute(TextAttribute::Italic)
.underline_color(Color::GREEN)
.hyperlink("https://example.com")
.padding(1)
.margin(2)
.border(Border::ROUNDED)
.border_top(false)
.border_right(false)
.border_bottom(false)
.border_left(false)
.border_text_style(TextStyle::new().bold())
.border_foreground(Color::CYAN)
.border_background(Color::BLACK)
.width(20)
.height(4)
.min_width(8)
.min_height(2)
.max_width(30)
.max_height(6)
.overflow(Overflow::ellipsis())
.align(Align::Center)
.vertical_align(VerticalAlign::Bottom)
.reset_foreground()
.reset_background()
.reset_attributes()
.reset_underline()
.reset_hyperlink()
.reset_padding()
.reset_margin()
.reset_border()
.reset_border_top()
.reset_border_right()
.reset_border_bottom()
.reset_border_left()
.reset_border_text_style()
.reset_border_foreground()
.reset_border_background()
.reset_width()
.reset_height()
.reset_min_width()
.reset_min_height()
.reset_max_width()
.reset_max_height()
.reset_overflow()
.reset_align()
.reset_vertical_align();
assert_eq!(style, BlockStyle::new());
}
#[test]
fn attribute_operations_accept_sets() {
let attributes = TextAttribute::Bold | TextAttribute::Italic;
let style = BlockStyle::new()
.add_attributes(attributes)
.remove_attribute(TextAttribute::Italic);
assert_eq!(style.get_attributes(), TextAttribute::Bold.into());
}
}