| Crates.io | tabprinter |
| lib.rs | tabprinter |
| version | 0.2.2 |
| created_at | 2024-09-16 04:21:57.624283+00 |
| updated_at | 2025-06-25 04:36:21.770532+00 |
| description | tabprinter is a Rust library for creating and printing formatted tables in the terminal. It supports various table styles and offers both color and non-color output options. |
| homepage | |
| repository | https://github.com/vschwaberow/tabprinter |
| max_upload_size | |
| id | 1375987 |
| size | 74,685 |
tabprinter is a Rust library for creating and printing formatted tables in the terminal. It supports various table styles and offers both color and non-color output options.
Add this to your Cargo.toml:
[dependencies]
tabprinter = "0.2.1"
Here's a basic example of how to use tabprinter:
use tabprinter::{Table, TableStyle, Alignment, Cell};
fn main() {
let mut table = Table::new(TableStyle::Grid);
// Define columns with headers and alignment
table.add_column("Name", Alignment::Left);
table.add_column("Age", Alignment::Right);
table.add_column("City", Alignment::Center);
// Add rows of data
table.add_row(vec![
Cell::new("Alice"),
Cell::new("30"),
Cell::new("New York"),
]);
table.add_row(vec![
Cell::new("Bob"),
Cell::new("25"),
Cell::new("Los Angeles"),
]);
// Print the table to stdout
table.print().unwrap();
}
This will output:
+------------+-------+-----------------+
| Name | Age | City |
+------------+-------+-----------------+
| Alice | 30 | New York |
| Bob | 25 | Los Angeles |
+------------+-------+-----------------+
tabprinter supports the following table styles:
Simple: No bordersGrid: ASCII bordersFancyGrid: Unicode bordersClean: Minimal bordersRound: Rounded cornersBanner: Top and bottom bannersBlock: Block-style bordersAmiga: Amiga-inspired style (color output only)Minimal: Thin bordersCompact: Compact thin bordersMarkdown: Markdown table syntaxDotted: Dotted bordersHeavy: Thick bordersNeon: Neon-style bordersTo change the style, simply use a different TableStyle when creating the table:
let mut table = Table::new(TableStyle::FancyGrid);
To use color output, use the print_color method instead of print:
use termcolor::{ColorChoice, StandardStream};
use tabprinter::{Cell, Color};
// Create a cell with background color
let mut cell = Cell::new("Colored Cell");
cell.style.background_color = Some(Color::Red);
cell.style.foreground_color = Some(Color::White);
cell.style.bold = true;
// Print with color support
let mut stdout = StandardStream::stdout(ColorChoice::Always);
table.print_color(&mut stdout).unwrap();
You can calculate statistics on numeric columns:
let sum = table.sum_column(1).unwrap_or(0.0);
let avg = table.average_column(1).unwrap_or(0.0);
let min = table.min_column(1).unwrap_or(0.0);
let max = table.max_column(1).unwrap_or(0.0);
Load tables directly from CSV files:
let table = Table::from_csv("data.csv")?;
Format numbers with decimal places and thousand separators:
let mut cell = Cell::new("1234.567");
cell.style.decimal_places = Some(2);
cell.style.thousand_separator = true;
// Displays as: 1,234.57
Check out the examples directory for more usage examples:
basic_usage.rs: Demonstrates basic table creation and printingdifferent_styles.rs: Shows all available table stylescustom_data.rs: Example of using custom data structures with tablescsv_usage.rs: Example of CSV usagecolumn_aggregations.rs: Example of Column Aggregationsgroup_by_subtotals.rs: Example of Group by SubtotalsTo run an example:
cargo run --example basic_usage
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.