tabled/settings/padding_color/
mod.rs1use crate::{
4 grid::{
5 ansi::ANSIStr,
6 config::Sides,
7 config::{CompactConfig, CompactMultilineConfig},
8 },
9 settings::TableOption,
10};
11
12#[cfg(feature = "std")]
13use crate::grid::{ansi::ANSIBuf, config::ColoredConfig, config::Entity};
14#[cfg(feature = "std")]
15use crate::settings::CellOption;
16
17#[cfg_attr(feature = "ansi", doc = "```")]
22#[cfg_attr(not(feature = "ansi"), doc = "```ignore")]
23#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
52pub struct PaddingColor<C> {
53 colors: Sides<C>,
54}
55
56impl<C> PaddingColor<C> {
57 pub const fn new(left: C, right: C, top: C, bottom: C) -> Self {
59 Self {
60 colors: Sides::new(left, right, top, bottom),
61 }
62 }
63
64 pub fn filled(color: C) -> Self
66 where
67 C: Clone,
68 {
69 Self::new(color.clone(), color.clone(), color.clone(), color)
70 }
71}
72
73impl PaddingColor<ANSIStr<'static>> {
74 pub const fn empty() -> Self {
76 Self::new(
77 ANSIStr::empty(),
78 ANSIStr::empty(),
79 ANSIStr::empty(),
80 ANSIStr::empty(),
81 )
82 }
83}
84
85impl<C> From<PaddingColor<C>> for Sides<C> {
86 fn from(value: PaddingColor<C>) -> Self {
87 value.colors
88 }
89}
90
91impl<C> From<Sides<C>> for PaddingColor<C> {
92 fn from(colors: Sides<C>) -> Self {
93 Self { colors }
94 }
95}
96
97#[cfg(feature = "std")]
98impl<R, C> CellOption<R, ColoredConfig> for PaddingColor<C>
99where
100 C: Into<ANSIBuf> + Clone,
101{
102 fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
103 let colors = self.colors.clone();
104 let pad = Sides::new(
105 Some(colors.left.into()),
106 Some(colors.right.into()),
107 Some(colors.top.into()),
108 Some(colors.bottom.into()),
109 );
110 cfg.set_padding_color(entity, pad);
111 }
112}
113
114#[cfg(feature = "std")]
115impl<R, D, C> TableOption<R, ColoredConfig, D> for PaddingColor<C>
116where
117 C: Into<ANSIBuf> + Clone,
118{
119 fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
120 <Self as CellOption<R, ColoredConfig>>::change(self, records, cfg, Entity::Global)
121 }
122}
123
124impl<R, D, C> TableOption<R, CompactConfig, D> for PaddingColor<C>
125where
126 C: Into<ANSIStr<'static>> + Clone,
127{
128 fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) {
129 let c = self.colors.clone();
130 let colors = Sides::new(c.left.into(), c.right.into(), c.top.into(), c.bottom.into());
131 *cfg = cfg.set_padding_color(colors);
132 }
133}
134
135impl<R, D, C> TableOption<R, CompactMultilineConfig, D> for PaddingColor<C>
136where
137 C: Into<ANSIStr<'static>> + Clone,
138{
139 fn change(self, _: &mut R, cfg: &mut CompactMultilineConfig, _: &mut D) {
140 let c = self.colors.clone();
141 let colors = Sides::new(c.left.into(), c.right.into(), c.top.into(), c.bottom.into());
142 cfg.set_padding_color(colors);
143 }
144}