use crate::{
grid::{
ansi::ANSIStr,
config::Sides,
config::{CompactConfig, CompactMultilineConfig},
},
settings::TableOption,
};
#[cfg(feature = "std")]
use crate::grid::{ansi::ANSIBuf, config::ColoredConfig, config::Entity};
#[cfg(feature = "std")]
use crate::settings::CellOption;
#[cfg_attr(feature = "ansi", doc = "```")]
#[cfg_attr(not(feature = "ansi"), doc = "```ignore")]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct PaddingColor<C> {
colors: Sides<C>,
}
impl<C> PaddingColor<C> {
pub const fn new(left: C, right: C, top: C, bottom: C) -> Self {
Self {
colors: Sides::new(left, right, top, bottom),
}
}
pub fn filled(color: C) -> Self
where
C: Clone,
{
Self::new(color.clone(), color.clone(), color.clone(), color)
}
}
impl PaddingColor<ANSIStr<'static>> {
pub const fn empty() -> Self {
Self::new(
ANSIStr::empty(),
ANSIStr::empty(),
ANSIStr::empty(),
ANSIStr::empty(),
)
}
}
impl<C> From<PaddingColor<C>> for Sides<C> {
fn from(value: PaddingColor<C>) -> Self {
value.colors
}
}
impl<C> From<Sides<C>> for PaddingColor<C> {
fn from(colors: Sides<C>) -> Self {
Self { colors }
}
}
#[cfg(feature = "std")]
impl<R, C> CellOption<R, ColoredConfig> for PaddingColor<C>
where
C: Into<ANSIBuf> + Clone,
{
fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
let colors = self.colors.clone();
let pad = Sides::new(
Some(colors.left.into()),
Some(colors.right.into()),
Some(colors.top.into()),
Some(colors.bottom.into()),
);
cfg.set_padding_color(entity, pad);
}
}
#[cfg(feature = "std")]
impl<R, D, C> TableOption<R, ColoredConfig, D> for PaddingColor<C>
where
C: Into<ANSIBuf> + Clone,
{
fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
<Self as CellOption<R, ColoredConfig>>::change(self, records, cfg, Entity::Global)
}
}
impl<R, D, C> TableOption<R, CompactConfig, D> for PaddingColor<C>
where
C: Into<ANSIStr<'static>> + Clone,
{
fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) {
let c = self.colors.clone();
let colors = Sides::new(c.left.into(), c.right.into(), c.top.into(), c.bottom.into());
*cfg = cfg.set_padding_color(colors);
}
}
impl<R, D, C> TableOption<R, CompactMultilineConfig, D> for PaddingColor<C>
where
C: Into<ANSIStr<'static>> + Clone,
{
fn change(self, _: &mut R, cfg: &mut CompactMultilineConfig, _: &mut D) {
let c = self.colors.clone();
let colors = Sides::new(c.left.into(), c.right.into(), c.top.into(), c.bottom.into());
cfg.set_padding_color(colors);
}
}