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
use crate::{
    grid::config::{ColoredConfig, Entity, Position, SpannedConfig},
    grid::records::{ExactRecords, Records},
    settings::CellOption,
};

use super::Offset;

/// [`LineChar`] sets a char to a specific location on a horizontal line.
///
/// # Example
///
/// ```rust
/// use tabled::{Table, settings::{style::{Style, LineChar, Offset}, Modify, object::{Object, Rows, Columns}}};
///
/// let mut table = Table::new(["Hello World"]);
/// table
///     .with(Style::markdown())
///     .with(Modify::new(Rows::single(1))
///         .with(LineChar::horizontal(':', Offset::Begin(0)))
///         .with(LineChar::horizontal(':', Offset::End(0)))
///     )
///     .with(Modify::new((1, 0).and((1, 1))).with(LineChar::vertical('#', Offset::Begin(0))));
///
/// assert_eq!(
///     table.to_string(),
///     concat!(
///         "| &str        |\n",
///         "|:-----------:|\n",
///         "# Hello World #",
///     ),
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct LineChar {
    c: char,
    offset: Offset,
    horizontal: bool,
}

impl LineChar {
    /// Creates a [`LineChar`] which overrides horizontal line.
    pub fn horizontal(c: char, offset: impl Into<Offset>) -> Self {
        let offset = offset.into();
        let horizontal = true;

        Self {
            c,
            offset,
            horizontal,
        }
    }

    /// Creates a [`LineChar`] which overrides vertical line.
    pub fn vertical(c: char, offset: impl Into<Offset>) -> Self {
        let offset = offset.into();
        let horizontal = false;

        Self {
            c,
            offset,
            horizontal,
        }
    }
}

impl<R> CellOption<R, ColoredConfig> for LineChar
where
    R: Records + ExactRecords,
{
    fn change(self, records: &mut R, cfg: &mut ColoredConfig, entity: Entity) {
        let cells = entity.iter(records.count_rows(), records.count_columns());

        match self.horizontal {
            true => add_char_horizontal(cfg, self.c, self.offset, cells),
            false => add_char_vertical(cfg, self.c, self.offset, cells),
        }
    }
}

fn add_char_vertical<I: Iterator<Item = Position>>(
    cfg: &mut SpannedConfig,
    c: char,
    offset: Offset,
    cells: I,
) {
    let offset = offset.into();

    for pos in cells {
        cfg.set_vertical_char(pos, c, offset);
    }
}

fn add_char_horizontal<I: Iterator<Item = Position>>(
    cfg: &mut SpannedConfig,
    c: char,
    offset: Offset,
    cells: I,
) {
    let offset = offset.into();

    for pos in cells {
        cfg.set_horizontal_char(pos, c, offset);
    }
}