tabprinter

Crates.iotabprinter
lib.rstabprinter
version
sourcesrc
created_at2024-09-16 04:21:57.624283+00
updated_at2025-04-05 06:17:14.005892+00
descriptiontabprinter 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
repositoryhttps://github.com/vschwaberow/tabprinter
max_upload_size
id1375987
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Volker Schwaberow (vschwaberow)

documentation

README

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 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
  • color_output.rs: Demonstrates using colored output for tables
  • custom_formatting.rs: Shows how to apply custom formatting to table cells
  • dynamic_tables.rs: Examples of creating dynamically sized tables
  • file_export.rs: How to export tables to files instead of stdout
  • complex_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.

Commit count: 9

cargo fmt