tabled/features/style/
vertical_line.rs

1use crate::{Table, TableOption};
2
3use super::Line;
4
5/// A horizontal split line which can be used to set a border.
6#[derive(Debug, Clone)]
7pub struct VerticalLine {
8    pub(crate) index: usize,
9    pub(crate) line: Option<Line>,
10}
11
12impl VerticalLine {
13    /// Creates a new horizontal split line.
14    pub const fn new(index: usize, line: Line) -> Self {
15        Self {
16            index,
17            line: Some(line),
18        }
19    }
20
21    /// Removes an existing split line by index.
22    ///
23    /// It not present or in case of index bigger than the count of columns it has no affect.
24    pub fn empty(index: usize) -> Self {
25        Self { index, line: None }
26    }
27
28    /// Sets a horizontal character.
29    pub const fn main(mut self, c: Option<char>) -> Self {
30        let mut line = match self.line {
31            Some(line) => line,
32            None => Line::empty(),
33        };
34
35        line.main = c;
36        self.line = Some(line);
37
38        self
39    }
40
41    /// Sets a vertical intersection character.
42    pub const fn intersection(mut self, c: Option<char>) -> Self {
43        let mut line = match self.line {
44            Some(line) => line,
45            None => Line::empty(),
46        };
47
48        line.intersection = c;
49        self.line = Some(line);
50
51        self
52    }
53
54    /// Sets a top character.
55    pub const fn top(mut self, c: Option<char>) -> Self {
56        let mut line = match self.line {
57            Some(line) => line,
58            None => Line::empty(),
59        };
60
61        line.connector1 = c;
62        self.line = Some(line);
63
64        self
65    }
66
67    /// Sets a bottom character.
68    pub const fn bottom(mut self, c: Option<char>) -> Self {
69        let mut line = match self.line {
70            Some(line) => line,
71            None => Line::empty(),
72        };
73
74        line.connector2 = c;
75        self.line = Some(line);
76
77        self
78    }
79
80    /// Checks if it's an empty line.
81    pub const fn is_empty(&self) -> bool {
82        match &self.line {
83            Some(l) => l.is_empty(),
84            None => true,
85        }
86    }
87}
88
89impl<R> TableOption<R> for VerticalLine {
90    fn change(&mut self, table: &mut Table<R>) {
91        match &self.line {
92            Some(line) => table
93                .get_config_mut()
94                .set_vertical_line(self.index, papergrid::VerticalLine::from(*line)),
95            None => table.get_config_mut().remove_vertical_line(self.index),
96        }
97    }
98}