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