[][src]Crate colonnade

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


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

colonnade = "0"


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

fn main() {
    // text to put in tabular form
    let text = 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(0, 8); // the first column should have a left margin 8 spaces wide
    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
                                                                 though that may

                           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.



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



Alignments one can apply to columns of text.


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