use crate::error::GraphError;
use prettytable::{Attr, Cell, Row, Table, color, format};
use std::io::{self, Write};
pub(super) struct Section {
title: Option<String>,
table: Table,
}
impl Section {
#[must_use]
pub(super) fn titled(title: &str, table: Table) -> Self {
Self {
title: Some(title.to_string()),
table,
}
}
#[must_use]
pub(super) fn untitled(table: Table) -> Self {
Self { title: None, table }
}
}
#[must_use]
pub(super) fn boxed_table() -> Table {
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_BOX_CHARS);
table
}
#[must_use]
pub(super) fn header_row(headers: &[&str], colour: color::Color) -> Row {
Row::new(
headers
.iter()
.map(|header| Cell::new(header).with_style(Attr::ForegroundColor(colour)))
.collect(),
)
}
#[must_use]
pub(super) fn render(sections: &[Section]) -> String {
let mut out = String::new();
for section in sections {
if let Some(title) = §ion.title {
out.push_str(title);
out.push('\n');
}
out.push_str(§ion.table.to_string());
}
out
}
#[inline(never)]
pub(super) fn print(sections: &[Section]) -> Result<(), GraphError> {
for section in sections {
if let Some(title) = §ion.title {
let mut stdout = io::stdout().lock();
writeln!(stdout, "{title}")?;
stdout.flush()?;
}
section.table.print_tty(false)?;
}
Ok(())
}