pub struct CellsFormatter<'a, 'b, T: ?Sized> { /* private fields */ }
Expand description

Used to define columns.

  • Use column to create column.
  • Use column_with to create multi level header.
  • Use content to create shared header columns.

Implementations§

source§

impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, T>

source

pub fn column_with( &mut self, header: impl RawCell, f: impl FnOnce(&mut CellsFormatter<'_, 'b, T>) )

Define column group. Used to create multi row header.

  • header : Column group header’s cell. If horizontal alignment is not specified, it is set to the center.
  • f : A function to define columns in the group.
§Examples
use text_grid::*;
struct RowData {
    a: u32,
    b_1: u32,
    b_2: u32,
}
impl Cells for RowData {
    fn fmt(f: &mut CellsFormatter<Self>) {
        f.column("a", |s| s.a);
        f.column_with("b", |f| {
            f.column("1", |s| s.b_1);
            f.column("2", |s| s.b_2);
        });
    }
}

let rows = [
    RowData { a: 300,b_1: 10, b_2: 20 },
    RowData { a: 300,b_1: 1, b_2: 500 },
];
let g = to_grid(rows);
assert_eq!(format!("\n{g}"), r#"
  a  |    b     |
-----|----------|
     | 1  |  2  |
-----|----|-----|
 300 | 10 |  20 |
 300 |  1 | 500 |
"#);
source

pub fn content<U: Cells>(&mut self, f: impl FnOnce(&'b T) -> U)

Define column content. Used to create shared header column.

  • f : A function to obtain cells.
§Examples
use text_grid::*;
struct RowData {
    a: u32,
    b_1: u32,
    b_2: u32,
}
impl Cells for RowData {
    fn fmt(f: &mut CellsFormatter<Self>) {
        f.column("a", |s| s.a);
        f.column_with("b", |f| {
            f.content(|s| s.b_1);
            f.content(|_| " ");
            f.content(|s| s.b_2);
        });
    }
}

let rows = [
    RowData { a: 300, b_1: 10, b_2: 20 },
    RowData { a: 300, b_1: 1, b_2: 500 },
];
let g = to_grid(rows);
assert_eq!(format!("\n{g}"), r#"
  a  |   b    |
-----|--------|
 300 | 10  20 |
 300 |  1 500 |
"#);
source

pub fn column<U: Cells>( &mut self, header: impl RawCell, f: impl FnOnce(&'b T) -> U )

Define column.

  • header : Column header’s cell. If horizontal alignment is not specified, it is set to the center.
  • f : A function to obtain cell.
§Examples
use text_grid::*;
struct RowData {
    a: u32,
    b: u32,
}
impl Cells for RowData {
    fn fmt(f: &mut CellsFormatter<Self>) {
        f.column("a", |s| s.a);
        f.column("b", |s| s.b);
    }
}

let rows = [
    RowData { a: 300, b: 1 },
    RowData { a: 2, b: 200 },
];
let g = to_grid(rows);
assert_eq!(format!("\n{g}"), r#"
  a  |  b  |
-----|-----|
 300 |   1 |
   2 | 200 |
"#);
source

pub fn map<U: ?Sized>( &mut self, m: impl FnOnce(&T) -> &U ) -> CellsFormatter<'_, 'b, U>

Creates a CellsFormatter whose source value was converted.

If you want to convert to an owned value instead of a reference, use map_with instead.

source

pub fn map_with<U>( &mut self, m: impl FnOnce(&'b T) -> U, f: impl FnOnce(&mut CellsFormatter<'_, '_, U>) )

Creates a CellsFormatter whose source value was converted.

Unlike map, it can be converted to an owned value.

source

pub fn filter( &mut self, f: impl FnOnce(&T) -> bool ) -> CellsFormatter<'_, '_, T>

Creates a CellsFormatter that outputs the body cell only when the source value satisfies the condition.

source

pub fn filter_map<U>( &mut self, f: impl FnOnce(&T) -> Option<&U> ) -> CellsFormatter<'_, 'b, U>

Creates a CellsFormatter that both filters and maps.

source

pub fn filter_map_with<U>( &mut self, f: impl FnOnce(&'b T) -> Option<U>, some: impl FnOnce(&mut CellsFormatter<'_, '_, U>) )

Creates a CellsFormatter that both filters and maps.

source

pub fn try_map_with<O, E: RawCell>( &mut self, f: impl FnOnce(&'b T) -> Result<O, E>, ok: impl FnOnce(&mut CellsFormatter<'_, '_, O>) )

source

pub fn stretch(&mut self) -> CellsFormatter<'_, 'b, T>

Return CellsFormatter that generates the columns to be stretched preferentially.

See ColumnStyle::stretch for details.

source

pub fn with(&mut self, f: impl Fn(&mut Self))

Apply f to self.

source§

impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, &T>

source

pub fn unref(&mut self) -> CellsFormatter<'_, '_, T>

source§

impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, &mut T>

source

pub fn unref(&mut self) -> CellsFormatter<'_, '_, T>

Auto Trait Implementations§

§

impl<'a, 'b, T> !RefUnwindSafe for CellsFormatter<'a, 'b, T>

§

impl<'a, 'b, T> !Send for CellsFormatter<'a, 'b, T>

§

impl<'a, 'b, T> !Sync for CellsFormatter<'a, 'b, T>

§

impl<'a, 'b, T: ?Sized> Unpin for CellsFormatter<'a, 'b, T>

§

impl<'a, 'b, T> !UnwindSafe for CellsFormatter<'a, 'b, T>

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>,

§

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>,

§

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.