Expand description
§lipgloss-table
A flexible and powerful table rendering library for terminal applications.
This crate provides a comprehensive table rendering system with advanced styling,
layout options, and terminal-aware text handling. It’s designed to work seamlessly
with the lipgloss
styling library to create beautiful terminal user interfaces.
§Features
- Flexible Table Construction: Build tables using a fluent builder pattern
- Advanced Styling: Apply different styles to headers, rows, and individual cells
- Border Customization: Control all aspects of table borders and separators
- Responsive Layout: Automatic width detection and content wrapping/truncation
- Height Constraints: Set maximum heights with automatic scrolling and overflow indicators
- ANSI-Aware: Proper handling of ANSI escape sequences in content
- Memory Safe: Built-in protections against memory exhaustion from malicious input
§Quick Start
use lipgloss_table::{Table, HEADER_ROW, header_row_style};
use lipgloss::{Style, Color};
// Create a simple table
let mut table = Table::new()
.headers(vec!["Name", "Age", "City"])
.row(vec!["Alice", "30", "New York"])
.row(vec!["Bob", "25", "London"])
.style_func(header_row_style);
println!("{}", table.render());
§Advanced Usage
§Custom Styling
use lipgloss_table::{Table, HEADER_ROW};
use lipgloss::{Style, Color};
let style_func = |row: i32, col: usize| {
match row {
HEADER_ROW => Style::new().bold(true).foreground(Color::from("#FFFFFF")),
_ if row % 2 == 0 => Style::new().background(Color::from("#F0F0F0")),
_ => Style::new(),
}
};
let mut table = Table::new()
.headers(vec!["Product", "Price", "Stock"])
.rows(vec![
vec!["Widget A", "$10.99", "50"],
vec!["Widget B", "$15.99", "25"],
vec!["Widget C", "$8.99", "100"],
])
.style_func(style_func)
.width(40);
println!("{}", table.render());
§Height-Constrained Tables with Scrolling
use lipgloss_table::Table;
let mut table = Table::new()
.headers(vec!["Item", "Description"])
.height(10) // Limit table to 10 lines
.offset(5); // Skip first 5 rows (scrolling)
// Add many rows...
for i in 1..=100 {
table = table.row(vec![format!("Item {}", i), "Description".to_string()]);
}
println!("{}", table.render());
println!("Table height: {}", table.compute_height());
§Predefined Style Functions
The crate includes several predefined styling functions:
default_styles
: Basic styling with no attributesheader_row_style
: Bold headers with default data rowszebra_style
: Alternating row backgrounds for better readabilityminimal_style
: Subtle styling with muted colorscolumn_style_func
: Factory for creating column-specific styles
§Integration with lipgloss
This crate is designed to work seamlessly with the lipgloss
styling library.
All styling functions receive lipgloss::Style
objects and can use the full
range of lipgloss features including colors, borders, padding, and alignment.
Re-exports§
pub use resizing::Resizer;
pub use resizing::ResizerColumn;
pub use rows::data_to_matrix;
pub use rows::Data;
pub use rows::Filter;
pub use rows::StringData;
Modules§
- resizing
- Internal module for table resizing logic and column width calculations.
- rows
- Internal module for data handling and row management.
- util
- Internal utility functions for table operations.
Structs§
- Table
- A flexible table renderer with advanced styling and layout capabilities.
Constants§
- HEADER_
ROW - HeaderRow denotes the header’s row index used when rendering headers. Use this value when looking to customize header styles in StyleFunc.
Functions§
- column_
style_ func - Creates a style function that applies column-specific styling to table cells.
- default_
styles - A basic style function that applies no formatting to any cells.
- header_
row_ style - A style function that makes header rows bold while leaving data rows unstyled.
- minimal_
style - A subtle style function that provides minimal, professional-looking table styling.
- zebra_
style - A style function that creates alternating row backgrounds (zebra striping) for improved readability.
Type Aliases§
- Boxed
Style Func - A trait object type for flexible style functions that can capture their environment.
- Style
Func - StyleFunc is the style function that determines the style of a Cell.