| 1 | use 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)] |
| 77 | pub 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 | |
| 88 | impl<R> CellOption<R, ColoredConfig> for TrimStrategy { |
| 89 | fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) { |
| 90 | let mut formatting = *cfg.get_formatting(entity); |
| 91 | |
| 92 | // todo: could be changed to be a struct an enum like consts in `impl` block. |
| 93 | match self { |
| 94 | TrimStrategy::Vertical => { |
| 95 | formatting.vertical_trim = true; |
| 96 | } |
| 97 | TrimStrategy::Horizontal => { |
| 98 | formatting.horizontal_trim = true; |
| 99 | } |
| 100 | TrimStrategy::Both => { |
| 101 | formatting.vertical_trim = true; |
| 102 | formatting.horizontal_trim = true; |
| 103 | } |
| 104 | TrimStrategy::None => { |
| 105 | formatting.vertical_trim = false; |
| 106 | formatting.horizontal_trim = false; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | cfg.set_formatting(entity, formatting); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | impl<R, D> TableOption<R, ColoredConfig, D> for TrimStrategy { |
| 115 | fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) { |
| 116 | <Self as CellOption<_, _>>::change(self, records, cfg, Entity::Global) |
| 117 | } |
| 118 | |
| 119 | fn hint_change(&self) -> Option<Entity> { |
| 120 | None |
| 121 | } |
| 122 | } |
| 123 | |