use super::*;
impl StatusBadge {
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::Pass => BADGE_PASS,
Self::Skip => BADGE_SKIP,
Self::Fail => BADGE_FAIL,
Self::Warn => BADGE_WARN,
Self::Info => BADGE_INFO,
}
}
}
impl TerminalTable {
#[must_use]
pub fn with_headers(headers: Vec<String>) -> Self {
Self {
headers,
rows: Vec::new(),
}
}
pub fn push_row(&mut self, row: Vec<String>) {
let Self { rows, .. } = self;
rows.push(row);
}
#[must_use]
pub fn render(&self) -> String {
let headers: &[String] = self.get_headers();
let rows: &[Vec<String>] = self.get_rows();
let col_count: usize = headers.len().max(
rows.iter()
.map(|row: &Vec<String>| row.len())
.max()
.unwrap_or(0),
);
if col_count == 0 {
return String::new();
}
let mut widths: Vec<usize> = vec![0; col_count];
for (idx, cell) in headers.iter().enumerate() {
widths[idx] = widths[idx].max(cell.chars().count());
}
for row in rows.iter() {
for (idx, cell) in row.iter().enumerate() {
widths[idx] = widths[idx].max(cell.chars().count());
}
}
let mut out: String = String::new();
if !headers.is_empty() {
write_table_row(&mut out, headers, &widths);
let divider: Vec<String> = widths
.iter()
.map(|width: &usize| "-".repeat((*width).max(3)))
.collect();
write_table_row(&mut out, ÷r, &widths);
}
for row in rows.iter() {
write_table_row(&mut out, row, &widths);
}
out
}
}