//! Column-aligned table widget. //! //! `Table` tracks per-column widths as rows are pushed, then renders rows as //! styled [`Line`](crate::Line) values or directly as a widget. It is intended //! for compact operational views where stable alignment matters more than //! complex cell layout. //! //! Example: //! ``` //! use rustik_tui_core::{Constraint, Table, TableOptions, Widget}; //! //! let mut t = Table::new(TableOptions::default()); //! t.push(["pest", "biological control"]); //! t.push(["aphid", "lacewing"]); //! t.push(["spider mite", "ladybug"]); //! //! let out = t //! .display(Constraint::UNBOUNDED) //! .lines() //! .map(str::trim_end) //! .collect::>() //! .join("\n") //! + "\n"; //! assert_eq!( //! out, //! "pest biological control\naphid lacewing\nspider mite ladybug\n", //! ); //! # Ok::<(), Box>(()) //! ``` //! Output: //! ``` plain //! pest biological control //! aphid lacewing //! spider mite ladybug //! ``` use std::{fmt, num::NonZeroUsize}; use super::cell::Text; use super::{Border, Buffer, Constraint, Line, Size, Style, Widget, colors}; /// Rendering options for a table. #[derive(Debug)] pub struct TableOptions { /// Whether the table should be allowed to overflow. pub overflow: bool, /// Horizontal spacing between table cells. pub spacing: usize, /// Table border. pub border: Option, } impl Default for TableOptions { fn default() -> Self { Self { overflow: false, spacing: 1, border: None, } } } impl TableOptions { /// Returns default options with a faint border enabled. pub fn bordered() -> Self { Self { border: Some(Border::new(colors::FAINT)), spacing: 3, ..Self::default() } } } /// Responsive behavior for one table column. #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub struct Column { /// Relative share of horizontal space assigned to this column. flex: usize, /// Order in which this column disappears as space contracts. collapse: Option, } impl Column { /// Returns a column that uses its intrinsic content width. pub const fn auto() -> Self { Self { flex: 0, collapse: None, } } /// Returns a column that receives this relative share of available width. pub const fn flex(flex: usize) -> Self { Self { flex, collapse: None, } } /// Returns an intrinsic-width column that disappears at `order`. pub const fn collapse(order: usize) -> Self { Self { flex: 0, collapse: Some(order), } } } /// Internal row representation for table rendering. #[derive(Debug)] enum Row { /// Header row. Header([T; W]), /// Data row. Data { /// Row cells. cells: [T; W], /// Optional row-wide style applied to spacing and trailing pad cells. style: Option