devashishdxt / cli-table

Rust crate for printing tables on command line.
Apache License 2.0
120 stars 9 forks source link

cli-table

Continuous Integration Crates.io Documentation License

Rust crate for printing tables on command line.

Usage

Add cli-table in your Cargo.toms's dependencies section

[dependencies]
cli-table = "0.4"

Simple usage

use cli_table::{format::Justify, print_stdout, Cell, Style, Table};

let table = vec![
    vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
    vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
    vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
]
.table()
.title(vec![
    "Name".cell().bold(true),
    "Age (in years)".cell().bold(true),
])
.bold(true);

assert!(print_stdout(table).is_ok());

Below is the output of the table we created just now:

+------------+----------------+
| Name       | Age (in years) |  <-- This row and all the borders/separators
+------------+----------------+      will appear in bold
| Tom        |             10 |
+------------+----------------+
| Jerry      |             15 |
+------------+----------------+
| Scooby Doo |             25 |
+------------+----------------+

Display trait implementation

To get a Display trait implementation of TableStruct, use display() function on the struct to get an instance of TableDisplay which implements Display trait.

use cli_table::{format::Justify, Cell, Style, Table};

let table = vec![
    vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
    vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
    vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
]
.table()
.title(vec![
    "Name".cell().bold(true),
    "Age (in years)".cell().bold(true),
])
.bold(true);

let table_display = table.display().unwrap();

println!("{}", table_display);

Below is the output of the table we created just now:

+------------+----------------+
| Name       | Age (in years) |  <-- This row and all the borders/separators
+------------+----------------+      will appear in bold
| Tom        |             10 |
+------------+----------------+
| Jerry      |             15 |
+------------+----------------+
| Scooby Doo |             25 |
+------------+----------------+

Derive macro

#[derive(Table)] can also be used to print a Vec or slice of structs as table.

use cli_table::{format::Justify, print_stdout, Table, WithTitle};

#[derive(Table)]
struct User {
    #[table(title = "ID", justify = "Justify::Right")]
    id: u64,
    #[table(title = "First Name")]
    first_name: &'static str,
    #[table(title = "Last Name")]
    last_name: &'static str,
}

let users = vec![
    User {
        id: 1,
        first_name: "Scooby",
        last_name: "Doo",
    },
    User {
        id: 2,
        first_name: "John",
        last_name: "Cena",
    },
];

assert!(print_stdout(users.with_title()).is_ok());

Below is the output of the table we created using derive macro:

+----+------------+-----------+
| ID | First Name | Last Name |  <-- This row will appear in bold
+----+------------+-----------+
|  1 | Scooby     | Doo       |
+----+------------+-----------+
|  2 | John       | Cena      |
+----+------------+-----------+

Field attributes

For more information on configurations available on derive macro, go to cli-table/examples/struct.rs.

CSV

This crate also integrates with csv crate. On enabling "csv" feature, you can use TryFrom<&mut Reader> for TableStruct trait implementation to convert csv::Reader to TableStruct.

For more information on handling CSV values, go to cli-table/examples/csv.rs.

Styling

Style of a table/cell can be modified by calling functions of [Style] trait. It is implementated by both [TableStruct] and [CellStruct].

For individually formatting each cell of a table, justify, align and padding functions can be used from CellStruct.

In addition to this, borders and separators of a table can be customized by calling border and separator functions in TableStruct. For example, to create a borderless table:

use cli_table::{Cell, Table, TableStruct, format::{Justify, Border}, print_stdout};

fn get_table() -> TableStruct {
    vec![
        vec!["Tom".cell(), 10.cell().justify(Justify::Right)],
        vec!["Jerry".cell(), 15.cell().justify(Justify::Right)],
        vec!["Scooby Doo".cell(), 20.cell().justify(Justify::Right)],
    ]
    .table()
}

let table = get_table().border(Border::builder().build()); // Attaches an empty border to the table
assert!(print_stdout(table).is_ok());

Features

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.