[][src]Struct colonnade::Colonnade

pub struct Colonnade { /* fields omitted */ }

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

Methods

impl Colonnade[src]

pub fn new(columns: usize, width: usize) -> Result<Colonnade, ColonnadeError>[src]

Construct a Colonnade with default values: left alignment, no column size constraints, no blank lines between rows, 1 space margin between columns.

Arguments

  • columns - The number of columns of data to expect
  • width - Viewport size in characters

Errors

  • ColonnadeError::InsufficientSpace - the viewport isn't wide enough for the columns and their margins

Example

let colonnade = Colonnade::new(4, 100);

pub fn tabulate<T>(
    &mut self,
    table: &Vec<Vec<T>>
) -> Result<Vec<String>, ColonnadeError> where
    T: ToString
[src]

Converts the raw data in table into a vector of strings representing the data in tabular form. Blank lines will be zero-width rather than full-width lines of whitespace.

If you need finer control over the text, for instance, if you want to add color codes, see macerate.

Arguments

  • table - The data to display.

Errors

Any errors of lay_out. If the data has already been laid out, this method will throw no errors.

Example

let mut colonnade = Colonnade::new(4, 100)?;
let data = vec![vec!["some", "words", "for", "example"]];
let lines = colonnade.tabulate(&data)?;

pub fn macerate<T>(
    &mut self,
    table: &Vec<Vec<T>>
) -> Result<Vec<Vec<(String, String)>>, ColonnadeError> where
    T: ToString
[src]

Chew up the text into bits suitable for piecemeal layout.

More specifically, macerate digests the raw data in table into a vector of vectors of (String, String) tuples representing the data in tabular form. Each tuple consists of a whitespace left margin and the contents of a column. Separator lines will consist of a margin and text tuple where the text is zero-width and the "margin" is as wide as the table.

Maceration is useful if you wish to insert color codes to colorize the data or otherwise manipulate the data post-layout. If you don't want to do this, see tabulate.

Arguments

  • table - The data to display.

Errors

Any errors of lay_out. If the data has already been laid out, this method will throw no errors.

Example

extern crate term;
// ... [some details omitted]
// 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 natively support color codes, but it is easy enough to combine with a crate like term.",
    ],
    vec!["", "Two or more rows of columns makes a table.", ""],
];
let mut colonnade = Colonnade::new(3, 80).unwrap();
// configure the table a bit
colonnade.left_margin_all(4);
colonnade.left_margin(0, 8);
colonnade.fixed_width_all(15);
colonnade.clear_limits(1);
colonnade.alignment(0, Alignment::Right);
colonnade.alignment(1, Alignment::Center);
colonnade.alignment(2, Alignment::Left);
colonnade.spaces_between_rows(1);
// if the text is in colored cells, you will probably want some padding
colonnade.padding_all(1);

// now print out the table
let mut t = term::stdout().unwrap();
for line in colonnade.macerate(&text)? {
    for (i, (margin, text)) in line.iter().enumerate() {
        write!(t, "{}", margin);
        let background_color = if i % 2 == 0 {
            term::color::WHITE
        } else {
            term::color::BLACK
        };
        let foreground_color = match i % 3 {
            1 => term::color::GREEN,
            2 => term::color::RED,
            _ => term::color::BLUE,
        };
        t.bg(background_color)?;
        t.fg(foreground_color)?;
        write!(t, "{}", text)?;
        t.reset();
    }
    println!();
}

pub fn lay_out<T>(&mut self, table: &Vec<Vec<T>>) -> Result<(), ColonnadeError> where
    T: ToString
[src]

Determine column widths given data.

Normally you do not need to call this method because it is called when you tabulate the first batch of data. However, this initial layout will then be used for every subsequent batch of data regardless of its size. If you want to re-flow the table to better fit the new data, you acn call layout.

Arguments

  • table - The data to display.

Errors

  • ColonnadeError::InconsistentColumns - The number of columns in some row of table does not match the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
let data = vec![vec!["some", "words", "for", "example"]];
let lines = colonnade.tabulate(&data)?;
// ... later
let data = vec![vec!["a very different, wider", "set of", "words that won't fit comfortably in the old layout"]];
// reflow table
colonnade.lay_out(&data)?;
let lines = colonnade.tabulate(&data)?;

pub fn spaces_between_rows(&mut self, n: usize)[src]

Specify a number of blank lines to insert between table rows.

Arguments

  • n - A number of spaces.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// we want rows to be separated by a single blank line
colonnade.spaces_between_rows(1);

pub fn priority_all(&mut self, priority: usize)[src]

Assign the same priority to all columns. By default, all columns have the lowest priority.

Priority determines the order in which columns give up space when the viewport lacks sufficient space to display all columns without wrapping. Lower priority columns give up space first.

Arguments

  • priority - The common priority. Lower numbers confer higher priority; 0 is the highest priority.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign all columns the highest priority
colonnade.priority_all(0);
// now demote the last column
colonnade.priority(3, 1);

pub fn priority(
    &mut self,
    index: usize,
    priority: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular priority to a particular column.

Priority determines the order in which columns give up space when the viewport lacks sufficient space to display all columns without wrapping. Lower priority columns give up space first.

Arguments

  • index - Which column to which you wish to assign priority.
  • priority - The column's priority. Lower numbers confer higher priority; 0 is the highest priority.

Error

  • ColonnadeError::OutOfBounds - The index is beyond the bounds of the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign all columns the highest priority
colonnade.priority_all(0);
// now demote the last column
colonnade.priority(3, 1)?;

pub fn max_width_all(&mut self, max_width: usize) -> Result<(), ColonnadeError>[src]

Assign the same maximum width to all columns. By default columns have no maximum width.

Arguments

  • max_width - The common maximum width.

Errors

  • ColonnadeError::MinGreaterThanMax - Assigning a maximum width in conflict with some assigned minimum width.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign all columns a maximum width of 20
colonnade.max_width_all(20)?;
// at most we will now use only 83 of the characters provided by the viewport (until we mess with margins)

pub fn max_width(
    &mut self,
    index: usize,
    max_width: usize
) -> Result<(), ColonnadeError>
[src]

Assign the same maximum width to all columns. By default columns have no maximum width.

Arguments

  • max_width - The common maximum width.

Errors

  • ColonnadeError::MinGreaterThanMax - Assigning a maximum width in conflict with some assigned minimum width.
  • ColonnadeError::OutOfBounds - Attemping to assign a maximum width to a column that does not exist.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign the first column a maximum width of 20
colonnade.max_width(0, 20)?;

pub fn min_width_all(&mut self, min_width: usize) -> Result<(), ColonnadeError>[src]

Assign the same minimum width to all columns. By default columns have no minimum width.

Arguments

  • min_width - The common minimum width.

Errors

  • ColonnadeError::MinGreaterThanMax - Assigning a maximum width in conflict with some assigned minimum width.
  • ColonnadeError::InsufficientSpace - Assigning this minimum width means the columns require more space than the viewport provides.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign all columns a minimum width of 20
colonnade.min_width_all(20)?;
// we will now use at least 83 of the characters provided by the viewport (until we mess with margins)

pub fn min_width(
    &mut self,
    index: usize,
    min_width: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular minimum width to a particular column. By default columns have no minimum width.

Arguments

  • index - The column to which we wish to assign a minimum width.
  • min_width - The common minimum width.

Errors

  • ColonnadeError::MinGreaterThanMax - Assigning a maximum width in conflict with some assigned minimum width.
  • ColonnadeError::InsufficientSpace - Assigning this minimum width means the columns require more space than the viewport provides.
  • ColonnadeError::OutOfBounds - The index is outside the columns in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign the first column a minimum width of 20
colonnade.min_width(0, 20)?;

pub fn fixed_width_all(&mut self, width: usize) -> Result<(), ColonnadeError>[src]

Assign the same maximum and minimum width to all columns. By default columns have neither a maximum nor a minimum width.

Arguments

  • width - The common width.

Errors

This method is a convenience method which assigns all columns the same maximum and minimum width. Therefore the errors thrown are those thrown by max_width_all and min_width_all.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign all columns a width of 20
colonnade.fixed_width_all(20)?;
// we will now use at exactly 83 of the characters provided by the viewport (until we mess with margins)

pub fn fixed_width(
    &mut self,
    index: usize,
    min_width: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular maximum and minimum width to a particular column. By default columns have neither a maximum nor a minimum width.

Arguments

  • index - The column to configure.
  • width - The common width.

Errors

This method is a convenience method which assigns the column in question the same maximum and minimum width. Therefore the errors thrown are those thrown by max_width and min_width.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign the first column a width of 20
colonnade.fixed_width(0, 20)?;

pub fn clear_limits_all(&mut self)[src]

Remove any maximum or minimum column widths.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// assign all columns a width of 20
colonnade.fixed_width_all(20)?;
// later ...
colonnade.clear_limits_all();

pub fn clear_limits(&mut self, index: usize) -> Result<(), ColonnadeError>[src]

Remove maximum or minimum column widths from a particular column.

Errors

  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// initially assign all columns a width of 20
colonnade.fixed_width_all(20);
// but we want the first column to be flexible
colonnade.clear_limits(0)?;

pub fn alignment_all(&mut self, alignment: Alignment)[src]

Assign all columns the same alignment. The default alignment is left.

Arguments

  • alignment - The desired alignment.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// all columns should be right-aligned (they're numeric)
colonnade.alignment_all(Alignment::Right);

pub fn alignment(
    &mut self,
    index: usize,
    alignment: Alignment
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular alignment. The default alignment is left.

Arguments

  • index - The column to modify.
  • alignment - The desired alignment.

Errors

  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
// the first column should be right-aligned (it's numeric)
colonnade.alignment(0, Alignment::Right)?;

pub fn left_margin_all(
    &mut self,
    left_margin: usize
) -> Result<(), ColonnadeError>
[src]

Assign all columns the same left margin. The left margin is a number of blank spaces before the content of the column. By default the first column has a left margin of 0 and the other columns have a left margin of 1.

Arguments

  • left_margin - The width in blank spaces of the desired margin.

Errors

  • ColonnadeError::InsufficientSpace - This margin will require more space than is available in the viewport.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.left_margin_all(2)?;

pub fn left_margin(
    &mut self,
    index: usize,
    left_margin: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular left margin. The left margin is a number of blank spaces before the content of the column. By default the first column has a left margin of 0 and the other columns have a left margin of 1.

Arguments

  • index - The column to configure.
  • left_margin - The width in blank spaces of the desired margin.

Errors

  • ColonnadeError::InsufficientSpace - This margin will require more space than is available in the viewport.
  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.left_margin(0, 2)?;

pub fn padding_all(&mut self, padding: usize) -> Result<(), ColonnadeError>[src]

Assign all columns the same padding. The padding is a number of blank spaces before and after the contents of the column and a number of blank lines above and below it. By default the padding is 0. You most likely don't want any padding unless you are colorizing the text -- text immediately after color transitions is more difficult to read and less aesthetically pleasing.

Arguments

  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This padding will require more space than is available in the viewport.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_all(1)?;

pub fn padding(
    &mut self,
    index: usize,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular padding.

See padding_all.

Arguments

  • index - The column to configure.
  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This margin will require more space than is available in the viewport.
  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding(0, 1)?;

pub fn padding_horizontal_all(
    &mut self,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign all columns the same horizontal padding -- space before and after the column's text.

See padding_all.

Arguments

  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This padding will require more space than is available in the viewport.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_horizontal_all(1)?;

pub fn padding_horizontal(
    &mut self,
    index: usize,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular horizontal padding -- space before and after the column's text.

See padding_all.

Arguments

  • index - The column to configure.
  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This margin will require more space than is available in the viewport.
  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_horizontal(0, 1)?;

pub fn padding_left_all(&mut self, padding: usize) -> Result<(), ColonnadeError>[src]

Assign all columns the same left padding -- space before the column's text.

See padding_all.

Arguments

  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This padding will require more space than is available in the viewport.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_left_all(1)?;

pub fn padding_left(
    &mut self,
    index: usize,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular left padding -- space before the column's text.

See padding_all.

Arguments

  • index - The column to configure.
  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This margin will require more space than is available in the viewport.
  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_left(0, 1)?;

pub fn padding_right_all(
    &mut self,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign all columns the same right padding -- space after the column's text.

See padding_all.

Arguments

  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This padding will require more space than is available in the viewport.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_right_all(1)?;

pub fn padding_right(
    &mut self,
    index: usize,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular right padding -- space after the column's text.

See padding_all.

Arguments

  • index - The column to configure.
  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::InsufficientSpace - This margin will require more space than is available in the viewport.
  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_right(0, 1)?;

pub fn padding_vertical_all(&mut self, padding: usize)[src]

Assign all columns the same vertical padding -- blank lines before and after the column's text.

See padding_all.

Arguments

  • padding - The width in blank spaces/lines of the desired padding.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_vertical_all(1);

pub fn padding_vertical(
    &mut self,
    index: usize,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular vertical padding -- blank lines before and after the column's text.

See padding_all.

Arguments

  • index - The column to configure.
  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_vertical(0, 1)?;

pub fn padding_top_all(&mut self, padding: usize)[src]

Assign all columns the same top padding -- blank lines before the column's text.

See padding_all.

Arguments

  • padding - The width in blank spaces/lines of the desired padding.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_top_all(1);

pub fn padding_top(
    &mut self,
    index: usize,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular top padding -- blank lines before the column's text.

See padding_all.

Arguments

  • index - The column to configure.
  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_top(0, 1)?;

pub fn padding_bottom_all(&mut self, padding: usize)[src]

Assign all columns the same bottom padding -- blank lines after the column's text.

See padding_all.

Arguments

  • padding - The width in blank spaces/lines of the desired padding.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_bottom_all(1);

pub fn padding_bottom(
    &mut self,
    index: usize,
    padding: usize
) -> Result<(), ColonnadeError>
[src]

Assign a particular column a particular bottom padding -- blank lines after the column's text.

See padding_all.

Arguments

  • index - The column to configure.
  • padding - The width in blank spaces/lines of the desired padding.

Errors

  • ColonnadeError::OutOfBounds - The column specified does not exist in the spec.

Example

let mut colonnade = Colonnade::new(4, 100)?;
colonnade.padding_bottom(0, 1)?;

Trait Implementations

impl Clone for Colonnade[src]

impl Debug for Colonnade[src]

Auto Trait Implementations

Blanket Implementations

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = !

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]