Struct text_grid::GridBuilder

source ·
pub struct GridBuilder { /* private fields */ }
Expand description

A builder used to create plain-text table.

Examples

use text_grid::*;
let mut g = GridBuilder::new();
g.push(|b| {
    b.push(cell("name").right());
    b.push("type");
    b.push("value");
});
g.push_separator();
g.push(|b| {
    b.push(cell(String::from("X")).right());
    b.push("A");
    b.push(10);
});
g.push(|b| {
    b.push(cell("Y").right());
    b.push_with_colspan(cell("BBB").center(), 2);
});
assert_eq!(format!("\n{g}"), r#"
 name | type | value |
------|------|-------|
    X | A    |    10 |
    Y |     BBB      |
"#);

Implementations§

source§

impl GridBuilder

source

pub fn new() -> Self

Create a new GridBuilder.

Examples found in repository?
examples/grid_buf.rs (line 3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    use text_grid::*;
    let mut g = GridBuilder::new();
    g.push(|b| {
        b.push(cell("name").right());
        b.push("type");
        b.push("value");
    });
    g.push_separator();
    g.push(|b| {
        b.push(cell(String::from("X")).right());
        b.push("A");
        b.push(10);
    });
    g.push(|b| {
        b.push(cell("Y").right());
        b.push_with_colspan(cell("BBB").center(), 2);
    });
    print!("{}", g);
}
More examples
Hide additional examples
examples/set_column_separators.rs (line 3)
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::*;
    let mut g = GridBuilder::new();
    g.push(|b| {
        b.push("A");
        b.push("B");
        b.push("C");
    });
    g.push(|b| {
        b.push("AAA");
        b.push("BBB");
        b.push("CCC");
    });
    g.set_column_separators(vec![true, true]);
    println!("{:?}", vec![true, true]);
    println!("{}", g);

    g.set_column_separators(vec![false, true]);
    println!("{:?}", vec![false, true]);
    println!("{}", g);
}
source

pub fn set_column_separators(&mut self, separators: Vec<bool>)

Set column separator’s visibility.

separators[0] indicate visibility of the right side of the leftmost column.

Examples
use text_grid::*;
let mut g = GridBuilder::new();
g.push(|b| {
    b.push("A");
    b.push("B");
    b.push("C");
});
g.push(|b| {
    b.push("AAA");
    b.push("BBB");
    b.push("CCC");
});
g.set_column_separators(vec![true, true]);
assert_eq!(format!("\n{g}"), r#"
 A   | B   | C   |
 AAA | BBB | CCC |
"#);

g.set_column_separators(vec![false, true]);
assert_eq!(format!("\n{g}"), r#"
 A  B   | C   |
 AAABBB | CCC |
"#);
Examples found in repository?
examples/set_column_separators.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::*;
    let mut g = GridBuilder::new();
    g.push(|b| {
        b.push("A");
        b.push("B");
        b.push("C");
    });
    g.push(|b| {
        b.push("AAA");
        b.push("BBB");
        b.push("CCC");
    });
    g.set_column_separators(vec![true, true]);
    println!("{:?}", vec![true, true]);
    println!("{}", g);

    g.set_column_separators(vec![false, true]);
    println!("{:?}", vec![false, true]);
    println!("{}", g);
}
source

pub fn push(&mut self, f: impl FnOnce(&mut RowBuilder<'_>))

Append a row to the bottom of the grid.

Examples found in repository?
examples/grid_buf.rs (lines 4-8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    use text_grid::*;
    let mut g = GridBuilder::new();
    g.push(|b| {
        b.push(cell("name").right());
        b.push("type");
        b.push("value");
    });
    g.push_separator();
    g.push(|b| {
        b.push(cell(String::from("X")).right());
        b.push("A");
        b.push(10);
    });
    g.push(|b| {
        b.push(cell("Y").right());
        b.push_with_colspan(cell("BBB").center(), 2);
    });
    print!("{}", g);
}
More examples
Hide additional examples
examples/set_column_separators.rs (lines 4-8)
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::*;
    let mut g = GridBuilder::new();
    g.push(|b| {
        b.push("A");
        b.push("B");
        b.push("C");
    });
    g.push(|b| {
        b.push("AAA");
        b.push("BBB");
        b.push("CCC");
    });
    g.set_column_separators(vec![true, true]);
    println!("{:?}", vec![true, true]);
    println!("{}", g);

    g.set_column_separators(vec![false, true]);
    println!("{:?}", vec![false, true]);
    println!("{}", g);
}
source

pub fn push_separator(&mut self)

Append a row separator to the bottom of the grid.

Examples found in repository?
examples/grid_buf.rs (line 9)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
fn main() {
    use text_grid::*;
    let mut g = GridBuilder::new();
    g.push(|b| {
        b.push(cell("name").right());
        b.push("type");
        b.push("value");
    });
    g.push_separator();
    g.push(|b| {
        b.push(cell(String::from("X")).right());
        b.push("A");
        b.push(10);
    });
    g.push(|b| {
        b.push(cell("Y").right());
        b.push_with_colspan(cell("BBB").center(), 2);
    });
    print!("{}", g);
}

Trait Implementations§

source§

impl Debug for GridBuilder

source§

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

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

impl Default for GridBuilder

source§

fn default() -> GridBuilder

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

impl Display for GridBuilder

source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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.