tabled/settings/padding/
mod.rs

1//! This module contains a [`Padding`] setting of a cell on a [`Table`].
2//!
3//! # Example
4//!
5//!
6#![cfg_attr(feature = "std", doc = "```")]
7#![cfg_attr(not(feature = "std"), doc = "```ignore")]
8//! # use tabled::{settings::{Style, Padding, object::Rows}, Table};
9//! # let data: Vec<&'static str> = Vec::new();
10//! let mut table = Table::new(&data);
11//! table.modify(
12//!     Rows::one(0),
13//!     Padding::new(0, 0, 1, 1).fill('>', '<', '^', 'V'),
14//! );
15//! ```
16//!
17//! [`Table`]: crate::Table
18
19use crate::{
20    grid::{
21        config::{CompactConfig, CompactMultilineConfig},
22        config::{Indent, Sides},
23    },
24    settings::TableOption,
25};
26
27#[cfg(feature = "std")]
28use crate::{
29    grid::{config::ColoredConfig, config::Entity},
30    settings::padding_expand::PaddingExpand,
31    settings::CellOption,
32};
33
34/// Padding is responsible for a left/right/top/bottom inner indent of a particular cell.
35///
36/// # Example
37///
38#[cfg_attr(feature = "std", doc = "```")]
39#[cfg_attr(not(feature = "std"), doc = "```ignore")]
40/// use tabled::{
41///     Table,
42///     settings::{Padding, Style, Modify, object::Cell},
43/// };
44///
45/// let table = Table::new("2022".chars())
46///     .with(Style::modern())
47///     .with(Modify::new((2, 0)).with(Padding::new(1, 1, 2, 2)))
48///     .to_string();
49///
50/// assert_eq!(
51///     table,
52///     concat!(
53///         "┌──────┐\n",
54///         "│ char │\n",
55///         "├──────┤\n",
56///         "│ 2    │\n",
57///         "├──────┤\n",
58///         "│      │\n",
59///         "│      │\n",
60///         "│ 0    │\n",
61///         "│      │\n",
62///         "│      │\n",
63///         "├──────┤\n",
64///         "│ 2    │\n",
65///         "├──────┤\n",
66///         "│ 2    │\n",
67///         "└──────┘",
68///     ),
69/// );
70/// ```
71#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
72pub struct Padding {
73    indent: Sides<Indent>,
74}
75
76impl Padding {
77    /// Construct's an Padding object.
78    ///
79    /// It uses space(' ') as a default fill character.
80    /// To set a custom character you can use [`Padding::fill`] function.
81    pub const fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
82        Self {
83            indent: Sides::new(
84                Indent::spaced(left),
85                Indent::spaced(right),
86                Indent::spaced(top),
87                Indent::spaced(bottom),
88            ),
89        }
90    }
91
92    /// Construct's an Padding object with all sides set to 0.
93    ///
94    /// It uses space(' ') as a default fill character.
95    /// To set a custom character you can use [`Padding::fill`] function.
96    pub const fn zero() -> Self {
97        Self::new(0, 0, 0, 0)
98    }
99
100    /// The function, sets a characters for the padding on an each side.
101    pub const fn fill(mut self, left: char, right: char, top: char, bottom: char) -> Self {
102        self.indent.left.fill = left;
103        self.indent.right.fill = right;
104        self.indent.top.fill = top;
105        self.indent.bottom.fill = bottom;
106        self
107    }
108
109    #[cfg(feature = "std")]
110    /// Construct's an PaddingExpand object.
111    pub const fn expand(horizontal: bool) -> PaddingExpand {
112        if horizontal {
113            PaddingExpand::Horizontal
114        } else {
115            PaddingExpand::Vertical
116        }
117    }
118}
119
120#[cfg(feature = "std")]
121impl<R> CellOption<R, ColoredConfig> for Padding {
122    fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
123        let indent = self.indent;
124        let pad = Sides::new(indent.left, indent.right, indent.top, indent.bottom);
125        cfg.set_padding(entity, pad);
126    }
127}
128
129#[cfg(feature = "std")]
130impl<R, D> TableOption<R, ColoredConfig, D> for Padding {
131    fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
132        <Self as CellOption<R, ColoredConfig>>::change(self, records, cfg, Entity::Global)
133    }
134}
135
136impl<R, D> TableOption<R, CompactConfig, D> for Padding {
137    fn change(self, _: &mut R, cfg: &mut CompactConfig, _: &mut D) {
138        *cfg = cfg.set_padding(self.indent);
139    }
140}
141
142impl<R, D> TableOption<R, CompactMultilineConfig, D> for Padding {
143    fn change(self, _: &mut R, cfg: &mut CompactMultilineConfig, _: &mut D) {
144        cfg.set_padding(self.indent);
145    }
146}