#table #printing #style #output #terminal #color #formatted

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.

3 releases

0.1.2 Sep 24, 2024
0.1.1 Sep 16, 2024
0.1.0 Sep 16, 2024

#599 in Text processing

Download history 238/week @ 2024-09-10 103/week @ 2024-09-17 207/week @ 2024-09-24 51/week @ 2024-10-01

137 downloads per month

MIT license

21KB
410 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

  • Multiple table styles: Simple, Grid, FancyGrid, Clean, Round, Banner, Block, and Amiga
  • Customizable column widths and alignments
  • Color output support (using termcolor)
  • Easy-to-use API for creating and populating tables

Installation

Add this to your Cargo.toml:

[dependencies]
tabprinter = "0.1.0"

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);
table.add_column("Name", 10, Alignment::Left);
table.add_column("Age", 5, Alignment::Right);
table.add_column("City", 15, Alignment::Center);
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(),
]);
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 borders
  • Grid: ASCII borders
  • FancyGrid: Unicode borders
  • Clean: Minimal borders
  • Round: Rounded corners
  • Banner: Top and bottom banners
  • Block: Block-style borders
  • Amiga: 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 printing
  • different_styles.rs: Shows all available table styles
  • custom_data.rs: Example of using custom data structures with tables

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.3–8MB
~53K SLoC