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§
Source§impl Cell
impl Cell
Sourcepub fn new_align(string: &str, align: Alignment) -> Cell
pub fn new_align(string: &str, align: Alignment) -> Cell
Create a new Cell
initialized with content from string
.
Text alignment in cell is configurable with the align
argument
Examples found in repository?
3fn main() {
4 /*
5 The following code will output
6
7 +---------------+---------------+--------------+
8 | A table with horizontal span |
9 +===============+===============+==============+
10 | This is a cell with span of 2 | span of 1 |
11 +---------------+---------------+--------------+
12 | span of 1 | span of 1 | span of 1 |
13 +---------------+---------------+--------------+
14 | This cell with a span of 3 is centered |
15 +---------------+---------------+--------------+
16 */
17
18 let mut table: prettytable::Table = table![
19 [H2 -> "This is a cell with span of 2", "span of 1"],
20 ["span of 1", "span of 1", "span of 1"],
21 [H03c -> "This cell with a span of 3 is centered"]
22 ];
23 table.set_titles(Row::new(vec![Cell::new_align(
24 "A table with horizontal span",
25 Alignment::CENTER,
26 )
27 .with_hspan(3)]));
28 table.printstd();
29}
Sourcepub fn new(string: &str) -> Cell
pub fn new(string: &str) -> Cell
Create a new Cell
initialized with content from string
.
By default, content is align to LEFT
Examples found in repository?
21fn main() {
22 let mut table = Table::new();
23 table.add_row(row!["ABC", "DEFG", "HIJKLMN"]);
24 table.add_row(row!["foobar", "bar", "foo"]);
25 table.add_row(Row::new(vec![
26 Cell::new("foobar2"),
27 Cell::new("bar2"),
28 Cell::new("foo2"),
29 ]));
30 table.printstd();
31 println!("Modified : ");
32 table.set_element("new_foo", 2, 1).unwrap();
33 table.printstd();
34
35 // The same table can be built the following way :
36 let _table = table!(
37 ["ABC", "DEFG", "HIJKLMN"],
38 ["foobar", "bar", "foo"],
39 ["foobar2", "bar2", "foo2"]
40 );
41
42 // Or directly print it like this
43 let _table = ptable!(
44 ["ABC", "DEFG", "HIJKLMN"],
45 ["foobar", "bar", "foo"],
46 ["foobar2", "bar2", "foo2"]
47 );
48}
More examples
6fn main() {
7 let _ = table!();
8 let mut table = Table::new();
9 // Add style to a cell
10 table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
11 // Add style to a full row
12 table.add_row(row![FY => "styled", "bar", "foo"]);
13 table.add_row(Row::new(vec![
14 Cell::new("foobar2"),
15 // Create a cell with a red foreground color
16 Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
17 // Create a cell with red foreground color, yellow background color, with bold characters
18 Cell::new("foo2").style_spec("FrByb"),
19 // Using the cell! macro
20 cell!(Fr->"red"),
21 ]));
22
23 table.printstd();
24
25 // Print a table with some styles on it :
26 // FrBybl means : Foregound red, Background yellow, bold, left align
27 ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
28
29 // You can also apply style to full rows :
30 let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
31 // Set a title line, with all text centered in the cell
32 table.set_titles(row![c => "Title 1", "Title 2"]);
33 table.printstd();
34}
Sourcepub fn with_style(self, attr: Attr) -> Cell
pub fn with_style(self, attr: Attr) -> Cell
Add a style attribute to the cell. Can be chained
Examples found in repository?
6fn main() {
7 let _ = table!();
8 let mut table = Table::new();
9 // Add style to a cell
10 table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
11 // Add style to a full row
12 table.add_row(row![FY => "styled", "bar", "foo"]);
13 table.add_row(Row::new(vec![
14 Cell::new("foobar2"),
15 // Create a cell with a red foreground color
16 Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
17 // Create a cell with red foreground color, yellow background color, with bold characters
18 Cell::new("foo2").style_spec("FrByb"),
19 // Using the cell! macro
20 cell!(Fr->"red"),
21 ]));
22
23 table.printstd();
24
25 // Print a table with some styles on it :
26 // FrBybl means : Foregound red, Background yellow, bold, left align
27 ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
28
29 // You can also apply style to full rows :
30 let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
31 // Set a title line, with all text centered in the cell
32 table.set_titles(row![c => "Title 1", "Title 2"]);
33 table.printstd();
34}
Sourcepub fn with_hspan(self, hspan: usize) -> Cell
pub fn with_hspan(self, hspan: usize) -> Cell
Add horizontal spanning to the cell
Examples found in repository?
3fn main() {
4 /*
5 The following code will output
6
7 +---------------+---------------+--------------+
8 | A table with horizontal span |
9 +===============+===============+==============+
10 | This is a cell with span of 2 | span of 1 |
11 +---------------+---------------+--------------+
12 | span of 1 | span of 1 | span of 1 |
13 +---------------+---------------+--------------+
14 | This cell with a span of 3 is centered |
15 +---------------+---------------+--------------+
16 */
17
18 let mut table: prettytable::Table = table![
19 [H2 -> "This is a cell with span of 2", "span of 1"],
20 ["span of 1", "span of 1", "span of 1"],
21 [H03c -> "This cell with a span of 3 is centered"]
22 ];
23 table.set_titles(Row::new(vec![Cell::new_align(
24 "A table with horizontal span",
25 Alignment::CENTER,
26 )
27 .with_hspan(3)]));
28 table.printstd();
29}
Sourcepub fn reset_style(&mut self)
pub fn reset_style(&mut self)
Remove all style attributes and reset alignment to default (LEFT)
Sourcepub fn style_spec(self, spec: &str) -> Cell
pub fn style_spec(self, spec: &str) -> Cell
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?
6fn main() {
7 let _ = table!();
8 let mut table = Table::new();
9 // Add style to a cell
10 table.add_row(row![FrByb->"ABC", "DEFG", "HIJKLMN"]);
11 // Add style to a full row
12 table.add_row(row![FY => "styled", "bar", "foo"]);
13 table.add_row(Row::new(vec![
14 Cell::new("foobar2"),
15 // Create a cell with a red foreground color
16 Cell::new("bar2").with_style(Attr::ForegroundColor(color::RED)),
17 // Create a cell with red foreground color, yellow background color, with bold characters
18 Cell::new("foo2").style_spec("FrByb"),
19 // Using the cell! macro
20 cell!(Fr->"red"),
21 ]));
22
23 table.printstd();
24
25 // Print a table with some styles on it :
26 // FrBybl means : Foregound red, Background yellow, bold, left align
27 ptable!([FrBybl->"A", "B", FrBybr->"C"], [123, 234, 345, 456], [Fg => 1, 2, 3]);
28
29 // You can also apply style to full rows :
30 let mut table = table!([Frb => "A", "B", "C"], [1, 2, 3, 4], ["A\nBCCZZZ\nDDD", 2, table]);
31 // Set a title line, with all text centered in the cell
32 table.set_titles(row![c => "Title 1", "Title 2"]);
33 table.printstd();
34}
Sourcepub fn get_content(&self) -> String
pub fn get_content(&self) -> String
Return a copy of the full string contained in the cell