| Crates.io | tabela |
| lib.rs | tabela |
| version | 0.2.0 |
| created_at | 2025-04-12 21:28:12.521135+00 |
| updated_at | 2025-04-21 00:28:43.957796+00 |
| description | Pretty tables for the terminal |
| homepage | https://docs.rs/tabela |
| repository | https://github.com/DarkCeptor44/handy-rs |
| max_upload_size | |
| id | 1631265 |
| size | 38,625 |
tabela (Portuguese for "table") is a Rust crate that provides a simple and easy-to-use way to display tabular data in the terminal, with the ability to add colors, styles and alignment to each cell.
I decided to write it because I found myself repeating the same table code over and over again in my projects, which consists of iterating through the data and figuring out the widths of each column based on the longest string in each column, including the header if provided, then writing the headers with correct width to a string, then iterating through the data again and writing each cell with the correct width like format!("{:<width1$} {:<width2$}", row.field1, row.field2).
&[&R] instead of Vec<R>.as_row method that returns a vector of cells. For example if you have a data of type Vec<Person> you'd have to implement the Row trait for &Person, refer to the example below.V that implements the Display trait, as well as the color (optional), style (optional) and alignment (left by default).colored::Color.Bold, Italic or Dimmed.Left, Center or Right.Add this to your Cargo.toml:
[dependencies]
tabela = "^0.2"
Or install it with cargo add tabela.
use tabela::{Alignment, Cell, Color, Row, Table};
// row type
struct Person {
name: String,
age: u8,
}
// row implementation
impl Row for &Person {
fn as_row(&self) -> Vec<Cell> {
vec![
self.name.clone().into(),
// you'd use `Cell::new(&self.name)` instead to avoid cloning a string and to add color/style/change alignment
Cell::new(self.age).with_color(Color::Cyan).with_alignment(Alignment::Center),
// adds the age field with cyan color and center alignment,
// since `u8` already has a `Display` implementation you
// don't need to do anything, if something doesn't have a `Display` impl
// you can manually turn it to a `String` or `&str` and feed that into `Cell::new`
]
}
}
let data = [
Person {
name: "Johnny".into(),
age: 30,
},
Person {
name: "Jane".into(),
age: 25,
},
];
let data_refs: Vec<&Person> = data.iter().collect(); // rows have to be references, in the future maybe
// support for owned rows will be added
let table = Table::new(&data_refs)
.with_header(&["Name", "Some Age"], None, Some(CellStyle::Bold), None) // adds header with bold style
.with_separator(" "); // uses two spaces as separator (personal preference)
let formatted = table.format().unwrap(); // errors can only happen in `Table::format` if the
// header length is different than the row length or
// if the other rows' length is different than the first row length
// so I wouldn't worry about using `unwrap` here
println!("{formatted}");
// output (without color/style characters):
//
// Name Some Age
// Johnny 30
// Jane 25
// (extra '\n' at the end)
Run the tests with cargo test.
TBA
This crate is distributed under the terms of the MIT license.