use ctl_text::{BoxChars, Line, Span, Style};
#[derive(Clone, Copy, Debug)]
pub(crate) struct Border {
left: char,
fill: char,
junction: char,
right: char,
}
impl Border {
const fn new(left: char, fill: char, junction: char, right: char) -> Self {
Self {
left,
fill,
junction,
right,
}
}
pub(crate) fn line(self, widths: &[u16]) -> Line {
let mut text = String::from(self.left);
for (index, width) in widths.iter().enumerate() {
if index > 0 {
text.push(self.junction);
}
text.extend(std::iter::repeat_n(self.fill, usize::from(*width) + 2));
}
text.push(self.right);
Line::from(Span::new(text, Style::new()))
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) struct Glyphs {
pub(crate) top: Border,
pub(crate) header: Border,
pub(crate) row: Border,
pub(crate) bottom: Border,
pub(crate) outer: char,
pub(crate) inner: char,
pub(crate) rule: char,
}
const UNICODE: Glyphs = Glyphs {
top: Border::new('┌', '─', '┬', '┐'),
header: Border::new('╞', '═', '╪', '╡'),
row: Border::new('├', '╌', '┼', '┤'),
bottom: Border::new('└', '─', '┴', '┘'),
outer: '│',
inner: '┆',
rule: '─',
};
const ASCII: Glyphs = Glyphs {
top: Border::new('+', '-', '+', '+'),
header: Border::new('+', '=', '+', '+'),
row: Border::new('+', '-', '+', '+'),
bottom: Border::new('+', '-', '+', '+'),
outer: '|',
inner: '|',
rule: '-',
};
impl Glyphs {
pub(crate) fn for_chars(box_chars: BoxChars) -> Self {
if box_chars == BoxChars::Ascii {
ASCII
} else {
UNICODE
}
}
}