pub struct CellsFormatter<'a, T> { /* 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, T> CellsFormatter<'a, T>

source

pub fn column_with( &mut self, header: impl CellSource, f: impl FnOnce(&mut CellsFormatter<'_, 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 CellsSource 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 mut g = Grid::new();
g.push(&RowData {
    a: 300,
    b_1: 10,
    b_2: 20,
});
g.push(&RowData {
    a: 300,
    b_1: 1,
    b_2: 500,
});
assert_eq!(format!("\n{g}"), r#"
  a  |    b     |
-----|----------|
     | 1  |  2  |
-----|----|-----|
 300 | 10 |  20 |
 300 |  1 | 500 |
"#);
Examples found in repository?
examples/row_write_group.rs (lines 11-14)
9
10
11
12
13
14
15
        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);
            });
        }
More examples
Hide additional examples
examples/row_write_content.rs (lines 11-15)
9
10
11
12
13
14
15
16
        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);
            });
        }
examples/readme.rs (lines 14-17)
10
11
12
13
14
15
16
17
18
        fn fmt(f: &mut CellsFormatter<&Self>) {
            f.column("a", |&s| &s.a);
            f.column("b", |&s| s.b);
            f.column("c", |&s| cell(s.c).left());
            f.column_with("d", |f| {
                f.column("x", |&s| s.d);
                f.column("y", |&s| cells_e!("{:.2e}", s.d));
            });
        }
source

pub fn content<U: CellsSource>(&mut self, f: impl FnOnce(&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 CellsSource 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 mut g = Grid::new();
g.push(&RowData {
    a: 300,
    b_1: 10,
    b_2: 20,
});
g.push(&RowData {
    a: 300,
    b_1: 1,
    b_2: 500,
});
assert_eq!(format!("\n{g}"), r#"
  a  |   b    |
-----|--------|
 300 | 10  20 |
 300 |  1 500 |
"#);
Examples found in repository?
examples/row_write_content.rs (line 12)
9
10
11
12
13
14
15
16
        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);
            });
        }
source

pub fn column<U: CellsSource>( &mut self, header: impl CellSource, f: impl FnOnce(&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 CellsSource for RowData {
    fn fmt(f: &mut CellsFormatter<&Self>) {
        f.column("a", |s| s.a);
        f.column("b", |s| s.b);
    }
}

let mut g = Grid::new();
g.push(&RowData { a: 300, b: 1 });
g.push(&RowData { a: 2, b: 200 });
assert_eq!(format!("\n{g}"), r#"
  a  |  b  |
-----|-----|
 300 |   1 |
   2 | 200 |
"#);
Examples found in repository?
examples/grid.rs (line 9)
8
9
10
11
        fn fmt(f: &mut CellsFormatter<&Self>) {
            f.column("a", |s| s.a);
            f.column("b", |s| s.b);
        }
More examples
Hide additional examples
examples/grid_schema.rs (line 12)
10
11
12
13
14
        fn fmt(&self, f: &mut CellsFormatter<&[u32]>) {
            for i in 0..self.len {
                f.column(i, |s| s[i]);
            }
        }
examples/cell_macro.rs (line 9)
8
9
10
11
        fn fmt(f: &mut CellsFormatter<&Self>) {
            f.column("a", |&s| cell!("{:.2}", s.a).right());
            f.column("b", |&s| cell!("{:.3}", s.b).right());
        }
examples/row_write_group.rs (line 10)
9
10
11
12
13
14
15
        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);
            });
        }
examples/row_write_content.rs (line 10)
9
10
11
12
13
14
15
16
        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);
            });
        }
examples/readme.rs (line 11)
10
11
12
13
14
15
16
17
18
        fn fmt(f: &mut CellsFormatter<&Self>) {
            f.column("a", |&s| &s.a);
            f.column("b", |&s| s.b);
            f.column("c", |&s| cell(s.c).left());
            f.column_with("d", |f| {
                f.column("x", |&s| s.d);
                f.column("y", |&s| cells_e!("{:.2e}", s.d));
            });
        }
source

pub fn map<'x, U: 'x>( &'x mut self, f: impl FnOnce(&T) -> U ) -> CellsFormatter<'x, U>

Creates a CellsFormatter whose source value was converted.

source

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

Creates a CellsFormatter whose source value was converted to reference.

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<'x, U: 'x>( &'x mut self, f: impl FnOnce(&T) -> Option<U> ) -> CellsFormatter<'x, U>

Creates a CellsFormatter that both filters and maps.

source

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

Apply f to self.

source§

impl<T, E: CellSource> CellsFormatter<'_, Result<T, E>>

source

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

If Ok, output the cells defined by f. If Err, output the cell by the error value using the colspan of the cells output by f.

source§

impl<T, E: CellSource> CellsFormatter<'_, &Result<T, E>>

source

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

If Ok, output the cells defined by f. If Err, output the cell by the error value using the colspan of the cells output by f.

Auto Trait Implementations§

§

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

§

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

§

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

§

impl<'a, T> Unpin for CellsFormatter<'a, T>where T: Unpin,

§

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

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere 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 Twhere 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.