tabled/settings/formatting/
trim_strategy.rs

1use crate::{
2    grid::config::ColoredConfig,
3    grid::config::Entity,
4    settings::{CellOption, TableOption},
5};
6
7/// `TrimStrategy` determines if it's allowed to use empty space while doing [`Alignment`].
8///
9/// # Examples
10///
11/// ```
12/// use tabled::{
13///     Table,
14///     settings::{
15///         Style, Modify, Alignment, object::Segment,
16///         formatting::{TrimStrategy, AlignmentStrategy}
17///     }
18/// };
19///
20/// let mut table = Table::new(&["   Hello World"]);
21/// table
22///     .with(Style::modern())
23///     .with(
24///         Modify::new(Segment::all())
25///             .with(Alignment::left())
26///             .with(TrimStrategy::Horizontal)
27///     );
28///
29/// // Note that nothing was changed exactly.
30///
31/// assert_eq!(
32///     table.to_string(),
33///     "┌────────────────┐\n\
34///      │ &str           │\n\
35///      ├────────────────┤\n\
36///      │ Hello World    │\n\
37///      └────────────────┘"
38/// );
39///
40/// // To trim lines you would need also set [`AlignmentStrategy`].
41/// table.with(Modify::new(Segment::all()).with(AlignmentStrategy::PerLine));
42///
43/// assert_eq!(
44///     table.to_string(),
45///     "┌────────────────┐\n\
46///      │ &str           │\n\
47///      ├────────────────┤\n\
48///      │ Hello World    │\n\
49///      └────────────────┘"
50/// );
51///
52/// let mut table = Table::new(&["   \n\n\n    Hello World"]);
53/// table
54///     .with(Style::modern())
55///     .with(
56///         Modify::new(Segment::all())
57///             .with(Alignment::center())
58///             .with(Alignment::top())
59///             .with(TrimStrategy::Vertical)
60///     );
61///
62/// assert_eq!(
63///     table.to_string(),
64///     "┌─────────────────┐\n\
65///      │      &str       │\n\
66///      ├─────────────────┤\n\
67///      │     Hello World │\n\
68///      │                 │\n\
69///      │                 │\n\
70///      │                 │\n\
71///      └─────────────────┘"
72/// );
73/// ```
74///
75/// [`Alignment`]: crate::settings::Alignment
76#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
77pub enum TrimStrategy {
78    /// Allow vertical trim.
79    Vertical,
80    /// Allow horizontal trim.
81    Horizontal,
82    /// Allow horizontal and vertical trim.
83    Both,
84    /// Doesn't allow any trim.
85    None,
86}
87
88impl<R> CellOption<R, ColoredConfig> for TrimStrategy {
89    fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
90        match self {
91            TrimStrategy::Vertical => {
92                cfg.set_trim_vertical(entity, true);
93            }
94            TrimStrategy::Horizontal => {
95                cfg.set_trim_horizontal(entity, true);
96            }
97            TrimStrategy::Both => {
98                cfg.set_trim_horizontal(entity, true);
99                cfg.set_trim_vertical(entity, true);
100            }
101            TrimStrategy::None => {
102                cfg.set_trim_horizontal(entity, false);
103                cfg.set_trim_vertical(entity, false);
104            }
105        }
106    }
107}
108
109impl<R, D> TableOption<R, ColoredConfig, D> for TrimStrategy {
110    fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
111        <Self as CellOption<_, _>>::change(self, records, cfg, Entity::Global)
112    }
113
114    fn hint_change(&self) -> Option<Entity> {
115        None
116    }
117}