use ctl_text::{Context, Line, Measurement, Renderable, Text};
use crate::align::Align;
use crate::column::Column;
use crate::frame::{Frame, RecordStyle};
use crate::table::Table;
#[derive(Clone, Debug)]
pub struct Record {
table: Table,
}
impl Record {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn style(self, style: RecordStyle) -> Self {
Self {
table: Self::table(style).rows(self.table.into_rows()),
}
}
#[must_use]
pub fn row(self, key: impl Into<Text>, value: impl Into<Text>) -> Self {
self.push(key.into(), value.into())
}
fn push(mut self, key: Text, value: Text) -> Self {
self.table = self.table.row([key, value]);
self
}
fn table(style: RecordStyle) -> Table {
let (frame, keys) = match style {
RecordStyle::KeysRight => (Frame::Plain, Align::Right),
RecordStyle::KeysLeft => (Frame::Plain, Align::Left),
RecordStyle::Boxed => (Frame::Grid, Align::Left),
};
Table::new([Column::untitled().align(keys), Column::untitled()]).frame(frame)
}
}
impl Default for Record {
fn default() -> Self {
Self {
table: Self::table(RecordStyle::default()),
}
}
}
impl Renderable for Record {
fn measure(&self, context: &Context) -> Measurement {
self.table.measure(context)
}
fn render(&self, context: &Context) -> Vec<Line> {
self.table.render(context)
}
}