codeberg_cli/render/
table.rsuse comfy_table::*;
#[derive(Debug)]
pub struct TableWrapper {
table: Table,
pub(crate) max_width: Option<u16>,
}
impl Default for TableWrapper {
fn default() -> Self {
Self {
table: Table::new(),
max_width: None,
}
}
}
impl std::ops::Deref for TableWrapper {
type Target = Table;
fn deref(&self) -> &Self::Target {
&self.table
}
}
impl std::ops::DerefMut for TableWrapper {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.table
}
}
impl TableWrapper {
pub fn show(self) -> String {
let Self {
mut table,
max_width,
} = self;
let widths = max_width
.map(|width| width.min(table.column_max_content_widths().iter().sum::<u16>()))
.map(|width| (width - 2) / table.column_count() as u16)
.into_iter()
.map(Width::Fixed)
.map(ColumnConstraint::Absolute)
.cycle()
.chain(
max_width
.is_none()
.then_some(ColumnConstraint::ContentWidth)
.into_iter()
.cycle(),
);
table.set_constraints(widths);
format!("{table}")
}
}