#alignment

tabular

Plain text tables, aligned automatically

5 releases

0.2.0 Jan 8, 2022
0.1.4 Dec 30, 2019
0.1.2 Sep 19, 2018

#98 in Value formatting

Download history 10853/week @ 2024-06-13 10000/week @ 2024-06-20 9350/week @ 2024-06-27 7623/week @ 2024-07-04 9171/week @ 2024-07-11 8313/week @ 2024-07-18 10275/week @ 2024-07-25 7087/week @ 2024-08-01 7861/week @ 2024-08-08 8322/week @ 2024-08-15 7925/week @ 2024-08-22 7962/week @ 2024-08-29 10830/week @ 2024-09-05 10846/week @ 2024-09-12 9800/week @ 2024-09-19 8385/week @ 2024-09-26

41,262 downloads per month
Used in 32 crates (30 directly)

MIT/Apache

39KB
506 lines

tabular: plain text tables in Rust

Build Status Crates.io License: MIT License: Apache 2.0 Documentation (latest release) Documentation (main)

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_heading() and Table::add_heading() 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).

  • With the ansi-cell feature enabled, the Row::with_ansi_cell and Row::add_ansi_cell methods can be used to add cells with ANSI color codes, and still have their widths be computed correctly.

  • The Row::with_custom_width_cell and Row::add_custom_width_cell methods can be used to customize alignment precisely.

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.

Dependencies

~1.5MB
~19K SLoC