tabled/features/style/border_char.rs
1use papergrid::records::Records;
2
3use crate::{style::Offset, CellOption, Table};
4
5/// [`BorderChar`] sets a char to a specific location on a horizontal line.
6///
7/// # Example
8///
9/// ```rust
10/// use tabled::{Table, style::{Style, BorderChar, Offset}, Modify, object::Rows};
11///
12/// let mut table = Table::new(["Hello World"]);
13/// table
14/// .with(Style::markdown())
15/// .with(Modify::new(Rows::single(1))
16/// .with(BorderChar::horizontal(':', Offset::Begin(0)))
17/// .with(BorderChar::horizontal(':', Offset::End(0)))
18/// .with(BorderChar::vertical('#', Offset::Begin(0)))
19/// );
20///
21/// assert_eq!(
22/// table.to_string(),
23/// concat!(
24/// "| &str |\n",
25/// "|:-----------:|\n",
26/// "# Hello World #",
27/// ),
28/// );
29/// ```
30#[derive(Debug)]
31pub struct BorderChar {
32 c: char,
33 offset: Offset,
34 horizontal: bool,
35}
36
37impl BorderChar {
38 /// Creates a [`BorderChar`] which overrides horizontal line.
39 pub fn horizontal(c: char, offset: Offset) -> Self {
40 Self {
41 c,
42 offset,
43 horizontal: true,
44 }
45 }
46
47 /// Creates a [`BorderChar`] which overrides vertical line.
48 pub fn vertical(c: char, offset: Offset) -> Self {
49 Self {
50 c,
51 offset,
52 horizontal: false,
53 }
54 }
55}
56
57impl<R> CellOption<R> for BorderChar
58where
59 R: Records,
60{
61 fn change_cell(&mut self, table: &mut Table<R>, entity: papergrid::Entity) {
62 let offset = self.offset.into();
63 for pos in entity.iter(table.count_rows(), table.count_rows()) {
64 match self.horizontal {
65 true => {
66 table
67 .get_config_mut()
68 .override_horizontal_border(pos, self.c, offset);
69 }
70 false => {
71 table
72 .get_config_mut()
73 .override_vertical_border(pos, self.c, offset);
74 }
75 }
76 }
77 }
78}