Struct Table

Source
pub struct Table<'a> { /* private fields */ }
Expand description

The Table structure

§Example

use::stybulate::*;
let mut table = Table::new(
    Style::Fancy,
    vec![
        vec![Cell::from("answer"), Cell::Int(42)],
        vec![Cell::from("pi"), Cell::Float(3.1415)],
    ],
    Some(Headers::from(vec!["strings", "numbers"]))
);
table.set_align(Align::Center, Align::Right);

Implementations§

Source§

impl<'a> Table<'a>

Source

pub fn new( style: Style, contents: Vec<Vec<Cell<'a>>>, headers: Option<Headers>, ) -> Self

Table constructor with default alignments (Align::Left for strings and Align::Decimal for numbers)

Examples found in repository?
examples/wide_characters.rs (lines 5-14)
3fn main() {
4    // Wide characters are handled correctly when computing columns length:
5    let table = Table::new(
6        Style::Grid,
7        vec![
8            vec![Cell::from("Tabulate with style!")],
9            vec![Cell::from("табулировать со стилем!")],
10            vec![Cell::from("スタイルで集計!")],
11            vec![Cell::from("用样式表!")],
12        ],
13        None,
14    );
15    println!("{}", table.tabulate());
16    /* Will print:
17    +-------------------------+
18    | Tabulate with style!    |
19    +-------------------------+
20    | табулировать со стилем! |
21    +-------------------------+
22    | スタイルで集計!        |
23    +-------------------------+
24    | 用样式表!              |
25    +-------------------------+
26    */
27}
More examples
Hide additional examples
examples/simple.rs (lines 5-12)
3fn main() {
4    // A simple table with strings and numbers and the default alignments
5    let mut table = Table::new(
6        Style::Fancy,
7        vec![
8            vec![Cell::from("spam"), Cell::Float(41.9999)],
9            vec![Cell::from("eggs"), Cell::Int(451)],
10        ],
11        Some(Headers::from(vec!["strings", "numbers"])),
12    );
13    println!("{}", table.tabulate());
14    /* Will print:
15    ╒═══════════╤═══════════╕
16    │ strings   │   numbers │
17    ╞═══════════╪═══════════╡
18    │ spam      │   41.9999 │
19    ├───────────┼───────────┤
20    │ eggs      │  451      │
21    ╘═══════════╧═══════════╛
22    */
23
24    // Alignment can be changed like so:
25    table.set_align(Align::Right, Align::Center);
26    println!("{}", table.tabulate());
27    /* Will print:
28    ╒═══════════╤═══════════╕
29    │   strings │  numbers  │
30    ╞═══════════╪═══════════╡
31    │      spam │  41.9999  │
32    ├───────────┼───────────┤
33    │      eggs │ 451.0000  │
34    ╘═══════════╧═══════════╛
35    */
36
37    // With the feature "ansi_term_style", the borders style can be changed like so:
38    table.set_border_style(ansi_term::Color::Cyan.bold());
39    println!("{}", table.tabulate());
40    // Will print same as above but with bold and colored borders
41}
examples/colored_content.rs (lines 5-41)
3fn main() {
4    // Colored content can be inserted either with AsciiEscapedString
5    let table = Table::new(
6        Style::Presto,
7        vec![
8            vec![Cell::from("Normal"), Cell::from("Stybulate")],
9            vec![
10                Cell::from("Red"),
11                Cell::Text(Box::new(AsciiEscapedString::from(
12                    "\x1b[31mStybulate\x1b[0m",
13                ))),
14            ],
15            vec![
16                Cell::from("Green"),
17                Cell::Text(Box::new(AsciiEscapedString::from(
18                    "\x1b[32mStybulate\x1b[0m",
19                ))),
20            ],
21            vec![
22                Cell::from("Yellow"),
23                Cell::Text(Box::new(AsciiEscapedString::from(
24                    "\x1b[33mStybulate\x1b[0m",
25                ))),
26            ],
27            vec![
28                Cell::from("Blue"),
29                Cell::Text(Box::new(AsciiEscapedString::from(
30                    "\x1b[34mStybulate\x1b[0m",
31                ))),
32            ],
33            vec![
34                Cell::from("Mutlicolor and blinking (because why not?)"),
35                Cell::Text(Box::new(AsciiEscapedString::from(
36                    "\x1b[5;30mS\x1b[31mt\x1b[32my\x1b[33mb\x1b[34mu\x1b[35ml\x1b[36ma\x1b[37mt\x1b[30me\x1b[0m",
37                ))),
38            ],
39        ],
40        Some(Headers::from(vec!["Style", "Text"])),
41    );
42    println!("{}", table.tabulate());
43
44    // or with ansi_term ANSIStrings (with the feature "ansi_term_style")
45    use ansi_term::ANSIStrings;
46    use ansi_term::Color::*;
47    let red_stybulate = &[Red.paint("Stybulate")];
48    let green_stybulate = &[Green.paint("Stybulate")];
49    let yellow_stybulate = &[Yellow.paint("Stybulate")];
50    let blue_stybulate = &[Blue.paint("Stybulate")];
51    let multicolorandblinking_stybulate = &[
52        Black.blink().paint("S"),
53        Red.blink().paint("t"),
54        Green.blink().paint("y"),
55        Yellow.blink().paint("b"),
56        Blue.blink().paint("u"),
57        Purple.blink().paint("l"),
58        Cyan.blink().paint("a"),
59        White.blink().paint("t"),
60        Black.blink().paint("e"),
61    ];
62    let table2 = Table::new(
63        Style::FancyGithub,
64        vec![
65            vec![Cell::from("Normal"), Cell::from("Stybulate")],
66            vec![
67                Cell::from("Red"),
68                Cell::Text(Box::new(ANSIStrings(red_stybulate))),
69            ],
70            vec![
71                Cell::from("Green"),
72                Cell::Text(Box::new(ANSIStrings(green_stybulate))),
73            ],
74            vec![
75                Cell::from("Yellow"),
76                Cell::Text(Box::new(ANSIStrings(yellow_stybulate))),
77            ],
78            vec![
79                Cell::from("Blue"),
80                Cell::Text(Box::new(ANSIStrings(blue_stybulate))),
81            ],
82            vec![
83                Cell::from("Mutlicolor and blinking (because why not?)"),
84                Cell::Text(Box::new(ANSIStrings(multicolorandblinking_stybulate))),
85            ],
86        ],
87        Some(Headers::from(vec!["Style", "Text"])),
88    );
89    println!("{}", table2.tabulate());
90}
Source

pub fn set_align(&mut self, str_align: Align, num_align: Align)

Set the table alignments (defaults are Align::Left for strings and Align::Decimal for numbers)

§Panics

Panics if str_align is equal to Align::Decimal

Examples found in repository?
examples/simple.rs (line 25)
3fn main() {
4    // A simple table with strings and numbers and the default alignments
5    let mut table = Table::new(
6        Style::Fancy,
7        vec![
8            vec![Cell::from("spam"), Cell::Float(41.9999)],
9            vec![Cell::from("eggs"), Cell::Int(451)],
10        ],
11        Some(Headers::from(vec!["strings", "numbers"])),
12    );
13    println!("{}", table.tabulate());
14    /* Will print:
15    ╒═══════════╤═══════════╕
16    │ strings   │   numbers │
17    ╞═══════════╪═══════════╡
18    │ spam      │   41.9999 │
19    ├───────────┼───────────┤
20    │ eggs      │  451      │
21    ╘═══════════╧═══════════╛
22    */
23
24    // Alignment can be changed like so:
25    table.set_align(Align::Right, Align::Center);
26    println!("{}", table.tabulate());
27    /* Will print:
28    ╒═══════════╤═══════════╕
29    │   strings │  numbers  │
30    ╞═══════════╪═══════════╡
31    │      spam │  41.9999  │
32    ├───────────┼───────────┤
33    │      eggs │ 451.0000  │
34    ╘═══════════╧═══════════╛
35    */
36
37    // With the feature "ansi_term_style", the borders style can be changed like so:
38    table.set_border_style(ansi_term::Color::Cyan.bold());
39    println!("{}", table.tabulate());
40    // Will print same as above but with bold and colored borders
41}
Source

pub fn set_border_style(&mut self, style: Style)

Set the borders style

§Feature

Needs feature ansi_term_style.

Examples found in repository?
examples/simple.rs (line 38)
3fn main() {
4    // A simple table with strings and numbers and the default alignments
5    let mut table = Table::new(
6        Style::Fancy,
7        vec![
8            vec![Cell::from("spam"), Cell::Float(41.9999)],
9            vec![Cell::from("eggs"), Cell::Int(451)],
10        ],
11        Some(Headers::from(vec!["strings", "numbers"])),
12    );
13    println!("{}", table.tabulate());
14    /* Will print:
15    ╒═══════════╤═══════════╕
16    │ strings   │   numbers │
17    ╞═══════════╪═══════════╡
18    │ spam      │   41.9999 │
19    ├───────────┼───────────┤
20    │ eggs      │  451      │
21    ╘═══════════╧═══════════╛
22    */
23
24    // Alignment can be changed like so:
25    table.set_align(Align::Right, Align::Center);
26    println!("{}", table.tabulate());
27    /* Will print:
28    ╒═══════════╤═══════════╕
29    │   strings │  numbers  │
30    ╞═══════════╪═══════════╡
31    │      spam │  41.9999  │
32    ├───────────┼───────────┤
33    │      eggs │ 451.0000  │
34    ╘═══════════╧═══════════╛
35    */
36
37    // With the feature "ansi_term_style", the borders style can be changed like so:
38    table.set_border_style(ansi_term::Color::Cyan.bold());
39    println!("{}", table.tabulate());
40    // Will print same as above but with bold and colored borders
41}
Source

pub fn tabulate(&self) -> String

Creates the table as a String

Examples found in repository?
examples/wide_characters.rs (line 15)
3fn main() {
4    // Wide characters are handled correctly when computing columns length:
5    let table = Table::new(
6        Style::Grid,
7        vec![
8            vec![Cell::from("Tabulate with style!")],
9            vec![Cell::from("табулировать со стилем!")],
10            vec![Cell::from("スタイルで集計!")],
11            vec![Cell::from("用样式表!")],
12        ],
13        None,
14    );
15    println!("{}", table.tabulate());
16    /* Will print:
17    +-------------------------+
18    | Tabulate with style!    |
19    +-------------------------+
20    | табулировать со стилем! |
21    +-------------------------+
22    | スタイルで集計!        |
23    +-------------------------+
24    | 用样式表!              |
25    +-------------------------+
26    */
27}
More examples
Hide additional examples
examples/simple.rs (line 13)
3fn main() {
4    // A simple table with strings and numbers and the default alignments
5    let mut table = Table::new(
6        Style::Fancy,
7        vec![
8            vec![Cell::from("spam"), Cell::Float(41.9999)],
9            vec![Cell::from("eggs"), Cell::Int(451)],
10        ],
11        Some(Headers::from(vec!["strings", "numbers"])),
12    );
13    println!("{}", table.tabulate());
14    /* Will print:
15    ╒═══════════╤═══════════╕
16    │ strings   │   numbers │
17    ╞═══════════╪═══════════╡
18    │ spam      │   41.9999 │
19    ├───────────┼───────────┤
20    │ eggs      │  451      │
21    ╘═══════════╧═══════════╛
22    */
23
24    // Alignment can be changed like so:
25    table.set_align(Align::Right, Align::Center);
26    println!("{}", table.tabulate());
27    /* Will print:
28    ╒═══════════╤═══════════╕
29    │   strings │  numbers  │
30    ╞═══════════╪═══════════╡
31    │      spam │  41.9999  │
32    ├───────────┼───────────┤
33    │      eggs │ 451.0000  │
34    ╘═══════════╧═══════════╛
35    */
36
37    // With the feature "ansi_term_style", the borders style can be changed like so:
38    table.set_border_style(ansi_term::Color::Cyan.bold());
39    println!("{}", table.tabulate());
40    // Will print same as above but with bold and colored borders
41}
examples/colored_content.rs (line 42)
3fn main() {
4    // Colored content can be inserted either with AsciiEscapedString
5    let table = Table::new(
6        Style::Presto,
7        vec![
8            vec![Cell::from("Normal"), Cell::from("Stybulate")],
9            vec![
10                Cell::from("Red"),
11                Cell::Text(Box::new(AsciiEscapedString::from(
12                    "\x1b[31mStybulate\x1b[0m",
13                ))),
14            ],
15            vec![
16                Cell::from("Green"),
17                Cell::Text(Box::new(AsciiEscapedString::from(
18                    "\x1b[32mStybulate\x1b[0m",
19                ))),
20            ],
21            vec![
22                Cell::from("Yellow"),
23                Cell::Text(Box::new(AsciiEscapedString::from(
24                    "\x1b[33mStybulate\x1b[0m",
25                ))),
26            ],
27            vec![
28                Cell::from("Blue"),
29                Cell::Text(Box::new(AsciiEscapedString::from(
30                    "\x1b[34mStybulate\x1b[0m",
31                ))),
32            ],
33            vec![
34                Cell::from("Mutlicolor and blinking (because why not?)"),
35                Cell::Text(Box::new(AsciiEscapedString::from(
36                    "\x1b[5;30mS\x1b[31mt\x1b[32my\x1b[33mb\x1b[34mu\x1b[35ml\x1b[36ma\x1b[37mt\x1b[30me\x1b[0m",
37                ))),
38            ],
39        ],
40        Some(Headers::from(vec!["Style", "Text"])),
41    );
42    println!("{}", table.tabulate());
43
44    // or with ansi_term ANSIStrings (with the feature "ansi_term_style")
45    use ansi_term::ANSIStrings;
46    use ansi_term::Color::*;
47    let red_stybulate = &[Red.paint("Stybulate")];
48    let green_stybulate = &[Green.paint("Stybulate")];
49    let yellow_stybulate = &[Yellow.paint("Stybulate")];
50    let blue_stybulate = &[Blue.paint("Stybulate")];
51    let multicolorandblinking_stybulate = &[
52        Black.blink().paint("S"),
53        Red.blink().paint("t"),
54        Green.blink().paint("y"),
55        Yellow.blink().paint("b"),
56        Blue.blink().paint("u"),
57        Purple.blink().paint("l"),
58        Cyan.blink().paint("a"),
59        White.blink().paint("t"),
60        Black.blink().paint("e"),
61    ];
62    let table2 = Table::new(
63        Style::FancyGithub,
64        vec![
65            vec![Cell::from("Normal"), Cell::from("Stybulate")],
66            vec![
67                Cell::from("Red"),
68                Cell::Text(Box::new(ANSIStrings(red_stybulate))),
69            ],
70            vec![
71                Cell::from("Green"),
72                Cell::Text(Box::new(ANSIStrings(green_stybulate))),
73            ],
74            vec![
75                Cell::from("Yellow"),
76                Cell::Text(Box::new(ANSIStrings(yellow_stybulate))),
77            ],
78            vec![
79                Cell::from("Blue"),
80                Cell::Text(Box::new(ANSIStrings(blue_stybulate))),
81            ],
82            vec![
83                Cell::from("Mutlicolor and blinking (because why not?)"),
84                Cell::Text(Box::new(ANSIStrings(multicolorandblinking_stybulate))),
85            ],
86        ],
87        Some(Headers::from(vec!["Style", "Text"])),
88    );
89    println!("{}", table2.tabulate());
90}

Auto Trait Implementations§

§

impl<'a> Freeze for Table<'a>

§

impl<'a> !RefUnwindSafe for Table<'a>

§

impl<'a> !Send for Table<'a>

§

impl<'a> !Sync for Table<'a>

§

impl<'a> Unpin for Table<'a>

§

impl<'a> !UnwindSafe for Table<'a>

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> 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, 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.