[][src]Crate colonnade

This crate provides a library for displaying tabular data in, for instance, a command line app.

Usage

Colonnade is on crates.io and can be used by adding colonnade to your dependencies in your project's Cargo.toml.

[dependencies]
colonnade = "0"

Example

extern crate colonnade;
use colonnade::{Alignment, Colonnade};

#[allow(unused_must_use)]
fn main() {
    // text to put in tabular form
    let text = vec![
        vec![
            "Colonnade lets you format text in columns.",
            "As you can see, it supports text alignment, viewport width, and column widths.",
            "It doesn't yet support color codes or other formatting, though that may come.",
        ],
        vec!["", "Two or more rows of columns makes a table.", ""],
    ];

    // 3 columns of text in an 80-character viewport
    let mut colonnade = Colonnade::new(3, 80).unwrap();

    // configure the table a bit
    colonnade.left_margin_all(4);
    colonnade.left_margin(0, 8); // the first column should have a left margin 8 spaces wide
    colonnade.fixed_width_all(15);
    colonnade.clear_limits(1); // the central column has no fixed size limits
    colonnade.alignment(0, Alignment::Right);
    colonnade.alignment(1, Alignment::Center);
    colonnade.alignment(2, Alignment::Left);
    colonnade.spaces_between_rows(1); // add a blank link between rows

    // now print out the table
    for line in colonnade.tabulate(&text).unwrap() {
        println!("{}", line);
    }
}

which produces

         Colonnade lets     As you can see, it supports text     It doesn't yet
        you format text      alignment, viewport width, and      support color
            in columns.              column widths.              codes or other
                                                                 formatting,
                                                                 though that may
                                                                 come.

                           Two or more rows of columns makes
                                        a table.

If Colonnade doesn't have enough space in a column to fit the text, it will attempt to wrap it, splitting on whitespace. If this is not possible because a word in the text is so long it does not fit in the column, it will fit as much as it can, splitting mid-word and marking the split with a hyphen (unless the column is only one character wide).

To control the layout you can specify minimum and maximum column widths and column priorities. If the columns differ in priority, lower priority, higher priority number, columns will get wrapped first.

Structs

Colonnade

A struct holding formatting information. This is the object which tabulates data.

Enums

Alignment

Alignments one can apply to columns of text.

ColonnadeError

All the things that can go wrong when laying out tabular data.