tabled/features/padding.rs
1//! This module contains a [`Padding`] setting of a cell on a [`Table`].
2//!
3//! # Example
4//!
5//! ```
6//! use tabled::{TableIteratorExt, Padding, Style, Modify, object::Cell};
7//!
8//! let data = "2022".chars();
9//!
10//! let table = data.table()
11//! .with(Style::modern())
12//! .with(Modify::new(Cell(2, 0)).with(Padding::new(1, 1, 2, 2)))
13//! .to_string();
14//!
15//! assert_eq!(
16//! table,
17//! concat!(
18//! "┌──────┐\n",
19//! "│ char │\n",
20//! "├──────┤\n",
21//! "│ 2 │\n",
22//! "├──────┤\n",
23//! "│ │\n",
24//! "│ │\n",
25//! "│ 0 │\n",
26//! "│ │\n",
27//! "│ │\n",
28//! "├──────┤\n",
29//! "│ 2 │\n",
30//! "├──────┤\n",
31//! "│ 2 │\n",
32//! "└──────┘",
33//! ),
34//! );
35//! ```
36//!
37//! [`Table`]: crate::Table
38
39use papergrid::{Entity, Indent};
40
41use crate::{
42 table::{CellOption, Table},
43 TableOption,
44};
45
46// #[cfg(feature = "color")]
47// use crate::style::Color;
48
49/// Padding is responsible for a left/right/top/bottom inner indent of a particular cell.
50///
51/// ```rust,no_run
52/// # use tabled::{Style, Padding, object::Rows, Table, Modify};
53/// # let data: Vec<&'static str> = Vec::new();
54/// let table = Table::new(&data).with(Modify::new(Rows::single(0)).with(Padding::new(0, 0, 1, 1).set_fill('>', '<', '^', 'V')));
55/// ```
56#[derive(Debug)]
57pub struct Padding(papergrid::Padding);
58
59impl Padding {
60 /// Construct's an Padding object.
61 ///
62 /// It uses space(' ') as a default fill character.
63 /// To set a custom character you can use [`Self::set_fill`] function.
64 pub fn new(left: usize, right: usize, top: usize, bottom: usize) -> Self {
65 Self(papergrid::Padding {
66 top: Indent::spaced(top),
67 bottom: Indent::spaced(bottom),
68 left: Indent::spaced(left),
69 right: Indent::spaced(right),
70 })
71 }
72
73 /// Construct's an Padding object with all sides set to 0.
74 ///
75 /// It uses space(' ') as a default fill character.
76 /// To set a custom character you can use [`Self::set_fill`] function.
77 pub fn zero() -> Self {
78 let indent = Indent::spaced(0);
79 Self(papergrid::Padding {
80 top: indent,
81 bottom: indent,
82 left: indent,
83 right: indent,
84 })
85 }
86
87 /// The function, sets a characters for the padding on an each side.
88 pub fn set_fill(mut self, left: char, right: char, top: char, bottom: char) -> Self {
89 self.0.left.fill = left;
90 self.0.right.fill = right;
91 self.0.top.fill = top;
92 self.0.bottom.fill = bottom;
93 self
94 }
95}
96
97impl<R> CellOption<R> for Padding {
98 fn change_cell(&mut self, table: &mut Table<R>, entity: Entity) {
99 table.get_config_mut().set_padding(entity, self.0);
100 table.destroy_width_cache();
101 }
102}
103
104impl<R> TableOption<R> for Padding {
105 fn change(&mut self, table: &mut Table<R>) {
106 table.get_config_mut().set_padding(Entity::Global, self.0);
107 table.destroy_width_cache();
108 }
109}