Tables
Automated table rendering with Unicode support and custom styling
Overview
Create beautiful ASCII/Unicode tables for terminal output
The table module provides:
- Automatic column width calculation
- Unicode box-drawing characters
- Header and data row support
- Custom alignment and padding
- Colored cells and borders
Basic Table
Create a simple table with headers and rows
Basic Table Example
use zfish::table::Table;
// Create table with headers
let mut table = Table::new(vec!["Name", "Age", "City"]);
// Add data rows
table.add_row(vec!["Alice", "25", "New York"]);
table.add_row(vec!["Bob", "30", "London"]);
table.add_row(vec!["Charlie", "28", "Tokyo"]);
// Print the table
table.print();Output
āāāāāāāāāāā¬āāāāāā¬āāāāāāāāāāā ā Name ā Age ā City ā āāāāāāāāāāā¼āāāāāā¼āāāāāāāāāā⤠ā Alice ā 25 ā New York ā ā Bob ā 30 ā London ā ā Charlie ā 28 ā Tokyo ā āāāāāāāāāāā“āāāāāā“āāāāāāāāāāā
Styled Table
Customize table appearance with colors and styles
Styled Table Example
use zfish::table::Table;
use zfish::style::Color;
// Create table with headers
let mut table = Table::new(vec!["Product", "Price", "Stock"]);
table.set_box_style(BoxStyle::Double);
// Add colored rows
table.add_row(&["Laptop", "$999", "In Stock"]);
table.add_row(&["Mouse", "$25", "Low Stock"]);
table.add_row(&["Keyboard", "$75", "Out of Stock"]);
// Set border color
table.set_border_color(Color::Blue);
table.print();Output
āāāāāāāāāāāā¬āāāāāāāā¬āāāāāāāāāāāāāāā ā Product ā Price ā Stock ā āāāāāāāāāāāā¼āāāāāāāā¼āāāāāāāāāāāāāā⤠ā Laptop ā $999 ā In Stock ā ā Mouse ā $25 ā Low Stock ā ā Keyboard ā $75 ā Out of Stock ā āāāāāāāāāāāā“āāāāāāāā“āāāāāāāāāāāāāāā
Column Alignment
Align columns left, center, or right
Column Alignment Example
use zfish::table::{Table, Alignment};
// Create table with headers
let mut table = Table::new(vec!["Item", "Quantity", "Price"]);
// Set column alignments
table.set_column_alignment(0, Alignment::Left);
table.set_column_alignment(1, Alignment::Center);
table.set_column_alignment(2, Alignment::Right);
// Add rows
table.add_row(vec!["Apple", "10", "$1.50"]);
table.add_row(vec!["Banana", "5", "$0.80"]);
table.add_row(vec!["Orange", "8", "$1.20"]);
table.print();Output
āāāāāāāāāā¬āāāāāāāāāāā¬āāāāāāāā ā Item ā Quantity ā Price ā āāāāāāāāāā¼āāāāāāāāāāā¼āāāāāāā⤠ā Apple ā 10 ā $1.50 ā ā Banana ā 5 ā $0.80 ā ā Orange ā 8 ā $1.20 ā āāāāāāāāāā“āāāāāāāāāāā“āāāāāāāā
Custom Box Style
Use different box-drawing character sets
ASCII Box Style
use zfish::table::{Table, BoxStyle};
// Create table with headers
let mut table = Table::new(vec!["Header 1", "Header 2", "Header 3"]);
// Use ASCII style instead of Unicode
table.set_box_style(BoxStyle::Ascii);
table.add_row(vec!["Data 1", "Data 2", "Data 3"]);
table.print();Output
+----------+----------+----------+ | Header 1 | Header 2 | Header 3 | +----------+----------+----------+ | Data 1 | Data 2 | Data 3 | +----------+----------+----------+
Unicode Support
Proper handling of Unicode characters and emoji
Unicode Support Example
use zfish::table::Table;
// Create table with headers
let mut table = Table::new(vec!["Emoji", "Name", "Category"]);
table.add_row(vec!["š", "Rocket", "Transport"]);
table.add_row(vec!["šØ", "Palette", "Art"]);
table.add_row(vec!["š", "Fish", "Animal"]);
table.add_row(vec!["ä½ å„½", "Hello (Chinese)", "Language"]);
table.print();Output
āāāāāāāāā¬āāāāāāāāāāāāāāāāāāā¬āāāāāāāāāāāā ā Emoji ā Name ā Category ā āāāāāāāāā¼āāāāāāāāāāāāāāāāāāā¼āāāāāāāāāāāā¤ ā š ā Rocket ā Transport ā ā šØ ā Palette ā Art ā ā š ā Fish ā Animal ā ā ä½ å„½ ā Hello (Chinese) ā Language ā āāāāāāāāā“āāāāāāāāāāāāāāāāāāā“āāāāāāāāāāāā
Manual Box Drawing
Draw custom boxes and borders
Manual Box Drawing
use zfish::table::draw_box;
use zfish::style::Color;
// Draw a colored box
draw_box(
"Hello, ZFish!",
Color::Green,
Some(60),
true
);
// Draw a simple box
draw_box(
"Simple message",
Color::Blue,
None,
false
);Output
āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā ā Hello, ZFish! ā āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā āāāāāāāāāāāāāāāāāāāā ā Simple message ā āāāāāāāāāāāāāāāāāāāā
API Reference
Available methods and options
Table Methods
new()Create a new table
add_row(&[&str])Add a row to the table
set_header_style(color)Style the header row
set_column_alignment(col, align)Set column alignment
set_border_color(color)Set border color
set_box_style(style)Set box-drawing style
print()Print the table
Box Styles
UnicodeAsciiRoundedDoubleBold