Struct text_grid::CellsFormatter
source · pub struct CellsFormatter<'a, T> { /* private fields */ }Expand description
Used to define columns.
- Use
columnto create column. - Use
column_withto create multi level header. - Use
contentto create shared header columns.
Implementations§
source§impl<'a, T> CellsFormatter<'a, T>
impl<'a, T> CellsFormatter<'a, T>
sourcepub fn column_with(
&mut self,
header: impl CellSource,
f: impl FnOnce(&mut CellsFormatter<'_, T>)
)
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?
More examples
sourcepub fn content<U: CellsSource>(&mut self, f: impl FnOnce(&T) -> U)
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 |
"#);sourcepub fn column<U: CellsSource>(
&mut self,
header: impl CellSource,
f: impl FnOnce(&T) -> U
)
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?
More examples
sourcepub fn map<'x, U: 'x>(
&'x mut self,
f: impl FnOnce(&T) -> U
) -> CellsFormatter<'x, U>
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.
sourcepub fn as_ref(&mut self) -> CellsFormatter<'_, &T>
pub fn as_ref(&mut self) -> CellsFormatter<'_, &T>
Creates a CellsFormatter whose source value was converted to reference.
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<'x, U: 'x>(
&'x mut self,
f: impl FnOnce(&T) -> Option<U>
) -> CellsFormatter<'x, U>
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§impl<T, E: CellSource> CellsFormatter<'_, Result<T, E>>
impl<T, E: CellSource> CellsFormatter<'_, Result<T, E>>
sourcepub fn ok_with(&mut self, f: impl FnOnce(&mut CellsFormatter<'_, &T>))
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>>
impl<T, E: CellSource> CellsFormatter<'_, &Result<T, E>>
sourcepub fn ok_with(&mut self, f: impl FnOnce(&mut CellsFormatter<'_, &T>))
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> 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