tabled/settings/formatting/
justification.rs

1use crate::{
2    grid::{
3        ansi::ANSIBuf,
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::one(0)).with(Justification::new('#')));
71/// table.with(Modify::new(Columns::one(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)]
86pub struct Justification {
87    c: Option<char>,
88    color: Option<ANSIBuf>,
89}
90
91impl 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    /// Creates new [`Justification`] object.
101    pub fn colored(c: char, color: Color) -> Self {
102        Self {
103            c: Some(c),
104            color: Some(color.into()),
105        }
106    }
107
108    /// Sets a color for a justification.
109    pub fn color(self, color: Color) -> Self {
110        Self {
111            c: self.c,
112            color: Some(color.into()),
113        }
114    }
115}
116
117impl<R, D> TableOption<R, ColoredConfig, D> for Justification {
118    fn change(self, _: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
119        let c = self.c.unwrap_or(' ');
120        let color = self.color;
121
122        cfg.set_justification(Entity::Global, c);
123        cfg.set_justification_color(Entity::Global, color);
124    }
125
126    fn hint_change(&self) -> Option<Entity> {
127        None
128    }
129}
130
131impl<R> CellOption<R, ColoredConfig> for Justification {
132    fn change(self, _: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
133        let c = self.c.unwrap_or(' ');
134        let color = self.color;
135
136        cfg.set_justification(entity, c);
137        cfg.set_justification_color(entity, color);
138    }
139
140    fn hint_change(&self) -> Option<Entity> {
141        None
142    }
143}