use crate::cli::output::Tabular;
use crate::error::Result;
use comfy_table::{presets, Cell, ContentArrangement, Table};
pub struct TableOutput {
max_width: Option<u16>,
}
impl TableOutput {
pub fn new() -> Self {
Self { max_width: None }
}
pub fn with_max_width(mut self, width: u16) -> Self {
self.max_width = Some(width);
self
}
pub fn print<T: Tabular>(&self, value: &T) -> Result<()> {
let headers = T::headers();
let row = value.row();
let mut table = Table::new();
table.load_preset(presets::UTF8_FULL);
table.set_content_arrangement(ContentArrangement::Dynamic);
if let Some(width) = self.max_width {
table.set_width(width);
}
if headers.len() <= 4 {
for (key, val) in headers.iter().zip(row.iter()) {
table.add_row(vec![Cell::new(key), Cell::new(val)]);
}
} else {
table.set_header(&headers);
table.add_row(&row);
}
println!("{table}");
Ok(())
}
pub fn print_rows<T: Tabular>(&self, values: &[T]) -> Result<()> {
if values.is_empty() {
println!("No results");
return Ok(());
}
let mut table = Table::new();
table.load_preset(presets::UTF8_FULL);
table.set_content_arrangement(ContentArrangement::Dynamic);
if let Some(width) = self.max_width {
table.set_width(width);
}
table.set_header(&T::headers());
for value in values {
table.add_row(&value.row());
}
println!("{table}");
Ok(())
}
}
impl Default for TableOutput {
fn default() -> Self {
Self::new()
}
}