use std::borrow::Cow;
use papergrid::records::Records;
use crate::{Table, TableOption};
use super::Offset;
#[derive(Debug)]
pub struct BorderText<'a> {
text: Cow<'a, str>,
row: SplitLineIndex,
offset: Offset,
}
#[derive(Debug)]
enum SplitLineIndex {
First,
Last,
Line(usize),
}
impl<'a> BorderText<'a> {
pub fn new<S>(line: usize, text: S) -> Self
where
S: Into<Cow<'a, str>>,
{
Self {
text: text.into(),
row: SplitLineIndex::Line(line),
offset: Offset::Begin(0),
}
}
pub fn first<S>(text: S) -> Self
where
S: Into<Cow<'a, str>>,
{
Self {
text: text.into(),
row: SplitLineIndex::First,
offset: Offset::Begin(0),
}
}
pub fn last<S>(text: S) -> Self
where
S: Into<Cow<'a, str>>,
{
Self {
text: text.into(),
row: SplitLineIndex::Last,
offset: Offset::Begin(0),
}
}
pub fn offset(mut self, offset: Offset) -> Self {
self.offset = offset;
self
}
}
impl<'a, R> TableOption<R> for BorderText<'a>
where
R: Records,
{
fn change(&mut self, table: &mut Table<R>) {
let row = match self.row {
SplitLineIndex::First => 0,
SplitLineIndex::Last => table.shape().0,
SplitLineIndex::Line(row) => {
if row > table.shape().0 {
return;
}
row
}
};
table
.get_config_mut()
.override_split_line(row, self.text.as_ref(), self.offset.into());
}
}