1//! This module contains a configuration of a Border to set its color via [`BorderColor`].
2
3use crate::{
4 grid::{
5 color::AnsiColor,
6 config::{Border, ColoredConfig, Entity},
7 records::{ExactRecords, Records},
8 },
9 settings::{color::Color, CellOption, TableOption},
10};
11
12/// BorderColored represents a colored border of a Cell.
13///
14/// ```rust,no_run
15/// # use tabled::{settings::{style::BorderColor, Style, Color, object::Rows, Modify}, Table};
16/// #
17/// # let data: Vec<&'static str> = Vec::new();
18/// #
19/// let table = Table::new(&data)
20/// .with(Style::ascii())
21/// .with(Modify::new(Rows::single(0)).with(BorderColor::default().top(Color::FG_RED)));
22/// ```
23#[derive(Debug, Clone, Default, Eq, PartialEq)]
24pub struct BorderColor(Border<AnsiColor<'static>>);
25
26impl BorderColor {
27 /// This function constructs a cell borders with all sides set.
28 #[allow(clippy::too_many_arguments)]
29 pub fn full(
30 top: Color,
31 bottom: Color,
32 left: Color,
33 right: Color,
34 top_left: Color,
35 top_right: Color,
36 bottom_left: Color,
37 bottom_right: Color,
38 ) -> Self {
39 Self(Border::full(
40 top.into(),
41 bottom.into(),
42 left.into(),
43 right.into(),
44 top_left.into(),
45 top_right.into(),
46 bottom_left.into(),
47 bottom_right.into(),
48 ))
49 }
50
51 /// This function constructs a cell borders with all sides's char set to a given character.
52 /// It behaves like [`Border::full`] with the same character set to each side.
53 pub fn filled(c: Color) -> Self {
54 let c: AnsiColor<'_> = c.into();
55
56 Self(Border {
57 top: Some(c.clone()),
58 bottom: Some(c.clone()),
59 left: Some(c.clone()),
60 right: Some(c.clone()),
61 left_bottom_corner: Some(c.clone()),
62 left_top_corner: Some(c.clone()),
63 right_bottom_corner: Some(c.clone()),
64 right_top_corner: Some(c),
65 })
66 }
67
68 /// Set a top border character.
69 pub fn top(mut self, c: Color) -> Self {
70 self.0.top = Some(c.into());
71 self
72 }
73
74 /// Set a bottom border character.
75 pub fn bottom(mut self, c: Color) -> Self {
76 self.0.bottom = Some(c.into());
77 self
78 }
79
80 /// Set a left border character.
81 pub fn left(mut self, c: Color) -> Self {
82 self.0.left = Some(c.into());
83 self
84 }
85
86 /// Set a right border character.
87 pub fn right(mut self, c: Color) -> Self {
88 self.0.right = Some(c.into());
89 self
90 }
91
92 /// Set a top left intersection character.
93 pub fn corner_top_left(mut self, c: Color) -> Self {
94 self.0.left_top_corner = Some(c.into());
95 self
96 }
97
98 /// Set a top right intersection character.
99 pub fn corner_top_right(mut self, c: Color) -> Self {
100 self.0.right_top_corner = Some(c.into());
101 self
102 }
103
104 /// Set a bottom left intersection character.
105 pub fn corner_bottom_left(mut self, c: Color) -> Self {
106 self.0.left_bottom_corner = Some(c.into());
107 self
108 }
109
110 /// Set a bottom right intersection character.
111 pub fn corner_bottom_right(mut self, c: Color) -> Self {
112 self.0.right_bottom_corner = Some(c.into());
113 self
114 }
115}
116
117impl<R> CellOption<R, ColoredConfig> for BorderColor
118where
119 R: Records + ExactRecords,
120{
121 fn change(self, records: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
122 let count_rows: usize = records.count_rows();
123 let count_columns: usize = records.count_columns();
124
125 let border_color: &Border> = &self.0;
126
127 for pos: (usize, usize) in entity.iter(count_rows, count_cols:count_columns) {
128 cfg.set_border_color(pos, border_color.clone());
129 }
130 }
131}
132
133impl<R, D> TableOption<R, D, ColoredConfig> for BorderColor
134where
135 R: Records + ExactRecords,
136{
137 fn change(self, records: &mut R, cfg: &mut ColoredConfig, _: &mut D) {
138 let count_rows: usize = records.count_rows();
139 let count_columns: usize = records.count_columns();
140
141 let border_color: &Border> = &self.0;
142
143 for row: usize in 0..count_rows {
144 for col: usize in 0..count_columns {
145 cfg.set_border_color((row, col), border_color.clone());
146 }
147 }
148 }
149}
150
151impl From<BorderColor> for Border<AnsiColor<'static>> {
152 fn from(val: BorderColor) -> Self {
153 val.0
154 }
155}
156