use std::fmt::Display;
use bitflags::bitflags;
use super::Color;
bitflags! {
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy)]
pub struct Attributes: u8 {
const BOLD = 0b01;
const ITALIC = 0b10;
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct StyleSheet {
pub fg: Option<Color>,
pub bg: Option<Color>,
pub att: Attributes,
}
impl StyleSheet {
pub fn new() -> Self {
Self::empty()
}
pub fn empty() -> Self {
Self {
fg: None,
bg: None,
att: Attributes::empty(),
}
}
pub fn is_empty(&self) -> bool {
self.fg.is_none() && self.bg.is_none() && self.att.is_empty()
}
pub fn with_fg(mut self, fg: Color) -> Self {
self.fg = Some(fg);
self
}
pub fn with_bg(mut self, bg: Color) -> Self {
self.bg = Some(bg);
self
}
pub fn with_attr(mut self, attributes: Attributes) -> Self {
self.att = attributes;
self
}
}
impl Default for StyleSheet {
fn default() -> Self {
Self::empty()
}
}
#[derive(Clone, Debug)]
pub struct Styled<T>
where
T: Display,
{
pub content: T,
pub style: StyleSheet,
}
impl<T> Styled<T>
where
T: Display,
{
pub fn new(content: T) -> Self {
Self {
content,
style: StyleSheet::default(),
}
}
#[allow(unused)]
pub fn with_style_sheet(mut self, style_sheet: StyleSheet) -> Self {
self.style = style_sheet;
self
}
pub fn with_fg(mut self, fg: Color) -> Self {
self.style.fg = Some(fg);
self
}
pub fn with_bg(mut self, bg: Color) -> Self {
self.style.bg = Some(bg);
self
}
pub fn with_attr(mut self, attributes: Attributes) -> Self {
self.style.att = attributes;
self
}
pub fn with_content<U>(self, content: U) -> Styled<U>
where
U: Display,
{
Styled {
content,
style: self.style,
}
}
}
impl<T> Copy for Styled<T> where T: Copy + Display {}
impl<T> Default for Styled<T>
where
T: Default + Display,
{
fn default() -> Self {
Self {
content: Default::default(),
style: Default::default(),
}
}
}
impl<T> From<T> for Styled<T>
where
T: Display,
{
fn from(from: T) -> Self {
Self::new(from)
}
}
impl<T> PartialEq for Styled<T>
where
T: PartialEq + Display,
{
fn eq(&self, other: &Self) -> bool {
self.style == other.style && self.content == other.content
}
}
impl<T> Eq for Styled<T> where T: Eq + Display {}