tabled/settings/padding_color/
mod.rs

1//! This module contains a [`PaddingColor`] setting of a cell on a [`Table`].
2
3use 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/// PaddingColor is responsible for a left/right/top/bottom inner color of a particular cell.
18///
19/// # Example
20///
21#[cfg_attr(feature = "ansi", doc = "```")]
22#[cfg_attr(not(feature = "ansi"), doc = "```ignore")]
23/// use tabled::{
24///     Table,
25///     settings::{Padding, PaddingColor, Color, Style},
26/// };
27///
28/// let table = Table::new("2024".chars())
29///     .with(Style::modern())
30///     .modify((2, 0), Padding::new(2, 4, 0, 0))
31///     .modify((2, 0), PaddingColor::filled(Color::FG_RED))
32///     .to_string();
33///
34/// assert_eq!(
35///     table,
36///     concat!(
37///         "┌───────┐\n",
38///         "│ char  │\n",
39///         "├───────┤\n",
40///         "│ 2     │\n",
41///         "├───────┤\n",
42///         "│\u{1b}[31m  \u{1b}[39m0\u{1b}[31m    \u{1b}[39m│\n",
43///         "├───────┤\n",
44///         "│ 2     │\n",
45///         "├───────┤\n",
46///         "│ 4     │\n",
47///         "└───────┘",
48///     ),
49/// );
50/// ```
51#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
52pub struct PaddingColor<C> {
53    colors: Sides<C>,
54}
55
56impl<C> PaddingColor<C> {
57    /// Construct's an Padding object.
58    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    /// The function, sets a color for all sides.
65    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    /// Construct's an Padding object with no color.
75    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}