Skip to main content

Table

Struct Table 

Source
pub struct Table { /* private fields */ }
Expand description

Main table type for terminal output.

A table owns its headers, rows, and column styling. Content is measured at render time, so the output can adapt to the current terminal width without manual layout code from the caller.

§Typical Flow

  1. Build the schema with Table::with_columns or Table::new.
  2. Add rows with Table::add_row.
  3. Adjust columns with Table::column if you need per-column overrides.
  4. Call Table::render or print the table directly.

§Example

use tiny_table::{Cell, Column, Align, Table, Trunc};

let mut table = Table::with_columns(vec![
    Column::new("Name").width(0.35),
    Column::new("Role").truncate(Trunc::Middle),
    Column::new("Status"),
]);

table.add_section("Team").align(Align::Center);
table.add_row(vec![
    Cell::new("Ada Lovelace"),
    Cell::new("Principal Engineer"),
    Cell::new("Active"),
]);

let rendered = table.render();
assert!(rendered.contains("Name"));
assert!(rendered.contains("Ada Lovelace"));

Implementations§

Source§

impl Table

Source

pub fn new() -> Self

Create an empty table using the default Unicode border style.

Source

pub fn with_columns(columns: impl IntoIterator<Item = Column>) -> Self

Build a table from an explicit column schema.

Each Column contributes the header text and the default formatting for that column.

Examples found in repository?
examples/simple.rs (lines 4-8)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").width(15),
6        Column::new("Role").width(20),
7        Column::new("Status").width(10),
8    ]);
9
10    table.add_row(vec![
11        Cell::new("Ada Lovelace"),
12        Cell::new("Engineer"),
13        Cell::new("Active"),
14    ]);
15
16    table.add_row(vec![
17        Cell::new("Bob"),
18        Cell::new("Support"),
19        Cell::new("Away"),
20    ]);
21
22    println!("{}", table);
23}
More examples
Hide additional examples
examples/styled.rs (lines 4-8)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").bright_cyan().bold().width(0.3),
6        Column::new("Role").width(0.4).truncate(Trunc::Middle),
7        Column::new("Status").bright_yellow().bold().width(0.3),
8    ])
9    .with_section_style(SectionStyle {
10        horiz: "═",
11        mid_left: "╞",
12        mid_right: "╡",
13        mid_joint: "╪",
14    })
15    .with_separator_style(SectionStyle {
16        horiz: "╌",
17        mid_joint: "│",
18        ..SectionStyle::unicode()
19    });
20
21    table.add_section("Team").align(Align::Center);
22    table.add_row(vec![
23        Cell::new("Ada Lovelace"),
24        Cell::new("Principal Engineer"),
25        Cell::new("Active").bright_green(),
26    ]);
27
28    table.add_separator();
29    table.add_row(vec![
30        Cell::new("Bob"),
31        Cell::new("Support"),
32        Cell::new("Away"),
33    ]);
34
35    println!("{}", table);
36}
Source

pub fn with_style(self, style: TableStyle) -> Self

Replace the default border style for this table.

Source

pub fn with_section_style(self, style: SectionStyle) -> Self

Set the default style used for all section rows added with Table::add_section.

Individual sections can still override this by calling .style() on the SectionBuilder returned by Table::add_section.

Examples found in repository?
examples/styled.rs (lines 9-14)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").bright_cyan().bold().width(0.3),
6        Column::new("Role").width(0.4).truncate(Trunc::Middle),
7        Column::new("Status").bright_yellow().bold().width(0.3),
8    ])
9    .with_section_style(SectionStyle {
10        horiz: "═",
11        mid_left: "╞",
12        mid_right: "╡",
13        mid_joint: "╪",
14    })
15    .with_separator_style(SectionStyle {
16        horiz: "╌",
17        mid_joint: "│",
18        ..SectionStyle::unicode()
19    });
20
21    table.add_section("Team").align(Align::Center);
22    table.add_row(vec![
23        Cell::new("Ada Lovelace"),
24        Cell::new("Principal Engineer"),
25        Cell::new("Active").bright_green(),
26    ]);
27
28    table.add_separator();
29    table.add_row(vec![
30        Cell::new("Bob"),
31        Cell::new("Support"),
32        Cell::new("Away"),
33    ]);
34
35    println!("{}", table);
36}
Source

pub fn with_separator_style(self, style: SectionStyle) -> Self

Set the default style used for all separator rows added with Table::add_separator.

Individual separators can still override this by calling .style() on the SectionBuilder returned by Table::add_separator.

Examples found in repository?
examples/styled.rs (lines 15-19)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").bright_cyan().bold().width(0.3),
6        Column::new("Role").width(0.4).truncate(Trunc::Middle),
7        Column::new("Status").bright_yellow().bold().width(0.3),
8    ])
9    .with_section_style(SectionStyle {
10        horiz: "═",
11        mid_left: "╞",
12        mid_right: "╡",
13        mid_joint: "╪",
14    })
15    .with_separator_style(SectionStyle {
16        horiz: "╌",
17        mid_joint: "│",
18        ..SectionStyle::unicode()
19    });
20
21    table.add_section("Team").align(Align::Center);
22    table.add_row(vec![
23        Cell::new("Ada Lovelace"),
24        Cell::new("Principal Engineer"),
25        Cell::new("Active").bright_green(),
26    ]);
27
28    table.add_separator();
29    table.add_row(vec![
30        Cell::new("Bob"),
31        Cell::new("Support"),
32        Cell::new("Away"),
33    ]);
34
35    println!("{}", table);
36}
Source

pub fn set_columns(&mut self, columns: impl IntoIterator<Item = Column>)

Replace the table schema with a new set of columns.

This clears existing headers and column-specific overrides before the new schema is applied.

Source

pub fn add_row(&mut self, row: Vec<Cell>)

Add a data row.

Short rows are padded with empty cells. If a row has more cells than the header list, the table expands to fit the additional columns.

Examples found in repository?
examples/simple.rs (lines 10-14)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").width(15),
6        Column::new("Role").width(20),
7        Column::new("Status").width(10),
8    ]);
9
10    table.add_row(vec![
11        Cell::new("Ada Lovelace"),
12        Cell::new("Engineer"),
13        Cell::new("Active"),
14    ]);
15
16    table.add_row(vec![
17        Cell::new("Bob"),
18        Cell::new("Support"),
19        Cell::new("Away"),
20    ]);
21
22    println!("{}", table);
23}
More examples
Hide additional examples
examples/styled.rs (lines 22-26)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").bright_cyan().bold().width(0.3),
6        Column::new("Role").width(0.4).truncate(Trunc::Middle),
7        Column::new("Status").bright_yellow().bold().width(0.3),
8    ])
9    .with_section_style(SectionStyle {
10        horiz: "═",
11        mid_left: "╞",
12        mid_right: "╡",
13        mid_joint: "╪",
14    })
15    .with_separator_style(SectionStyle {
16        horiz: "╌",
17        mid_joint: "│",
18        ..SectionStyle::unicode()
19    });
20
21    table.add_section("Team").align(Align::Center);
22    table.add_row(vec![
23        Cell::new("Ada Lovelace"),
24        Cell::new("Principal Engineer"),
25        Cell::new("Active").bright_green(),
26    ]);
27
28    table.add_separator();
29    table.add_row(vec![
30        Cell::new("Bob"),
31        Cell::new("Support"),
32        Cell::new("Away"),
33    ]);
34
35    println!("{}", table);
36}
Source

pub fn add_section(&mut self, title: impl ToString) -> SectionBuilder<'_>

Add a full-width section separator inside the table.

Section rows span the entire table width and are useful for grouping related data visually. The returned builder currently lets you choose the label alignment.

If a default section style was set with Table::with_section_style, it is applied automatically. Call .style() on the returned builder to override it.

Examples found in repository?
examples/styled.rs (line 21)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").bright_cyan().bold().width(0.3),
6        Column::new("Role").width(0.4).truncate(Trunc::Middle),
7        Column::new("Status").bright_yellow().bold().width(0.3),
8    ])
9    .with_section_style(SectionStyle {
10        horiz: "═",
11        mid_left: "╞",
12        mid_right: "╡",
13        mid_joint: "╪",
14    })
15    .with_separator_style(SectionStyle {
16        horiz: "╌",
17        mid_joint: "│",
18        ..SectionStyle::unicode()
19    });
20
21    table.add_section("Team").align(Align::Center);
22    table.add_row(vec![
23        Cell::new("Ada Lovelace"),
24        Cell::new("Principal Engineer"),
25        Cell::new("Active").bright_green(),
26    ]);
27
28    table.add_separator();
29    table.add_row(vec![
30        Cell::new("Bob"),
31        Cell::new("Support"),
32        Cell::new("Away"),
33    ]);
34
35    println!("{}", table);
36}
Source

pub fn add_separator(&mut self) -> SectionBuilder<'_>

Add a full-width separator row with no label.

If a default separator style was set with Table::with_separator_style, it is applied automatically. Call .style() on the returned builder to override it.

Examples found in repository?
examples/styled.rs (line 28)
3fn main() {
4    let mut table = Table::with_columns(vec![
5        Column::new("Name").bright_cyan().bold().width(0.3),
6        Column::new("Role").width(0.4).truncate(Trunc::Middle),
7        Column::new("Status").bright_yellow().bold().width(0.3),
8    ])
9    .with_section_style(SectionStyle {
10        horiz: "═",
11        mid_left: "╞",
12        mid_right: "╡",
13        mid_joint: "╪",
14    })
15    .with_separator_style(SectionStyle {
16        horiz: "╌",
17        mid_joint: "│",
18        ..SectionStyle::unicode()
19    });
20
21    table.add_section("Team").align(Align::Center);
22    table.add_row(vec![
23        Cell::new("Ada Lovelace"),
24        Cell::new("Principal Engineer"),
25        Cell::new("Active").bright_green(),
26    ]);
27
28    table.add_separator();
29    table.add_row(vec![
30        Cell::new("Bob"),
31        Cell::new("Support"),
32        Cell::new("Away"),
33    ]);
34
35    println!("{}", table);
36}
Source

pub fn column<T: Into<ColumnTarget>>(&mut self, target: T) -> ColumnBuilder<'_>

Configure a column using either its zero-based index or exact header text.

Source

pub fn print(&self)

Print the formatted table to standard output.

Source

pub fn render(&self) -> String

Render the formatted table as a single string.

The returned string includes ANSI color codes when styling is applied. Fractional widths are resolved using the current terminal size when it can be detected.

Trait Implementations§

Source§

impl Default for Table

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Table

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Freeze for Table

§

impl RefUnwindSafe for Table

§

impl Send for Table

§

impl Sync for Table

§

impl Unpin for Table

§

impl UnsafeUnpin for Table

§

impl UnwindSafe for Table

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.