use crate::border::{hidden_border, Border};
use crate::position::{Position, LEFT, TOP};
use crate::renderer::Renderer;
use crate::style::properties::*;
use std::sync::Arc;
#[derive(Clone)]
pub struct Style {
pub(crate) r: Option<Renderer>,
pub(crate) props: u64,
pub(crate) value: String,
pub(crate) attrs: u32,
pub(crate) fg_color: Option<String>,
pub(crate) bg_color: Option<String>,
pub(crate) width: i32,
pub(crate) height: i32,
pub(crate) max_width: i32,
pub(crate) max_height: i32,
pub(crate) align_horizontal: Position,
pub(crate) align_vertical: Position,
pub(crate) padding_top: i32,
pub(crate) padding_right: i32,
pub(crate) padding_bottom: i32,
pub(crate) padding_left: i32,
pub(crate) margin_top: i32,
pub(crate) margin_right: i32,
pub(crate) margin_bottom: i32,
pub(crate) margin_left: i32,
pub(crate) margin_bg_color: Option<String>,
pub(crate) border_style: Border,
pub(crate) border_top_fg_color: Option<String>,
pub(crate) border_right_fg_color: Option<String>,
pub(crate) border_bottom_fg_color: Option<String>,
pub(crate) border_left_fg_color: Option<String>,
pub(crate) border_top_bg_color: Option<String>,
pub(crate) border_right_bg_color: Option<String>,
pub(crate) border_bottom_bg_color: Option<String>,
pub(crate) border_left_bg_color: Option<String>,
pub(crate) tab_width: i32,
pub(crate) transform: Option<Arc<dyn Fn(String) -> String + Send + Sync>>,
}
impl Default for Style {
fn default() -> Self {
Self {
r: None,
props: 0,
value: String::new(),
attrs: 0,
fg_color: None,
bg_color: None,
width: 0,
height: 0,
max_width: 0,
max_height: 0,
align_horizontal: LEFT,
align_vertical: TOP,
padding_top: 0,
padding_right: 0,
padding_bottom: 0,
padding_left: 0,
margin_top: 0,
margin_right: 0,
margin_bottom: 0,
margin_left: 0,
margin_bg_color: None,
border_style: hidden_border(),
border_top_fg_color: None,
border_right_fg_color: None,
border_bottom_fg_color: None,
border_left_fg_color: None,
border_top_bg_color: None,
border_right_bg_color: None,
border_bottom_bg_color: None,
border_left_bg_color: None,
tab_width: TAB_WIDTH_DEFAULT,
transform: None,
}
}
}
impl Style {
pub(crate) fn is_set(&self, k: PropKey) -> bool {
self.props & k != 0
}
pub(crate) fn set_prop(&mut self, k: PropKey) {
self.props |= k;
}
pub(crate) fn unset_prop(&mut self, k: PropKey) {
self.props &= !k;
}
pub(crate) fn get_attr(&self, bit: u32) -> bool {
self.attrs & bit != 0
}
pub(crate) fn set_attr(&mut self, bit: u32, value: bool) {
if value {
self.attrs |= bit;
} else {
self.attrs &= !bit;
}
}
pub fn new() -> Self {
Self::default()
}
pub fn set_string(mut self, s: &str) -> Self {
self.value = s.to_string();
self
}
pub fn value(&self) -> &str {
&self.value
}
pub fn string(&self) -> String {
self.value.clone()
}
}
impl std::fmt::Debug for Style {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("Style")
.field("props", &self.props)
.field("value", &self.value)
.field("attrs", &self.attrs)
.field("fg_color", &self.fg_color)
.field("bg_color", &self.bg_color)
.field("width", &self.width)
.field("height", &self.height)
.field("max_width", &self.max_width)
.field("max_height", &self.max_height)
.field("align_horizontal", &self.align_horizontal)
.field("align_vertical", &self.align_vertical)
.field("padding_top", &self.padding_top)
.field("padding_right", &self.padding_right)
.field("padding_bottom", &self.padding_bottom)
.field("padding_left", &self.padding_left)
.field("margin_top", &self.margin_top)
.field("margin_right", &self.margin_right)
.field("margin_bottom", &self.margin_bottom)
.field("margin_left", &self.margin_left)
.field("margin_bg_color", &self.margin_bg_color)
.field("border_style", &self.border_style)
.field("border_top_fg_color", &self.border_top_fg_color)
.field("border_right_fg_color", &self.border_right_fg_color)
.field("border_bottom_fg_color", &self.border_bottom_fg_color)
.field("border_left_fg_color", &self.border_left_fg_color)
.field("border_top_bg_color", &self.border_top_bg_color)
.field("border_right_bg_color", &self.border_right_bg_color)
.field("border_bottom_bg_color", &self.border_bottom_bg_color)
.field("border_left_bg_color", &self.border_left_bg_color)
.field("tab_width", &self.tab_width)
.finish()
}
}
impl std::fmt::Display for Style {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.string())
}
}