Struct text_grid::CellsFormatter
source · 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>
impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, T>
sourcepub fn column_with(
&mut self,
header: impl RawCell,
f: impl FnOnce(&mut CellsFormatter<'_, 'b, T>)
)
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 |
"#);
sourcepub fn content<U: Cells>(&mut self, f: impl FnOnce(&'b T) -> U)
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 |
"#);
sourcepub fn column<U: Cells>(
&mut self,
header: impl RawCell,
f: impl FnOnce(&'b T) -> U
)
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 |
"#);
sourcepub fn map<U: ?Sized>(
&mut self,
m: impl FnOnce(&T) -> &U
) -> CellsFormatter<'_, 'b, U>
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.
sourcepub fn map_with<U>(
&mut self,
m: impl FnOnce(&'b T) -> U,
f: impl FnOnce(&mut CellsFormatter<'_, '_, U>)
)
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.
sourcepub fn filter(
&mut self,
f: impl FnOnce(&T) -> bool
) -> CellsFormatter<'_, '_, T>
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.
sourcepub fn filter_map<U>(
&mut self,
f: impl FnOnce(&T) -> Option<&U>
) -> CellsFormatter<'_, 'b, U>
pub fn filter_map<U>( &mut self, f: impl FnOnce(&T) -> Option<&U> ) -> CellsFormatter<'_, 'b, U>
Creates a CellsFormatter
that both filters and maps.
sourcepub fn filter_map_with<U>(
&mut self,
f: impl FnOnce(&'b T) -> Option<U>,
some: impl FnOnce(&mut CellsFormatter<'_, '_, U>)
)
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.
pub fn try_map_with<O, E: RawCell>( &mut self, f: impl FnOnce(&'b T) -> Result<O, E>, ok: impl FnOnce(&mut CellsFormatter<'_, '_, O>) )
sourcepub fn stretch(&mut self) -> CellsFormatter<'_, 'b, T>
pub fn stretch(&mut self) -> CellsFormatter<'_, 'b, T>
Return CellsFormatter
that generates the columns to be stretched preferentially.
See ColumnStyle::stretch
for details.
source§impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, &T>
impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, &T>
pub fn unref(&mut self) -> CellsFormatter<'_, '_, T>
source§impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, &mut T>
impl<'a, 'b, T: ?Sized> CellsFormatter<'a, 'b, &mut T>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more