Struct text_grid::Grid

source ·
pub struct Grid<R: ?Sized, S = DefaultGridSchema<R>> { /* private fields */ }
Expand description

A builder used to create plain-text table from row values.

Generate a table using the columns defined by CellsFormatter.

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 |
"#);

Implementations§

source§

impl<R: CellsSource + ?Sized> Grid<R, DefaultGridSchema<R>>

source

pub fn new() -> Self

Create a new Grid with DefaultGridSchema and prepare header rows.

Examples found in repository?
examples/grid.rs (line 14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    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 });

    print!("{}", g);
}
More examples
Hide additional examples
examples/cell_macro.rs (line 14)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
    use text_grid::*;
    struct RowData {
        a: f64,
        b: f64,
    }
    impl CellsSource for RowData {
        fn fmt(f: &mut CellsFormatter<&Self>) {
            f.column("a", |&s| cell!("{:.2}", s.a).right());
            f.column("b", |&s| cell!("{:.3}", s.b).right());
        }
    }

    let mut g = Grid::new();
    g.push(&RowData { a: 1.10, b: 1.11 });
    g.push(&RowData { a: 1.00, b: 0.10 });

    print!("{}", g);

    panic!("failed");
}
examples/row_write_group.rs (line 18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    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,
    });

    print!("{}", g);
}
examples/row_write_content.rs (line 19)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    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,
    });

    print!("{}", g);
}
examples/readme.rs (line 21)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    use text_grid::*;
    struct RowData {
        a: String,
        b: u32,
        c: u32,
        d: f64,
    }
    impl CellsSource for RowData {
        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));
            });
        }
    }

    let mut g = Grid::new();
    g.push(&RowData {
        a: "ABC".to_string(),
        b: 300,
        c: 1,
        d: 100.1,
    });
    g.push(&RowData {
        a: "XY".to_string(),
        b: 2,
        c: 200,
        d: 1.234,
    });
    println!("\n{g}");
    assert_eq!(format!("\n{g}"), OUTPUT);
}
source§

impl<R: ?Sized, S: GridSchema<Source = R>> Grid<S::Source, S>

source

pub fn new_with_schema(schema: S) -> Self

Create a new Grid with specified schema and prepare header rows.

Examples found in repository?
examples/grid_schema.rs (line 17)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    use text_grid::*;

    struct MyGridSchema {
        len: usize,
    }

    impl GridSchema for MyGridSchema {
        type Source = [u32];
        fn fmt(&self, f: &mut CellsFormatter<&[u32]>) {
            for i in 0..self.len {
                f.column(i, |s| s[i]);
            }
        }
    }

    let mut g = Grid::new_with_schema(MyGridSchema { len: 3 });
    g.push(&[1, 2, 3]);
    g.push(&[4, 5, 6]);

    print!("{}", g);
}
source§

impl<R: ?Sized, S: GridSchema<Source = R>> Grid<R, S>

source

pub fn push(&mut self, source: &R)

Append a row to the bottom of the grid.

Examples found in repository?
examples/grid.rs (line 15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
fn main() {
    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 });

    print!("{}", g);
}
More examples
Hide additional examples
examples/grid_schema.rs (line 18)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
fn main() {
    use text_grid::*;

    struct MyGridSchema {
        len: usize,
    }

    impl GridSchema for MyGridSchema {
        type Source = [u32];
        fn fmt(&self, f: &mut CellsFormatter<&[u32]>) {
            for i in 0..self.len {
                f.column(i, |s| s[i]);
            }
        }
    }

    let mut g = Grid::new_with_schema(MyGridSchema { len: 3 });
    g.push(&[1, 2, 3]);
    g.push(&[4, 5, 6]);

    print!("{}", g);
}
examples/cell_macro.rs (line 15)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
fn main() {
    use text_grid::*;
    struct RowData {
        a: f64,
        b: f64,
    }
    impl CellsSource for RowData {
        fn fmt(f: &mut CellsFormatter<&Self>) {
            f.column("a", |&s| cell!("{:.2}", s.a).right());
            f.column("b", |&s| cell!("{:.3}", s.b).right());
        }
    }

    let mut g = Grid::new();
    g.push(&RowData { a: 1.10, b: 1.11 });
    g.push(&RowData { a: 1.00, b: 0.10 });

    print!("{}", g);

    panic!("failed");
}
examples/row_write_group.rs (lines 19-23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
fn main() {
    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,
    });

    print!("{}", g);
}
examples/row_write_content.rs (lines 20-24)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
fn main() {
    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,
    });

    print!("{}", g);
}
examples/readme.rs (lines 22-27)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
fn main() {
    use text_grid::*;
    struct RowData {
        a: String,
        b: u32,
        c: u32,
        d: f64,
    }
    impl CellsSource for RowData {
        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));
            });
        }
    }

    let mut g = Grid::new();
    g.push(&RowData {
        a: "ABC".to_string(),
        b: 300,
        c: 1,
        d: 100.1,
    });
    g.push(&RowData {
        a: "XY".to_string(),
        b: 2,
        c: 200,
        d: 1.234,
    });
    println!("\n{g}");
    assert_eq!(format!("\n{g}"), OUTPUT);
}
source

pub fn push_separator(&mut self)

Append a row separator to the bottom of the grid.

Trait Implementations§

source§

impl<R: ?Sized, S> Debug for Grid<R, S>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<R: CellsSource + ?Sized> Default for Grid<R, DefaultGridSchema<R>>

source§

fn default() -> Self

Returns the “default value” for a type. Read more
source§

impl<R: ?Sized, S> Display for Grid<R, S>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'a, R, S: GridSchema<Source = R>> Extend<&'a R> for Grid<R, S>

source§

fn extend<T: IntoIterator<Item = &'a R>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<R, S: GridSchema<Source = R>> Extend<R> for Grid<R, S>

source§

fn extend<T: IntoIterator<Item = R>>(&mut self, iter: T)

Extends a collection with the contents of an iterator. Read more
source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
source§

impl<'a, R: CellsSource> FromIterator<&'a R> for Grid<R>

source§

fn from_iter<T: IntoIterator<Item = &'a R>>(iter: T) -> Self

Creates a value from an iterator. Read more
source§

impl<R: CellsSource> FromIterator<R> for Grid<R>

source§

fn from_iter<T: IntoIterator<Item = R>>(iter: T) -> Self

Creates a value from an iterator. Read more

Auto Trait Implementations§

§

impl<R: ?Sized, S> RefUnwindSafe for Grid<R, S>where S: RefUnwindSafe,

§

impl<R: ?Sized, S> Send for Grid<R, S>where S: Send,

§

impl<R: ?Sized, S> Sync for Grid<R, S>where S: Sync,

§

impl<R: ?Sized, S> Unpin for Grid<R, S>where S: Unpin,

§

impl<R: ?Sized, S> UnwindSafe for Grid<R, S>where S: UnwindSafe,

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> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
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.