tabprinter
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.
Features
- Versatile Table Styles:
- 14 built-in styles: Simple, Grid, FancyGrid, Clean, Round, Banner, Block, Amiga, Minimal, Compact, Markdown, Dotted, Heavy, and Neon
- Support for both ASCII and Unicode border characters
- Flexible Content Handling:
- Customizable column widths (fixed, percentage-based, or auto)
- Text alignment options (Left, Right, Center)
- Multi-line cell content support
- Automatic text wrapping
- Advanced Formatting:
- Color output support via termcolor
- Bold, italic, and underline text formatting
- Custom cell foreground and background colors
- Number formatting with decimal places and thousand separators
- Output Options:
- Direct terminal output
- String conversion for further processing
- File export capabilities
- Data Analysis Features:
- Column aggregations (sum, average, min, max)
- Group by with subtotals
- CSV file integration
- Data filtering and sorting
- Developer-Friendly:
- Intuitive and easy-to-use API
- Minimal dependencies
- Comprehensive documentation and examples
Installation
Add this to your Cargo.toml:
[]
= "0.2.1"
Usage
Here's a basic example of how to use tabprinter:
use ;
This will output:
| | | |
| | | |
| | | |
Table Styles
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 borders
To change the style, simply use a different TableStyle when creating the table:
let mut table = new;
Color Output
To use color output, use the print_color method instead of print:
use ;
use ;
// Create a cell with background color
let mut cell = new;
cell.style.background_color = Some;
cell.style.foreground_color = Some;
cell.style.bold = true;
// Print with color support
let mut stdout = stdout;
table.print_color.unwrap;
Advanced Features
Column Aggregations
You can calculate statistics on numeric columns:
let sum = table.sum_column.unwrap_or;
let avg = table.average_column.unwrap_or;
let min = table.min_column.unwrap_or;
let max = table.max_column.unwrap_or;
CSV Integration
Load tables directly from CSV files:
let table = from_csv?;
Number Formatting
Format numbers with decimal places and thousand separators:
let mut cell = new;
cell.style.decimal_places = Some;
cell.style.thousand_separator = true;
// Displays as: 1,234.57
Examples
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 Subtotals
To run an example:
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.