#tables #formatting #alignment #text #cli

tabular

Plain text tables, aligned automatically

4 releases

✓ Uses Rust 2018 edition

0.1.4 Dec 30, 2019
0.1.3 Dec 30, 2019
0.1.2 Sep 19, 2018
0.1.1 Sep 18, 2018
0.1.0 Sep 18, 2018

#14 in Value formatting

Download history 6/week @ 2019-10-14 10/week @ 2019-10-21 105/week @ 2019-10-28 26/week @ 2019-11-04 7/week @ 2019-11-11 13/week @ 2019-11-18 30/week @ 2019-11-25 30/week @ 2019-12-02 140/week @ 2019-12-09 28/week @ 2019-12-16 9/week @ 2019-12-23 27/week @ 2019-12-30 24/week @ 2020-01-06 124/week @ 2020-01-13 33/week @ 2020-01-20

138 downloads per month
Used in 6 crates

MIT/Apache

33KB
422 lines

tabular: plain text tables in Rust

Build Status Crates.io License: MIT License: Apache 2.0

Builds plain, automatically-aligned tables of monospaced text. This is basically what you want if you are implementing ls.

Example

use tabular::{Table, Row};
use std::path::Path;

fn ls(dir: &Path) -> ::std::io::Result<()> {
    let mut table = Table::new("{:>}  {:<}{:<}  {:<}");
    for entry_result in ::std::fs::read_dir(dir)? {
        let entry    = entry_result?;
        let metadata = entry.metadata()?;

        table.add_row(Row::new()
             .with_cell(metadata.len())
             .with_cell(if metadata.permissions().readonly() {"r"} else {""})
             .with_cell(if metadata.is_dir() {"d"} else {""})
             .with_cell(entry.path().display()));
    }

    print!("{}", table);

    Ok(())
}

ls(Path::new(&"target")).unwrap();

produces something like

1198     target/.rustc_info.json
1120  d  target/doc
 192  d  target/package
1056  d  target/debug

Other features

  • The Table::with_header() and Table::add_header() methods add lines that span all columns.

  • The row! macro builds a row with a fixed number of columns using less syntax.

  • The Table::set_line_end() method allows changing the line ending to include a carriage return (or whatever you want).

Usage

It's on crates.io, so you can add

[dependencies]
tabular = "0.1.4"

to your Cargo.toml.

Feature unicode-width is enabled be default; it depends on the unicode-width crate. You can turn it off with:

[dependencies]
tabular = { version = "0.1.4", default-features = false }

Note that without unicode-width, alignment will be based on the count of the std::str::Chars iterator.

This crate supports Rust version 1.31.0 and later.

See also

You may also want:

  • text-tables – This is more automatic than tabular. You give it an array of arrays, it renders a nice table with borders. Tabular doesn't do borders.

  • prettytable — This has an API more similar to tabular’s in terms of building a table, but it does a lot more, including, color, borders, and CSV import.

Dependencies

~36KB