[][src]Struct tabular::Row

pub struct Row(_);

Type for building a Table row.

Make a new one with Row::new(), then add to it with Row::with_cell(). Or make a complete one with the row!() macro or Row::from_cells().

Examples

#[macro_use(row)]
extern crate tabular;

let table = tabular::Table::new("{:>}  ({:<}) {:<}")
    .with_row(row!(1, "I", "one"))
    .with_row(row!(5, "V", "five"))
    .with_row(row!(10, "X", "ten"))
    .with_row(row!(50, "L", "fifty"))
    .with_row(row!(100, "C", "one-hundred"));

assert_eq!( format!("\n{}", table),
            r#"
  1  (I) one
  5  (V) five
 10  (X) ten
 50  (L) fifty
100  (C) one-hundred
"# );

Methods

impl Row[src]

pub fn new() -> Self[src]

Makes a new, empty table row.

pub fn with_cell<S: Display>(self, value: S) -> Self[src]

Adds a cell to this table row.

Examples

struct DirEntry {
    size:         usize,
    is_directory: bool,
    name:         String,
}

impl DirEntry {
    fn to_row(&self) -> tabular::Row {
        tabular::Row::new()
            .with_cell(self.size)
            .with_cell(if self.is_directory { "d" } else { "" })
            .with_cell(&self.name)
    }
}

pub fn add_cell<S: Display>(&mut self, value: S) -> &mut Self[src]

Adds a cell to this table row.

This performs the same work as with_cell, but it's is convenient for adding cells in a loop without having to reassign the row each time. See the example for len.

pub fn from_cells<S, I>(values: I) -> Self where
    S: Into<String>,
    I: IntoIterator<Item = S>, 
[src]

Builds a row from an iterator over strings.

Examples

use std::fmt::Display;

struct Matrix<'a, T: 'a> {
    width:  usize,
    height: usize,
    data:   &'a [T],
}

impl<'a, T: Display> Display for Matrix<'a, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        let ncols = self.width;
        let row_spec: String =
             std::iter::repeat("{:>} ".chars()).take(ncols).flat_map(|x| x).collect();

        let mut table = Table::new(row_spec.trim_end());

        for row_index in 0 .. self.height {
            table.add_row(Row::from_cells(
                self.data[row_index * ncols ..]
                    .iter().take(ncols)
                    .map(|elt: &T| elt.to_string())));
        }

        write!(f, "{}", table)
    }
}

print!("{}", Matrix {
    width:   3,
    height:  2,
    data:    &[1, 23, 456, 7890, 12345, 678901],
});

pub fn len(&self) -> usize[src]

The number of cells in this row.

Examples

It's probably not actually useful, because you are unlikely to come upon a row whose size you don't already know. But it's useful for stating Table::add_row's invariant.

fn print_ragged_matrix<T: Display>(matrix: &[&[T]]) {
   let ncols = matrix.iter().map(|row| row.len()).max().unwrap_or(0);

   let mut row_spec = String::with_capacity(5 * ncols);
   for _ in 0 .. ncols {
       row_spec.push_str("{:>} ");
   }

   let mut table = Table::new(row_spec.trim_end());

   for row in matrix {
       let mut table_row = Row::from_cells(row.iter().map(ToString::to_string));

       // Don't remember how to count or subtract but I'll get there eventually.
       while table_row.len() < table.column_count() {
           table_row.add_cell("");
       }
   }

   print!("{}", table);
}

print_ragged_matrix(&[&[1, 2, 3, 4, 5], &[12, 23, 34], &[123, 234], &[1234]]);

pub fn is_empty(&self) -> bool[src]

Whether the row is empty

Trait Implementations

impl Clone for Row[src]

impl Debug for Row[src]

impl Default for Row[src]

Auto Trait Implementations

impl RefUnwindSafe for Row

impl Send for Row

impl Sync for Row

impl Unpin for Row

impl UnwindSafe for Row

Blanket Implementations

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

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

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

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 = Infallible

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.