GridPrinter

Struct GridPrinter 

Source
pub struct GridPrinter { /* private fields */ }
Expand description

An API to easily print a two dimensional array to stdout.

§Example

use grid_printer::GridPrinter;

let cars = vec![
    vec!["Make", "Model", "Color", "Year", "Price", ],
    vec!["Ford", "Pinto", "Green", "1978", "$750.00", ],
    vec!["Toyota", "Tacoma", "Red", "2006", "$15,475.23", ],
    vec!["Lamborghini", "Diablo", "Yellow", "2001", "$238,459.99", ],
];
 
let rows = cars.len();
let cols = cars[0].len();
let printer = GridPrinter::builder(rows, cols)
    .col_spacing(4)
    .build();
printer.print(&cars);

Output:

Make           Model     Color     Year    Price
Ford           Pinto     Green     1978    $750.00
Toyota         Tacoma    Red       2006    $15,475.23
Lamborghini    Diablo    Yellow    2001    $238,459.99

Implementations§

Source§

impl GridPrinter

Source

pub fn new(rows: usize, cols: usize) -> Self

Source

pub fn builder(rows: usize, cols: usize) -> GridPrinterBuilder

Examples found in repository?
examples/cars.rs (line 13)
3fn main() {
4    let cars = vec![
5        vec!["Make", "Model", "Color", "Year", "Price", ],
6        vec!["Ford", "Pinto", "Green", "1978", "$750.00", ],
7        vec!["Toyota", "Tacoma", "Red", "2006", "$15,475.23", ],
8        vec!["Lamborghini", "Diablo", "Yellow", "2001", "$238,459.99", ],
9    ];
10
11    let rows = cars.len();
12    let cols = cars[0].len();
13    let printer = GridPrinter::builder(rows, cols)
14        .col_spacing(4)
15        .build();
16    printer.print(&cars);
17}
More examples
Hide additional examples
examples/colors.rs (line 16)
5fn main() -> Result<(), Box<dyn Error>> {
6
7    let grid = vec![
8        vec![1, 2, 3, 4, ],
9        vec![5, 6, 7, 8, ],
10        vec![9, 10, 11, 12, ],
11    ];
12
13    let rows = grid.len();
14    let cols = grid[0].len();
15    
16    let printer = GridPrinter::builder(rows, cols)
17        .col_style(0, StyleOpt::new().fg(Fg::Magenta))?
18        .col_style(1, StyleOpt::new().fg(Fg::Black).bg(Bg::BrightYellow))?
19        .col_style(2, StyleOpt::new().sgr(Sgr::StrikeThrough))?
20        .col_style(3, StyleOpt::new().fg(Fg::Cyan))?
21        .build();
22    printer.print(&grid);
23
24    Ok(())
25}
Source

pub fn print_cell( &self, cell: &str, col_idx: usize, style_opt: Option<&StyleOpt>, )

Source

pub fn print<F: Display>(&self, source: &[Vec<F>])

Examples found in repository?
examples/cars.rs (line 16)
3fn main() {
4    let cars = vec![
5        vec!["Make", "Model", "Color", "Year", "Price", ],
6        vec!["Ford", "Pinto", "Green", "1978", "$750.00", ],
7        vec!["Toyota", "Tacoma", "Red", "2006", "$15,475.23", ],
8        vec!["Lamborghini", "Diablo", "Yellow", "2001", "$238,459.99", ],
9    ];
10
11    let rows = cars.len();
12    let cols = cars[0].len();
13    let printer = GridPrinter::builder(rows, cols)
14        .col_spacing(4)
15        .build();
16    printer.print(&cars);
17}
More examples
Hide additional examples
examples/colors.rs (line 22)
5fn main() -> Result<(), Box<dyn Error>> {
6
7    let grid = vec![
8        vec![1, 2, 3, 4, ],
9        vec![5, 6, 7, 8, ],
10        vec![9, 10, 11, 12, ],
11    ];
12
13    let rows = grid.len();
14    let cols = grid[0].len();
15    
16    let printer = GridPrinter::builder(rows, cols)
17        .col_style(0, StyleOpt::new().fg(Fg::Magenta))?
18        .col_style(1, StyleOpt::new().fg(Fg::Black).bg(Bg::BrightYellow))?
19        .col_style(2, StyleOpt::new().sgr(Sgr::StrikeThrough))?
20        .col_style(3, StyleOpt::new().fg(Fg::Cyan))?
21        .build();
22    printer.print(&grid);
23
24    Ok(())
25}

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.