tabled/features/
padding.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
//! This module contains a [`Padding`] setting of a cell on a [`Table`].
//!
//! # Example
//!
//! ```
//! use tabled::{TableIteratorExt, Padding, Style, Modify, object::Cell};
//!
//! let data = "2022".chars();
//!
//! let table = data.table()
//!     .with(Style::modern())
//!     .with(Modify::new(Cell(2, 0)).with(Padding::new(1, 1, 2, 2)))
//!     .to_string();
//!
//! assert_eq!(
//!     table,
//!     concat!(
//!         "┌──────┐\n",
//!         "│ char │\n",
//!         "├──────┤\n",
//!         "│ 2    │\n",
//!         "├──────┤\n",
//!         "│      │\n",
//!         "│      │\n",
//!         "│ 0    │\n",
//!         "│      │\n",
//!         "│      │\n",
//!         "├──────┤\n",
//!         "│ 2    │\n",
//!         "├──────┤\n",
//!         "│ 2    │\n",
//!         "└──────┘",
//!     ),
//! );
//! ```
//!
//! [`Table`]: crate::Table

use papergrid::{Entity, Indent};

use crate::{
    table::{CellOption, Table},
    TableOption,
};

// #[cfg(feature = "color")]
// use crate::style::Color;

/// Padding is responsible for a left/right/top/bottom inner indent of a particular cell.
///
/// ```rust,no_run
/// # use tabled::{Style, Padding, object::Rows, Table, Modify};
/// # let data: Vec<&'static str> = Vec::new();
/// let table = Table::new(&data).with(Modify::new(Rows::single(0)).with(Padding::new(0, 0, 1, 1).set_fill('>', '<', '^', 'V')));
/// ```
#[derive(Debug)]
pub struct Padding(papergrid::Padding);

impl Padding {
    /// Construct's an Padding object.
    ///
    /// It uses space(' ') as a default fill character.
    /// To set a custom character you can use [`Self::set_fill`] function.
    pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
        Self(papergrid::Padding {
            top: Indent::spaced(top),
            bottom: Indent::spaced(bottom),
            left: Indent::spaced(left),
            right: Indent::spaced(right),
        })
    }

    /// Construct's an Padding object with all sides set to 0.
    ///
    /// It uses space(' ') as a default fill character.
    /// To set a custom character you can use [`Self::set_fill`] function.
    pub fn zero() -> Self {
        let indent = Indent::spaced(0);
        Self(papergrid::Padding {
            top: indent,
            bottom: indent,
            left: indent,
            right: indent,
        })
    }

    /// The function, sets a characters for the padding on an each side.
    pub fn set_fill(mut self, left: char, right: char, top: char, bottom: char) -> Self {
        self.0.left.fill = left;
        self.0.right.fill = right;
        self.0.top.fill = top;
        self.0.bottom.fill = bottom;
        self
    }
}

impl<R> CellOption<R> for Padding {
    fn change_cell(&mut self, table: &mut Table<R>, entity: Entity) {
        table.get_config_mut().set_padding(entity, self.0);
        table.destroy_width_cache();
    }
}

impl<R> TableOption<R> for Padding {
    fn change(&mut self, table: &mut Table<R>) {
        table.get_config_mut().set_padding(Entity::Global, self.0);
        table.destroy_width_cache();
    }
}