Struct prettytable::Cell

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

Represent a table cell containing a string.

Once created, a cell’s content cannot be modified. The cell would have to be replaced by another one

Implementations§

Create a new Cell initialized with content from string. Text alignment in cell is configurable with the align argument

Examples found in repository?
examples/span.rs (lines 23-26)
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
fn main() {
    /*
        The following code will output

        +---------------+---------------+--------------+
        |         A table with horizontal span         |
        +===============+===============+==============+
        | This is a cell with span of 2 | span of 1    |
        +---------------+---------------+--------------+
        | span of 1     | span of 1     | span of 1    |
        +---------------+---------------+--------------+
        |    This cell with a span of 3 is centered    |
        +---------------+---------------+--------------+
    */

    let mut table: prettytable::Table = table![
    [H2 -> "This is a cell with span of 2", "span of 1"],
    ["span of 1", "span of 1", "span of 1"],
    [H03c -> "This cell with a span of 3 is centered"]
    ];
    table.set_titles(Row::new(vec![Cell::new_align(
        "A table with horizontal span",
        Alignment::CENTER,
    )
    .with_hspan(3)]));
    table.printstd();
}

Create a new Cell initialized with content from string. By default, content is align to LEFT

Examples found in repository?
examples/basic.rs (line 26)
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
fn main() {
    let mut table = Table::new();
    table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
    table.add_row(row!["foobar", "bar", "foo"]);
    table.add_row(Row::new(vec![
        Cell::new("foobar2"),
        Cell::new("bar2"),
        Cell::new("foo2"),
    ]));
    table.printstd();
    println!("Modified : ");
    table.set_element("new_foo", 2, 1).unwrap();
    table.printstd();

    // The same table can be built the following way :
    let _table = table!(
        ["ABC", "DEFG", "HIJKLMN"],
        ["foobar", "bar", "foo"],
        ["foobar2", "bar2", "foo2"]
    );

    // Or directly print it like this
    let _table = ptable!(
        ["ABC", "DEFG", "HIJKLMN"],
        ["foobar", "bar", "foo"],
        ["foobar2", "bar2", "foo2"]
    );
}
More examples
Hide additional examples
examples/style.rs (line 14)
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
fn main() {
    let _ = table!();
    let mut table = Table::new();
    // Add style to a cell
    table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
    // Add style to a full row
    table.add_row(row![FY => "styled", "bar", "foo"]);
    table.add_row(Row::new(vec![
        Cell::new("foobar2"),
        // Create a cell with a red foreground color
        Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
        // Create a cell with red foreground color, yellow background color, with bold characters
        Cell::new("foo2").style_spec("FrByb"),
        // Using the cell! macro
        cell!(Fr->"red"),
    ]));

    table.printstd();

    // Print a table with some styles on it :
    // FrBybl means : Foregound red, Background yellow, bold, left align
    ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);

    // You can also apply style to full rows :
    let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
    // Set a title line, with all text centered in the cell
    table.set_titles(row![c => "Title 1", "Title 2"]);
    table.printstd();
}

Set text alignment in the cell

Add a style attribute to the cell

Add a style attribute to the cell. Can be chained

Examples found in repository?
examples/style.rs (line 16)
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
fn main() {
    let _ = table!();
    let mut table = Table::new();
    // Add style to a cell
    table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
    // Add style to a full row
    table.add_row(row![FY => "styled", "bar", "foo"]);
    table.add_row(Row::new(vec![
        Cell::new("foobar2"),
        // Create a cell with a red foreground color
        Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
        // Create a cell with red foreground color, yellow background color, with bold characters
        Cell::new("foo2").style_spec("FrByb"),
        // Using the cell! macro
        cell!(Fr->"red"),
    ]));

    table.printstd();

    // Print a table with some styles on it :
    // FrBybl means : Foregound red, Background yellow, bold, left align
    ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);

    // You can also apply style to full rows :
    let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
    // Set a title line, with all text centered in the cell
    table.set_titles(row![c => "Title 1", "Title 2"]);
    table.printstd();
}

Add horizontal spanning to the cell

Examples found in repository?
examples/span.rs (line 27)
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
fn main() {
    /*
        The following code will output

        +---------------+---------------+--------------+
        |         A table with horizontal span         |
        +===============+===============+==============+
        | This is a cell with span of 2 | span of 1    |
        +---------------+---------------+--------------+
        | span of 1     | span of 1     | span of 1    |
        +---------------+---------------+--------------+
        |    This cell with a span of 3 is centered    |
        +---------------+---------------+--------------+
    */

    let mut table: prettytable::Table = table![
    [H2 -> "This is a cell with span of 2", "span of 1"],
    ["span of 1", "span of 1", "span of 1"],
    [H03c -> "This cell with a span of 3 is centered"]
    ];
    table.set_titles(Row::new(vec![Cell::new_align(
        "A table with horizontal span",
        Alignment::CENTER,
    )
    .with_hspan(3)]));
    table.printstd();
}

Remove all style attributes and reset alignment to default (LEFT)

Set the cell’s style by applying the given specifier string

Style spec syntax

The syntax for the style specifier looks like this : FrBybl which means Foreground red Background yellow bold left

List of supported specifiers :
  • F : Foreground (must be followed by a color specifier)
  • B : Background (must be followed by a color specifier)
  • H : Horizontal span (must be followed by a number)
  • b : bold
  • i : italic
  • u : underline
  • c : Align center
  • l : Align left
  • r : Align right
  • d : default style
List of color specifiers :
  • r : Red
  • b : Blue
  • g : Green
  • y : Yellow
  • c : Cyan
  • m : Magenta
  • w : White
  • d : Black

And capital letters are for bright colors. Eg :

  • R : Bright Red
  • B : Bright Blue
  • … and so on …
Examples found in repository?
examples/style.rs (line 18)
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
fn main() {
    let _ = table!();
    let mut table = Table::new();
    // Add style to a cell
    table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
    // Add style to a full row
    table.add_row(row![FY => "styled", "bar", "foo"]);
    table.add_row(Row::new(vec![
        Cell::new("foobar2"),
        // Create a cell with a red foreground color
        Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
        // Create a cell with red foreground color, yellow background color, with bold characters
        Cell::new("foo2").style_spec("FrByb"),
        // Using the cell! macro
        cell!(Fr->"red"),
    ]));

    table.printstd();

    // Print a table with some styles on it :
    // FrBybl means : Foregound red, Background yellow, bold, left align
    ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);

    // You can also apply style to full rows :
    let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
    // Set a title line, with all text centered in the cell
    table.set_titles(row![c => "Title 1", "Title 2"]);
    table.printstd();
}

Set horizontal span for this cell (must be > 0)

Get horizontal span of this cell (> 0)

Return a copy of the full string contained in the cell

Print the cell in HTML format to out.

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more

Return a cell initialized with a single empty String, with LEFT alignment

Converts to this type from the input type.
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Converts the given value to a String. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.