Crate tablestream[][src]

Expand description

TableStream is a tool for streaming tables out to the terminal. It will buffer some number of rows before first output and try to automatically determine appropriate widths for each column.

// Some sample data we want to show:
struct City {
    name: String,
    country: String,
    population: u32,
}
 
impl City {
    fn new(name: &str, country: &str, population: u32) -> Self {
        Self { name: name.to_string(), country: country.to_string(), population, }
    }
}
 
fn largest_cities() -> Vec<City> {
   vec![
       City::new("Shanghai", "China", 24_150_000),
       City::new("Beijing", "China", 21_700_000),
       City::new("Lagos", "Nigeria", 21_320_000),
   ]
}

let mut out = io::stdout();
let mut stream = Stream::new(&mut out, vec![
    // There are three different ways to specify which data to show in each column.
    // 1. A closure that takes a formatter, and a reference to your type, and writes it out.
    Column::new(|f, c: &City| write!(f, "{}", &c.name)).header("City"),
      
    // 2. Or we can use a shortcut macro to just show a single field:
    // (It must implement fmt::Display)
    col!(City: .country).header("Country"),

    // 3. You can optionally specify a formatter:
    // (Note: don't use padding/alignment in your formatters. TableStream will do that for you.)
    col!(City: "{:.2e}", .population).header("Population"),
]);

// Stream our data:
for city in largest_cities() {
   stream.row(city)?;
}
 
stream.finish()?;
  

Macros

Create a new column. Saves some boilerplate vs. Column::new(...).

Structs

Configure how we want to display a single column.

Allows printing rows of data to some io::Write.