tabled/settings/margin_color/
mod.rs

1//! This module contains a Margin settings of a [`Table`].
2//!
3//! [`Table`]: crate::Table
4
5use crate::{
6    grid::{
7        ansi::ANSIStr,
8        config::Sides,
9        config::{CompactConfig, CompactMultilineConfig},
10    },
11    settings::TableOption,
12};
13
14#[cfg(feature = "std")]
15use crate::grid::{ansi::ANSIBuf, config::ColoredConfig};
16
17/// MarginColor is responsible for a left/right/top/bottom outer color of a grid.
18///
19/// # Example
20///
21#[cfg_attr(feature = "ansi", doc = "```")]
22#[cfg_attr(not(feature = "ansi"), doc = "```ignore")]
23/// use tabled::{
24///     settings::{Margin, MarginColor, Style, Color},
25///     Table,
26/// };
27///
28/// let data = vec!["Hello", "World", "!"];
29///
30/// let mut table = Table::new(data);
31/// table
32///     .with(Style::markdown())
33///     .with(Margin::new(3, 3, 1, 0))
34///     .with(MarginColor::filled(Color::BG_RED));
35///
36/// assert_eq!(
37///     table.to_string(),
38///     concat!(
39///         "\u{1b}[41m               \u{1b}[49m\n",
40///         "\u{1b}[41m   \u{1b}[49m| &str  |\u{1b}[41m   \u{1b}[49m\n",
41///         "\u{1b}[41m   \u{1b}[49m|-------|\u{1b}[41m   \u{1b}[49m\n",
42///         "\u{1b}[41m   \u{1b}[49m| Hello |\u{1b}[41m   \u{1b}[49m\n",
43///         "\u{1b}[41m   \u{1b}[49m| World |\u{1b}[41m   \u{1b}[49m\n",
44///         "\u{1b}[41m   \u{1b}[49m| !     |\u{1b}[41m   \u{1b}[49m",
45///     )
46/// );
47/// ```
48#[derive(Debug, Clone)]
49pub struct MarginColor<C> {
50    colors: Sides<C>,
51}
52
53impl<C> MarginColor<C> {
54    /// Construct's an Margin object.
55    pub const fn new(left: C, right: C, top: C, bottom: C) -> Self {
56        Self {
57            colors: Sides::new(left, right, top, bottom),
58        }
59    }
60
61    /// The function, sets a color for the margin on an each side.
62    pub fn filled(color: C) -> Self
63    where
64        C: Clone,
65    {
66        Self::new(color.clone(), color.clone(), color.clone(), color)
67    }
68}
69
70impl MarginColor<ANSIStr<'static>> {
71    /// Construct's an Padding object with no color.
72    pub const fn empty() -> Self {
73        Self::new(
74            ANSIStr::empty(),
75            ANSIStr::empty(),
76            ANSIStr::empty(),
77            ANSIStr::empty(),
78        )
79    }
80}
81
82#[cfg(feature = "std")]
83impl<R, D, C> TableOption<R, ColoredConfig, D> for MarginColor<C>
84where
85    C: Into<ANSIBuf> + Clone,
86{
87    fn change(self, _: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
88        let c = self.colors.clone();
89        let margin = Sides::new(
90            Some(c.left.into()),
91            Some(c.right.into()),
92            Some(c.top.into()),
93            Some(c.bottom.into()),
94        );
95
96        cfg.set_margin_color(margin);
97    }
98}
99
100impl<R, D, C> TableOption<R, CompactConfig, D> for MarginColor<C>
101where
102    C: Into<ANSIStr<'static>> + Clone,
103{
104    fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) {
105        let colors = self.colors.convert_into();
106        *cfg = cfg.set_margin_color(colors);
107    }
108}
109
110impl<R, D, C> TableOption<R, CompactMultilineConfig, D> for MarginColor<C>
111where
112    C: Into<ANSIStr<'static>> + Clone,
113{
114    fn change(self, _: &mut R, cfg: &mut CompactMultilineConfig, _: &mut D) {
115        let colors = self.colors.convert_into();
116        cfg.set_margin_color(colors);
117    }
118}