tabled/settings/padding/
mod.rs1#![cfg_attr(feature = "std", doc = "```")]
7#![cfg_attr(not(feature = "std"), doc = "```ignore")]
8use 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#[cfg_attr(feature = "std", doc = "```")]
39#[cfg_attr(not(feature = "std"), doc = "```ignore")]
40#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
72pub struct Padding {
73 indent: Sides<Indent>,
74}
75
76impl Padding {
77 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 pub const fn zero() -> Self {
97 Self::new(0, 0, 0, 0)
98 }
99
100 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 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}