Crate tabular[][src]

Expand description

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

Usage

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

[dependencies]
tabular = "0.2.0"

to your Cargo.toml.

Features

  • unicode-width: enabled by default; depends on the unicode-width crate.

    With the unicode-width feature enabled, default alignment is based on [Unicode Standard Annex #11], with characters in the Ambiguous category considered to be 1 column wide.

    Without it, default alignment is based on the count of the std::str::Chars iterator.

  • ansi-cell: disabled by default; depends on the strip-ansi-escapes crate. Provides the with_ansi_cell and add_ansi_cell methods.

Minimum supported Rust version

The minimum supported Rust version (MSRV) of this crate is Rust 1.46.0. The MSRV may be bumped in a patch release.

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.

Macros

A macro for building a Row.

A macro for building a Table.

Structs

Type for building a Table row.

Builder type for constructing a formatted table.

Enums

Errors from parsing the table format string.

Type Definitions

Type alias specializing std::result::Result with this crate’s Error enum.