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§
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?
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();
}
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?
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
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();
}
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?
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();
}
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?
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();
}
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?
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();
}
sourcepub fn get_content(&self) -> String
pub fn get_content(&self) -> String
Return a copy of the full string contained in the cell