1 | use crate::{ |
2 | grid::{ |
3 | color::AnsiColor, |
4 | config::{ColoredConfig, Entity}, |
5 | }, |
6 | settings::{CellOption, Color, TableOption}, |
7 | }; |
8 | |
9 | /// Set a justification character and a color. |
10 | /// |
11 | /// Default value is `' '` (`<space>`) with no color. |
12 | /// |
13 | /// # Examples |
14 | /// |
15 | /// Setting a justification character. |
16 | /// |
17 | /// ``` |
18 | /// use tabled::{ |
19 | /// Table, |
20 | /// settings::formatting::Justification, |
21 | /// }; |
22 | /// |
23 | /// let mut table = Table::new(&[("Hello" , "" ), ("" , "World" )]); |
24 | /// table.with(Justification::new('#' )); |
25 | /// |
26 | /// assert_eq!( |
27 | /// table.to_string(), |
28 | /// "+-------+-------+ \n\ |
29 | /// | &str# | &str# | \n\ |
30 | /// +-------+-------+ \n\ |
31 | /// | Hello | ##### | \n\ |
32 | /// +-------+-------+ \n\ |
33 | /// | ##### | World | \n\ |
34 | /// +-------+-------+" |
35 | /// ); |
36 | /// ``` |
37 | /// |
38 | /// Setting a justification color. |
39 | /// |
40 | /// ``` |
41 | /// use tabled::{ |
42 | /// Table, |
43 | /// settings::{formatting::Justification, Color}, |
44 | /// }; |
45 | /// |
46 | /// let mut table = Table::new(&[("Hello" , "" ), ("" , "World" )]); |
47 | /// table.with(Justification::default().color(Color::BG_BRIGHT_RED)); |
48 | /// |
49 | /// assert_eq!( |
50 | /// table.to_string(), |
51 | /// "+-------+-------+ \n\ |
52 | /// | &str \u{1b}[101m \u{1b}[49m | &str \u{1b}[101m \u{1b}[49m | \n\ |
53 | /// +-------+-------+ \n\ |
54 | /// | Hello | \u{1b}[101m \u{1b}[49m | \n\ |
55 | /// +-------+-------+ \n\ |
56 | /// | \u{1b}[101m \u{1b}[49m | World | \n\ |
57 | /// +-------+-------+" |
58 | /// ); |
59 | /// ``` |
60 | /// |
61 | /// Use different justification for different columns. |
62 | /// |
63 | /// ``` |
64 | /// use tabled::{ |
65 | /// Table, |
66 | /// settings::{Modify, object::Columns, formatting::Justification}, |
67 | /// }; |
68 | /// |
69 | /// let mut table = Table::new(&[("Hello" , "" ), ("" , "World" )]); |
70 | /// table.with(Modify::new(Columns::single(0)).with(Justification::new('#' ))); |
71 | /// table.with(Modify::new(Columns::single(1)).with(Justification::new('@' ))); |
72 | /// |
73 | /// assert_eq!( |
74 | /// table.to_string(), |
75 | /// "+-------+-------+ \n\ |
76 | /// | &str# | &str@ | \n\ |
77 | /// +-------+-------+ \n\ |
78 | /// | Hello | @@@@@ | \n\ |
79 | /// +-------+-------+ \n\ |
80 | /// | ##### | World | \n\ |
81 | /// +-------+-------+" |
82 | /// ); |
83 | /// ``` |
84 | /// |
85 | #[derive (Debug, Default, Clone)] |
86 | pub struct Justification { |
87 | c: Option<char>, |
88 | color: Option<AnsiColor<'static>>, |
89 | } |
90 | |
91 | impl Justification { |
92 | /// Creates new [`Justification`] object. |
93 | pub fn new(c: char) -> Self { |
94 | Self { |
95 | c: Some(c), |
96 | color: None, |
97 | } |
98 | } |
99 | |
100 | /// Sets a color for a justification. |
101 | pub fn color(self, color: Color) -> Self { |
102 | Self { |
103 | c: self.c, |
104 | color: Some(color.into()), |
105 | } |
106 | } |
107 | } |
108 | |
109 | impl<R, D> TableOption<R, D, ColoredConfig> for Justification { |
110 | fn change(self, _: &mut R, cfg: &mut ColoredConfig, _: &mut D) { |
111 | let c: char = self.c.unwrap_or(default:' ' ); |
112 | let color: Option> = self.color; |
113 | |
114 | cfg.set_justification(Entity::Global, c); |
115 | cfg.set_justification_color(Entity::Global, color); |
116 | } |
117 | } |
118 | |
119 | impl<R> CellOption<R, ColoredConfig> for Justification { |
120 | fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) { |
121 | let c: char = self.c.unwrap_or(default:' ' ); |
122 | let color: Option> = self.color; |
123 | |
124 | cfg.set_justification(entity, c); |
125 | cfg.set_justification_color(entity, color); |
126 | } |
127 | } |
128 | |