tabled/features/style/border_char.rs
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
use papergrid::records::Records;
use crate::{style::Offset, CellOption, Table};
/// [`BorderChar`] sets a char to a specific location on a horizontal line.
///
/// # Example
///
/// ```rust
/// use tabled::{Table, style::{Style, BorderChar, Offset}, Modify, object::Rows};
///
/// let mut table = Table::new(["Hello World"]);
/// table
/// .with(Style::markdown())
/// .with(Modify::new(Rows::single(1))
/// .with(BorderChar::horizontal(':', Offset::Begin(0)))
/// .with(BorderChar::horizontal(':', Offset::End(0)))
/// .with(BorderChar::vertical('#', Offset::Begin(0)))
/// );
///
/// assert_eq!(
/// table.to_string(),
/// concat!(
/// "| &str |\n",
/// "|:-----------:|\n",
/// "# Hello World #",
/// ),
/// );
/// ```
#[derive(Debug)]
pub struct BorderChar {
c: char,
offset: Offset,
horizontal: bool,
}
impl BorderChar {
/// Creates a [`BorderChar`] which overrides horizontal line.
pub fn horizontal(c: char, offset: Offset) -> Self {
Self {
c,
offset,
horizontal: true,
}
}
/// Creates a [`BorderChar`] which overrides vertical line.
pub fn vertical(c: char, offset: Offset) -> Self {
Self {
c,
offset,
horizontal: false,
}
}
}
impl<R> CellOption<R> for BorderChar
where
R: Records,
{
fn change_cell(&mut self, table: &mut Table<R>, entity: papergrid::Entity) {
let offset = self.offset.into();
for pos in entity.iter(table.count_rows(), table.count_rows()) {
match self.horizontal {
true => {
table
.get_config_mut()
.override_horizontal_border(pos, self.c, offset);
}
false => {
table
.get_config_mut()
.override_vertical_border(pos, self.c, offset);
}
}
}
}
}