Struct 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

Source

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?
examples/span.rs (lines 23-26)
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}
Source

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?
examples/basic.rs (line 26)
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
Hide additional examples
examples/style.rs (line 14)
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}
Source

pub fn align(&mut self, align: Alignment)

Set text alignment in the cell

Source

pub fn style(&mut self, attr: Attr)

Add a style attribute to the cell

Source

pub fn with_style(self, attr: Attr) -> Cell

Add a style attribute to the cell. Can be chained

Examples found in repository?
examples/style.rs (line 16)
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}
Source

pub fn with_hspan(self, hspan: usize) -> Cell

Add horizontal spanning to the cell

Examples found in repository?
examples/span.rs (line 27)
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}
Source

pub fn reset_style(&mut self)

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

Source

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?
examples/style.rs (line 18)
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}
Source

pub fn set_hspan(&mut self, hspan: usize)

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

Source

pub fn get_hspan(&self) -> usize

Get horizontal span of this cell (> 0)

Source

pub fn get_content(&self) -> String

Return a copy of the full string contained in the cell

Source

pub fn print_html<T: Write + ?Sized>(&self, out: &mut T) -> Result<usize, Error>

Print the cell in HTML format to out.

Trait Implementations§

Source§

impl Clone for Cell

Source§

fn clone(&self) -> Cell

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Cell

Source§

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

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

impl Default for Cell

Source§

fn default() -> Cell

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

Source§

impl<'a, T: ToString> From<&'a T> for Cell

Source§

fn from(f: &T) -> Cell

Converts to this type from the input type.
Source§

impl Hash for Cell

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Cell

Source§

fn eq(&self, other: &Cell) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl ToString for Cell

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl Eq for Cell

Source§

impl StructuralPartialEq for Cell

Auto Trait Implementations§

§

impl Freeze for Cell

§

impl RefUnwindSafe for Cell

§

impl Send for Cell

§

impl Sync for Cell

§

impl Unpin for Cell

§

impl UnwindSafe for Cell

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 T
where 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

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 T
where U: TryFrom<T>,

Source§

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.