tabled/settings/margin/
mod.rs1#![cfg_attr(feature = "std", doc = "```")]
6#![cfg_attr(not(feature = "std"), doc = "```ignore")]
7use 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#[cfg_attr(feature = "std", doc = "```")]
31#[cfg_attr(not(feature = "std"), doc = "```ignore")]
32#[derive(Debug, Clone)]
54pub struct Margin {
55 indent: Sides<Indent>,
56}
57
58impl Margin {
59 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 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}