5 releases
0.2.1 | Apr 5, 2025 |
---|---|
0.2.0 | Feb 18, 2025 |
0.1.2 | Sep 24, 2024 |
0.1.1 | Sep 16, 2024 |
0.1.0 | Sep 16, 2024 |
#268 in Text processing
137 downloads per month
54KB
974 lines
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:
- 8 built-in styles: Simple, Grid, FancyGrid, Clean, Round, Banner, Block, and Amiga
- 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 background colors
- Output Options:
- Direct terminal output
- String conversion for further processing
- File export capabilities
- Developer-Friendly:
- Intuitive and easy-to-use API
- Minimal dependencies
- Comprehensive documentation and examples
Installation
Add this to your Cargo.toml
:
[dependencies]
tabprinter = "0.2.1"
Usage
Here's a basic example of how to use tabprinter
:
use tabprinter::{Table, TableStyle, Alignment};
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![
"Alice".to_string(),
"30".to_string(),
"New York".to_string(),
]);
table.add_row(vec![
"Bob".to_string(),
"25".to_string(),
"Los Angeles".to_string(),
]);
// Print the table to stdout
table.print().unwrap();
}
This will output:
+------------+-------+-----------------+
| Name | Age | City |
+------------+-------+-----------------+
| Alice | 30 | New York |
| Bob | 25 | Los Angeles |
+------------+-------+-----------------+
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)
To change the style, simply use a different TableStyle
when creating the table:
let mut table = Table::new(TableStyle::FancyGrid);
Color Output
To use color output, use the print_color
method instead of print
:
use termcolor::{ColorChoice, StandardStream};
let mut stdout = StandardStream::stdout(ColorChoice::Always);
table.print_color(&mut stdout).unwrap();
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 tablescolor_output.rs
: Demonstrates using colored output for tablescustom_formatting.rs
: Shows how to apply custom formatting to table cellsdynamic_tables.rs
: Examples of creating dynamically sized tablesfile_export.rs
: How to export tables to files instead of stdoutcomplex_layouts.rs
: Advanced table layout configurations
To run an example:
cargo run --example basic_usage
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.
Dependencies
~1.2–8MB
~51K SLoC