tabled/settings/margin/
mod.rs

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