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