tabled/features/margin.rs
1//! This module contains a Margin settings of a [`Table`].
2//!
3//! # Example
4//!
5//! ```
6//! use tabled::{Margin, Style, TableIteratorExt};
7//!
8//! let data = vec!["Hello", "World", "!"];
9//!
10//! let mut table = data.table();
11//! table.with(Style::markdown()).with(Margin::new(3, 3, 1, 0));
12//!
13//! assert_eq!(
14//! table.to_string(),
15//! concat!(
16//! " \n",
17//! " | &str | \n",
18//! " |-------| \n",
19//! " | Hello | \n",
20//! " | World | \n",
21//! " | ! | ",
22//! )
23//! );
24//! ```
25//!
26//! [`Table`]: crate::Table
27
28use papergrid::Indent;
29
30use crate::{Table, TableOption};
31
32/// Margin is responsible for a left/right/top/bottom outer indent of a grid.
33///
34/// ```rust,no_run
35/// # use tabled::{Margin, Table};
36/// # let data: Vec<&'static str> = Vec::new();
37/// let table = Table::new(&data).with(Margin::new(1, 1, 1, 1).set_fill('>', '<', 'V', '^'));
38/// ```
39#[derive(Debug, Clone)]
40pub struct Margin(papergrid::Margin);
41
42impl Margin {
43 /// Construct's an Margin object.
44 ///
45 /// It uses space(' ') as a default fill character.
46 /// To set a custom character you can use [`Self::set_fill`] function.
47 pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
48 Self(papergrid::Margin {
49 top: Indent::spaced(top),
50 bottom: Indent::spaced(bottom),
51 left: Indent::spaced(left),
52 right: Indent::spaced(right),
53 })
54 }
55
56 /// The function, sets a characters for the margin on an each side.
57 pub fn set_fill(mut self, left: char, right: char, top: char, bottom: char) -> Self {
58 self.0.left.fill = left;
59 self.0.right.fill = right;
60 self.0.top.fill = top;
61 self.0.bottom.fill = bottom;
62 self
63 }
64}
65
66impl<R> TableOption<R> for Margin {
67 fn change(&mut self, table: &mut Table<R>) {
68 table.get_config_mut().set_margin(self.0);
69 }
70}