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
- Build the schema with
Table::with_columnsorTable::new. - Add rows with
Table::add_row. - Adjust columns with
Table::columnif you need per-column overrides. - Call
Table::renderor 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
impl Table
Sourcepub fn with_columns(columns: impl IntoIterator<Item = Column>) -> Self
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?
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
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}Sourcepub fn with_style(self, style: TableStyle) -> Self
pub fn with_style(self, style: TableStyle) -> Self
Replace the default border style for this table.
Sourcepub fn with_section_style(self, style: SectionStyle) -> Self
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?
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}Sourcepub fn with_separator_style(self, style: SectionStyle) -> Self
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?
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}Sourcepub fn set_columns(&mut self, columns: impl IntoIterator<Item = Column>)
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.
Sourcepub fn add_row(&mut self, row: Vec<Cell>)
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?
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
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}Sourcepub fn add_section(&mut self, title: impl ToString) -> SectionBuilder<'_>
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?
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}Sourcepub fn add_separator(&mut self) -> SectionBuilder<'_>
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?
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}Sourcepub fn column<T: Into<ColumnTarget>>(&mut self, target: T) -> ColumnBuilder<'_>
pub fn column<T: Into<ColumnTarget>>(&mut self, target: T) -> ColumnBuilder<'_>
Configure a column using either its zero-based index or exact header text.