Skip to main content

Table

Struct Table 

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

Implementations§

Source§

impl Table

Source

pub fn new() -> Self

Examples found in repository?
examples/table.rs (line 16)
13fn demo_styles() {
14    // Basic table with Modern style
15    println!("=== Modern Style ===");
16    let mut table = Table::new();
17    table.set_style(TableStyle::Modern);
18    table.set_headers(["Name", "Age", "City"]);
19    table.add_row(["Kelana", "30", "Berlin"]);
20    table.add_row(["Kata", "25", "Yogyakarta"]);
21    table.add_row(["Cherry Blossom", "35", "Bikini Bottom"]);
22    table.print();
23
24    // Classic style
25    println!("\n=== Classic Style ===");
26    table.set_style(TableStyle::Classic);
27    table.print();
28
29    // Minimal style
30    println!("\n=== Minimal Style ===");
31    table.set_style(TableStyle::Minimal);
32    table.print();
33
34    // Compact style
35    println!("\n=== Compact Style ===");
36    table.set_style(TableStyle::Compact);
37    table.print();
38
39    // Markdown style
40    println!("\n=== Markdown Style ===");
41    table.set_style(TableStyle::Markdown);
42    table.print();
43}
44
45fn demo_builder() {
46    // Using TableBuilder
47    println!("\n=== TableBuilder with Constraints ===");
48    TableBuilder::new()
49        .style(TableStyle::Modern)
50        .header(["ID", "Name", "Score"])
51        .constrain(0, WidthConstraint::Fixed(5))
52        .constrain(1, WidthConstraint::Min(15))
53        .align(2, Alignment::Right)
54        .rows([
55            ["1", "Kelana", "95.5"],
56            ["2", "Kata", "87.2"],
57            ["3", "Cherry Blossom", "92.0"],
58        ])
59        .print();
60}
61
62fn demo_colspan() {
63    // Colspan example
64    println!("\n=== Colspan Example ===");
65    let mut table = Table::new();
66    table.set_style(TableStyle::Modern);
67
68    // Header row spanning all 5 columns
69    let mut header = Row::new();
70    let mut title_cell = Cell::new("span all 5 columns", Alignment::Left);
71    title_cell.set_span(5);
72    header.push(title_cell);
73    table.set_headers(header);
74
75    // Row: span 4 columns + 1 column
76    let mut row1 = Row::new();
77    let mut span4 = Cell::new("span 4 columns", Alignment::Left);
78    span4.set_span(4);
79    row1.push(span4);
80    row1.push(Cell::new("just 1 column", Alignment::Left));
81    table.add_row(row1);
82
83    // Row: span 3 columns + span 2 columns
84    let mut row2 = Row::new();
85    let mut span3_left = Cell::new("span 3 columns", Alignment::Left);
86    span3_left.set_span(3);
87    row2.push(span3_left);
88    let mut span2_right = Cell::new("span 2 columns", Alignment::Left);
89    span2_right.set_span(2);
90    row2.push(span2_right);
91    table.add_row(row2);
92
93    // Row: span 2 columns + span 3 columns
94    let mut row3 = Row::new();
95    let mut span2_left = Cell::new("span 2 columns", Alignment::Left);
96    span2_left.set_span(2);
97    row3.push(span2_left);
98    let mut span3_right = Cell::new("span 3 columns", Alignment::Left);
99    span3_right.set_span(3);
100    row3.push(span3_right);
101    table.add_row(row3);
102
103    // Row: 1 column + span 4 columns
104    let mut row4 = Row::new();
105    row4.push(Cell::new("just 1 column", Alignment::Left));
106    let mut span4_right = Cell::new("span 4 columns", Alignment::Left);
107    span4_right.set_span(4);
108    row4.push(span4_right);
109    table.add_row(row4);
110
111    // Row: all 5 individual columns
112    table.add_row([
113        "just 1 column",
114        "just 1 column",
115        "just 1 column",
116        "just 1 column",
117        "just 1 column",
118    ]);
119
120    table.print();
121}
122
123fn demo_invoice() {
124    // Invoice style colspan example
125    println!("\n=== Invoice Style with Colspan ===");
126    let mut invoice = Table::new();
127    invoice.set_style(TableStyle::Modern);
128
129    // Invoice header spanning all columns
130    let mut inv_header = Row::new();
131    let mut inv_title = Cell::new("INVOICE #2024-001", Alignment::Center);
132    inv_title.set_span(4);
133    inv_header.push(inv_title);
134    invoice.set_headers(inv_header);
135
136    // Column headers
137    invoice.add_row(Row::with_alignment(
138        ["Item", "Qty", "Price", "Total"],
139        Alignment::Center,
140    ));
141
142    // Line items
143    invoice.add_row(["Widget A", "5", "$10", "$50"]);
144    invoice.add_row(["Widget B", "3", "$15", "$45"]);
145    invoice.add_row(["Service", "1", "$25", "$25"]);
146
147    // Subtotal row
148    let mut subtotal = Row::new();
149    let mut subtotal_label = Cell::new("Subtotal:", Alignment::Right);
150    subtotal_label.set_span(3);
151    subtotal.push(subtotal_label);
152    subtotal.push(Cell::new("$120", Alignment::Right));
153    invoice.add_row(subtotal);
154
155    // Tax row
156    let mut tax = Row::new();
157    let mut tax_label = Cell::new("Tax (10%):", Alignment::Right);
158    tax_label.set_span(3);
159    tax.push(tax_label);
160    tax.push(Cell::new("$12", Alignment::Right));
161    invoice.add_row(tax);
162
163    // Grand total row
164    let mut grand_total = Row::new();
165    let mut total_label = Cell::new("TOTAL:", Alignment::Right);
166    total_label.set_span(3);
167    grand_total.push(total_label);
168    grand_total.push(Cell::new("$132", Alignment::Right));
169    invoice.add_row(grand_total);
170
171    invoice.print();
172}
Source

pub fn set_headers<R: Into<Row>>(&mut self, headers: R)

Examples found in repository?
examples/table.rs (line 18)
13fn demo_styles() {
14    // Basic table with Modern style
15    println!("=== Modern Style ===");
16    let mut table = Table::new();
17    table.set_style(TableStyle::Modern);
18    table.set_headers(["Name", "Age", "City"]);
19    table.add_row(["Kelana", "30", "Berlin"]);
20    table.add_row(["Kata", "25", "Yogyakarta"]);
21    table.add_row(["Cherry Blossom", "35", "Bikini Bottom"]);
22    table.print();
23
24    // Classic style
25    println!("\n=== Classic Style ===");
26    table.set_style(TableStyle::Classic);
27    table.print();
28
29    // Minimal style
30    println!("\n=== Minimal Style ===");
31    table.set_style(TableStyle::Minimal);
32    table.print();
33
34    // Compact style
35    println!("\n=== Compact Style ===");
36    table.set_style(TableStyle::Compact);
37    table.print();
38
39    // Markdown style
40    println!("\n=== Markdown Style ===");
41    table.set_style(TableStyle::Markdown);
42    table.print();
43}
44
45fn demo_builder() {
46    // Using TableBuilder
47    println!("\n=== TableBuilder with Constraints ===");
48    TableBuilder::new()
49        .style(TableStyle::Modern)
50        .header(["ID", "Name", "Score"])
51        .constrain(0, WidthConstraint::Fixed(5))
52        .constrain(1, WidthConstraint::Min(15))
53        .align(2, Alignment::Right)
54        .rows([
55            ["1", "Kelana", "95.5"],
56            ["2", "Kata", "87.2"],
57            ["3", "Cherry Blossom", "92.0"],
58        ])
59        .print();
60}
61
62fn demo_colspan() {
63    // Colspan example
64    println!("\n=== Colspan Example ===");
65    let mut table = Table::new();
66    table.set_style(TableStyle::Modern);
67
68    // Header row spanning all 5 columns
69    let mut header = Row::new();
70    let mut title_cell = Cell::new("span all 5 columns", Alignment::Left);
71    title_cell.set_span(5);
72    header.push(title_cell);
73    table.set_headers(header);
74
75    // Row: span 4 columns + 1 column
76    let mut row1 = Row::new();
77    let mut span4 = Cell::new("span 4 columns", Alignment::Left);
78    span4.set_span(4);
79    row1.push(span4);
80    row1.push(Cell::new("just 1 column", Alignment::Left));
81    table.add_row(row1);
82
83    // Row: span 3 columns + span 2 columns
84    let mut row2 = Row::new();
85    let mut span3_left = Cell::new("span 3 columns", Alignment::Left);
86    span3_left.set_span(3);
87    row2.push(span3_left);
88    let mut span2_right = Cell::new("span 2 columns", Alignment::Left);
89    span2_right.set_span(2);
90    row2.push(span2_right);
91    table.add_row(row2);
92
93    // Row: span 2 columns + span 3 columns
94    let mut row3 = Row::new();
95    let mut span2_left = Cell::new("span 2 columns", Alignment::Left);
96    span2_left.set_span(2);
97    row3.push(span2_left);
98    let mut span3_right = Cell::new("span 3 columns", Alignment::Left);
99    span3_right.set_span(3);
100    row3.push(span3_right);
101    table.add_row(row3);
102
103    // Row: 1 column + span 4 columns
104    let mut row4 = Row::new();
105    row4.push(Cell::new("just 1 column", Alignment::Left));
106    let mut span4_right = Cell::new("span 4 columns", Alignment::Left);
107    span4_right.set_span(4);
108    row4.push(span4_right);
109    table.add_row(row4);
110
111    // Row: all 5 individual columns
112    table.add_row([
113        "just 1 column",
114        "just 1 column",
115        "just 1 column",
116        "just 1 column",
117        "just 1 column",
118    ]);
119
120    table.print();
121}
122
123fn demo_invoice() {
124    // Invoice style colspan example
125    println!("\n=== Invoice Style with Colspan ===");
126    let mut invoice = Table::new();
127    invoice.set_style(TableStyle::Modern);
128
129    // Invoice header spanning all columns
130    let mut inv_header = Row::new();
131    let mut inv_title = Cell::new("INVOICE #2024-001", Alignment::Center);
132    inv_title.set_span(4);
133    inv_header.push(inv_title);
134    invoice.set_headers(inv_header);
135
136    // Column headers
137    invoice.add_row(Row::with_alignment(
138        ["Item", "Qty", "Price", "Total"],
139        Alignment::Center,
140    ));
141
142    // Line items
143    invoice.add_row(["Widget A", "5", "$10", "$50"]);
144    invoice.add_row(["Widget B", "3", "$15", "$45"]);
145    invoice.add_row(["Service", "1", "$25", "$25"]);
146
147    // Subtotal row
148    let mut subtotal = Row::new();
149    let mut subtotal_label = Cell::new("Subtotal:", Alignment::Right);
150    subtotal_label.set_span(3);
151    subtotal.push(subtotal_label);
152    subtotal.push(Cell::new("$120", Alignment::Right));
153    invoice.add_row(subtotal);
154
155    // Tax row
156    let mut tax = Row::new();
157    let mut tax_label = Cell::new("Tax (10%):", Alignment::Right);
158    tax_label.set_span(3);
159    tax.push(tax_label);
160    tax.push(Cell::new("$12", Alignment::Right));
161    invoice.add_row(tax);
162
163    // Grand total row
164    let mut grand_total = Row::new();
165    let mut total_label = Cell::new("TOTAL:", Alignment::Right);
166    total_label.set_span(3);
167    grand_total.push(total_label);
168    grand_total.push(Cell::new("$132", Alignment::Right));
169    invoice.add_row(grand_total);
170
171    invoice.print();
172}
Source

pub fn add_row<R: Into<Row>>(&mut self, row: R)

Examples found in repository?
examples/table.rs (line 19)
13fn demo_styles() {
14    // Basic table with Modern style
15    println!("=== Modern Style ===");
16    let mut table = Table::new();
17    table.set_style(TableStyle::Modern);
18    table.set_headers(["Name", "Age", "City"]);
19    table.add_row(["Kelana", "30", "Berlin"]);
20    table.add_row(["Kata", "25", "Yogyakarta"]);
21    table.add_row(["Cherry Blossom", "35", "Bikini Bottom"]);
22    table.print();
23
24    // Classic style
25    println!("\n=== Classic Style ===");
26    table.set_style(TableStyle::Classic);
27    table.print();
28
29    // Minimal style
30    println!("\n=== Minimal Style ===");
31    table.set_style(TableStyle::Minimal);
32    table.print();
33
34    // Compact style
35    println!("\n=== Compact Style ===");
36    table.set_style(TableStyle::Compact);
37    table.print();
38
39    // Markdown style
40    println!("\n=== Markdown Style ===");
41    table.set_style(TableStyle::Markdown);
42    table.print();
43}
44
45fn demo_builder() {
46    // Using TableBuilder
47    println!("\n=== TableBuilder with Constraints ===");
48    TableBuilder::new()
49        .style(TableStyle::Modern)
50        .header(["ID", "Name", "Score"])
51        .constrain(0, WidthConstraint::Fixed(5))
52        .constrain(1, WidthConstraint::Min(15))
53        .align(2, Alignment::Right)
54        .rows([
55            ["1", "Kelana", "95.5"],
56            ["2", "Kata", "87.2"],
57            ["3", "Cherry Blossom", "92.0"],
58        ])
59        .print();
60}
61
62fn demo_colspan() {
63    // Colspan example
64    println!("\n=== Colspan Example ===");
65    let mut table = Table::new();
66    table.set_style(TableStyle::Modern);
67
68    // Header row spanning all 5 columns
69    let mut header = Row::new();
70    let mut title_cell = Cell::new("span all 5 columns", Alignment::Left);
71    title_cell.set_span(5);
72    header.push(title_cell);
73    table.set_headers(header);
74
75    // Row: span 4 columns + 1 column
76    let mut row1 = Row::new();
77    let mut span4 = Cell::new("span 4 columns", Alignment::Left);
78    span4.set_span(4);
79    row1.push(span4);
80    row1.push(Cell::new("just 1 column", Alignment::Left));
81    table.add_row(row1);
82
83    // Row: span 3 columns + span 2 columns
84    let mut row2 = Row::new();
85    let mut span3_left = Cell::new("span 3 columns", Alignment::Left);
86    span3_left.set_span(3);
87    row2.push(span3_left);
88    let mut span2_right = Cell::new("span 2 columns", Alignment::Left);
89    span2_right.set_span(2);
90    row2.push(span2_right);
91    table.add_row(row2);
92
93    // Row: span 2 columns + span 3 columns
94    let mut row3 = Row::new();
95    let mut span2_left = Cell::new("span 2 columns", Alignment::Left);
96    span2_left.set_span(2);
97    row3.push(span2_left);
98    let mut span3_right = Cell::new("span 3 columns", Alignment::Left);
99    span3_right.set_span(3);
100    row3.push(span3_right);
101    table.add_row(row3);
102
103    // Row: 1 column + span 4 columns
104    let mut row4 = Row::new();
105    row4.push(Cell::new("just 1 column", Alignment::Left));
106    let mut span4_right = Cell::new("span 4 columns", Alignment::Left);
107    span4_right.set_span(4);
108    row4.push(span4_right);
109    table.add_row(row4);
110
111    // Row: all 5 individual columns
112    table.add_row([
113        "just 1 column",
114        "just 1 column",
115        "just 1 column",
116        "just 1 column",
117        "just 1 column",
118    ]);
119
120    table.print();
121}
122
123fn demo_invoice() {
124    // Invoice style colspan example
125    println!("\n=== Invoice Style with Colspan ===");
126    let mut invoice = Table::new();
127    invoice.set_style(TableStyle::Modern);
128
129    // Invoice header spanning all columns
130    let mut inv_header = Row::new();
131    let mut inv_title = Cell::new("INVOICE #2024-001", Alignment::Center);
132    inv_title.set_span(4);
133    inv_header.push(inv_title);
134    invoice.set_headers(inv_header);
135
136    // Column headers
137    invoice.add_row(Row::with_alignment(
138        ["Item", "Qty", "Price", "Total"],
139        Alignment::Center,
140    ));
141
142    // Line items
143    invoice.add_row(["Widget A", "5", "$10", "$50"]);
144    invoice.add_row(["Widget B", "3", "$15", "$45"]);
145    invoice.add_row(["Service", "1", "$25", "$25"]);
146
147    // Subtotal row
148    let mut subtotal = Row::new();
149    let mut subtotal_label = Cell::new("Subtotal:", Alignment::Right);
150    subtotal_label.set_span(3);
151    subtotal.push(subtotal_label);
152    subtotal.push(Cell::new("$120", Alignment::Right));
153    invoice.add_row(subtotal);
154
155    // Tax row
156    let mut tax = Row::new();
157    let mut tax_label = Cell::new("Tax (10%):", Alignment::Right);
158    tax_label.set_span(3);
159    tax.push(tax_label);
160    tax.push(Cell::new("$12", Alignment::Right));
161    invoice.add_row(tax);
162
163    // Grand total row
164    let mut grand_total = Row::new();
165    let mut total_label = Cell::new("TOTAL:", Alignment::Right);
166    total_label.set_span(3);
167    grand_total.push(total_label);
168    grand_total.push(Cell::new("$132", Alignment::Right));
169    invoice.add_row(grand_total);
170
171    invoice.print();
172}
Source

pub fn insert_row<R: Into<Row>>(&mut self, index: usize, row: R)

Source

pub fn remove_row(&mut self, index: usize) -> Option<Row>

Source

pub fn sort(&mut self, column: usize)

Sorts the rows by the content of the specified column in ascending order. Uses lexicographic (string) comparison.

Source

pub fn sort_desc(&mut self, column: usize)

Sorts the rows by the content of the specified column in descending order. Uses lexicographic (string) comparison.

Source

pub fn sort_num(&mut self, column: usize)

Sorts the rows by the specified column, treating cell content as numbers. Non-numeric values are treated as 0.0.

This method pre-parses numeric values before sorting for better performance on large tables.

Source

pub fn sort_num_desc(&mut self, column: usize)

Sorts the rows by the specified column in descending order, treating content as numbers. Non-numeric values are treated as 0.0.

This method pre-parses numeric values before sorting for better performance on large tables.

Source

pub fn sort_by<F>(&mut self, compare: F)
where F: FnMut(&Row, &Row) -> Ordering,

Sorts the rows using a custom comparison function.

Source

pub fn filter<F>(&mut self, predicate: F)
where F: FnMut(&Row) -> bool,

Filters rows in place, keeping only those for which the predicate returns true. Headers are not affected by filtering.

Source

pub fn filter_eq(&mut self, column: usize, value: &str)

Filters rows by the content of a specific column. Keeps rows where the column content equals the given value.

Source

pub fn filter_col<F>(&mut self, column: usize, predicate: F)
where F: Fn(&str) -> bool,

Filters rows by the content of a specific column using a predicate. Keeps rows where the predicate returns true for the column content.

Source

pub fn filter_has(&mut self, column: usize, substring: &str)

Filters rows where the specified column content contains the given substring.

Source

pub fn filtered<F>(&self, predicate: F) -> Self
where F: FnMut(&Row) -> bool,

Returns a new table containing only rows that match the predicate. The original table is not modified. Headers, style, and other settings are copied.

Source

pub fn add_column(&mut self, values: &[&str], alignment: Alignment)

Adds a new column to the table with the given values. The first value becomes the header (if headers exist), and the rest become row values. If there are more rows than values, empty cells are added. If there are more values than rows, extra values are ignored.

Source

pub fn insert_column( &mut self, index: usize, values: &[&str], alignment: Alignment, )

Inserts a new column at the specified index. The first value becomes the header (if headers exist), and the rest become row values.

Source

pub fn remove_column(&mut self, index: usize) -> bool

Removes a column at the specified index from all rows and headers. Returns true if the column was removed, false if the index was out of bounds.

Source

pub fn cols(&self) -> usize

Returns the number of columns in the table. Based on the maximum cell count across headers and all rows.

Source

pub fn set_style(&mut self, style: TableStyle)

Examples found in repository?
examples/table.rs (line 17)
13fn demo_styles() {
14    // Basic table with Modern style
15    println!("=== Modern Style ===");
16    let mut table = Table::new();
17    table.set_style(TableStyle::Modern);
18    table.set_headers(["Name", "Age", "City"]);
19    table.add_row(["Kelana", "30", "Berlin"]);
20    table.add_row(["Kata", "25", "Yogyakarta"]);
21    table.add_row(["Cherry Blossom", "35", "Bikini Bottom"]);
22    table.print();
23
24    // Classic style
25    println!("\n=== Classic Style ===");
26    table.set_style(TableStyle::Classic);
27    table.print();
28
29    // Minimal style
30    println!("\n=== Minimal Style ===");
31    table.set_style(TableStyle::Minimal);
32    table.print();
33
34    // Compact style
35    println!("\n=== Compact Style ===");
36    table.set_style(TableStyle::Compact);
37    table.print();
38
39    // Markdown style
40    println!("\n=== Markdown Style ===");
41    table.set_style(TableStyle::Markdown);
42    table.print();
43}
44
45fn demo_builder() {
46    // Using TableBuilder
47    println!("\n=== TableBuilder with Constraints ===");
48    TableBuilder::new()
49        .style(TableStyle::Modern)
50        .header(["ID", "Name", "Score"])
51        .constrain(0, WidthConstraint::Fixed(5))
52        .constrain(1, WidthConstraint::Min(15))
53        .align(2, Alignment::Right)
54        .rows([
55            ["1", "Kelana", "95.5"],
56            ["2", "Kata", "87.2"],
57            ["3", "Cherry Blossom", "92.0"],
58        ])
59        .print();
60}
61
62fn demo_colspan() {
63    // Colspan example
64    println!("\n=== Colspan Example ===");
65    let mut table = Table::new();
66    table.set_style(TableStyle::Modern);
67
68    // Header row spanning all 5 columns
69    let mut header = Row::new();
70    let mut title_cell = Cell::new("span all 5 columns", Alignment::Left);
71    title_cell.set_span(5);
72    header.push(title_cell);
73    table.set_headers(header);
74
75    // Row: span 4 columns + 1 column
76    let mut row1 = Row::new();
77    let mut span4 = Cell::new("span 4 columns", Alignment::Left);
78    span4.set_span(4);
79    row1.push(span4);
80    row1.push(Cell::new("just 1 column", Alignment::Left));
81    table.add_row(row1);
82
83    // Row: span 3 columns + span 2 columns
84    let mut row2 = Row::new();
85    let mut span3_left = Cell::new("span 3 columns", Alignment::Left);
86    span3_left.set_span(3);
87    row2.push(span3_left);
88    let mut span2_right = Cell::new("span 2 columns", Alignment::Left);
89    span2_right.set_span(2);
90    row2.push(span2_right);
91    table.add_row(row2);
92
93    // Row: span 2 columns + span 3 columns
94    let mut row3 = Row::new();
95    let mut span2_left = Cell::new("span 2 columns", Alignment::Left);
96    span2_left.set_span(2);
97    row3.push(span2_left);
98    let mut span3_right = Cell::new("span 3 columns", Alignment::Left);
99    span3_right.set_span(3);
100    row3.push(span3_right);
101    table.add_row(row3);
102
103    // Row: 1 column + span 4 columns
104    let mut row4 = Row::new();
105    row4.push(Cell::new("just 1 column", Alignment::Left));
106    let mut span4_right = Cell::new("span 4 columns", Alignment::Left);
107    span4_right.set_span(4);
108    row4.push(span4_right);
109    table.add_row(row4);
110
111    // Row: all 5 individual columns
112    table.add_row([
113        "just 1 column",
114        "just 1 column",
115        "just 1 column",
116        "just 1 column",
117        "just 1 column",
118    ]);
119
120    table.print();
121}
122
123fn demo_invoice() {
124    // Invoice style colspan example
125    println!("\n=== Invoice Style with Colspan ===");
126    let mut invoice = Table::new();
127    invoice.set_style(TableStyle::Modern);
128
129    // Invoice header spanning all columns
130    let mut inv_header = Row::new();
131    let mut inv_title = Cell::new("INVOICE #2024-001", Alignment::Center);
132    inv_title.set_span(4);
133    inv_header.push(inv_title);
134    invoice.set_headers(inv_header);
135
136    // Column headers
137    invoice.add_row(Row::with_alignment(
138        ["Item", "Qty", "Price", "Total"],
139        Alignment::Center,
140    ));
141
142    // Line items
143    invoice.add_row(["Widget A", "5", "$10", "$50"]);
144    invoice.add_row(["Widget B", "3", "$15", "$45"]);
145    invoice.add_row(["Service", "1", "$25", "$25"]);
146
147    // Subtotal row
148    let mut subtotal = Row::new();
149    let mut subtotal_label = Cell::new("Subtotal:", Alignment::Right);
150    subtotal_label.set_span(3);
151    subtotal.push(subtotal_label);
152    subtotal.push(Cell::new("$120", Alignment::Right));
153    invoice.add_row(subtotal);
154
155    // Tax row
156    let mut tax = Row::new();
157    let mut tax_label = Cell::new("Tax (10%):", Alignment::Right);
158    tax_label.set_span(3);
159    tax.push(tax_label);
160    tax.push(Cell::new("$12", Alignment::Right));
161    invoice.add_row(tax);
162
163    // Grand total row
164    let mut grand_total = Row::new();
165    let mut total_label = Cell::new("TOTAL:", Alignment::Right);
166    total_label.set_span(3);
167    grand_total.push(total_label);
168    grand_total.push(Cell::new("$132", Alignment::Right));
169    invoice.add_row(grand_total);
170
171    invoice.print();
172}
Source

pub fn set_padding(&mut self, padding: Padding)

Source

pub fn spacing(&mut self, spacing: usize)

Source

pub fn align(&mut self, column: usize, alignment: Alignment)

Source

pub fn valign(&mut self, alignment: VerticalAlignment)

Source

pub fn constrain(&mut self, constraint: WidthConstraint)

Source

pub fn set_constraint(&mut self, column: usize, constraint: WidthConstraint)

Source

pub fn constraints(&self) -> &[WidthConstraint]

Source

pub fn rows(&self) -> &[Row]

Source

pub fn headers(&self) -> Option<&Row>

Source

pub fn style(&self) -> TableStyle

Source

pub fn padding(&self) -> Padding

Source

pub fn get_spacing(&self) -> usize

Source

pub fn get_align(&self, column: usize) -> Option<Alignment>

Source

pub fn get_valign(&self) -> VerticalAlignment

Source

pub fn len(&self) -> usize

Source

pub fn is_empty(&self) -> bool

Source

pub fn row<R: Into<Row>>(self, cells: R) -> Self

Source

pub fn header<R: Into<Row>>(self, headers: R) -> Self

Source

pub fn truncate(self, limit: usize) -> Self

Source

pub fn print(&self)

Examples found in repository?
examples/table.rs (line 22)
13fn demo_styles() {
14    // Basic table with Modern style
15    println!("=== Modern Style ===");
16    let mut table = Table::new();
17    table.set_style(TableStyle::Modern);
18    table.set_headers(["Name", "Age", "City"]);
19    table.add_row(["Kelana", "30", "Berlin"]);
20    table.add_row(["Kata", "25", "Yogyakarta"]);
21    table.add_row(["Cherry Blossom", "35", "Bikini Bottom"]);
22    table.print();
23
24    // Classic style
25    println!("\n=== Classic Style ===");
26    table.set_style(TableStyle::Classic);
27    table.print();
28
29    // Minimal style
30    println!("\n=== Minimal Style ===");
31    table.set_style(TableStyle::Minimal);
32    table.print();
33
34    // Compact style
35    println!("\n=== Compact Style ===");
36    table.set_style(TableStyle::Compact);
37    table.print();
38
39    // Markdown style
40    println!("\n=== Markdown Style ===");
41    table.set_style(TableStyle::Markdown);
42    table.print();
43}
44
45fn demo_builder() {
46    // Using TableBuilder
47    println!("\n=== TableBuilder with Constraints ===");
48    TableBuilder::new()
49        .style(TableStyle::Modern)
50        .header(["ID", "Name", "Score"])
51        .constrain(0, WidthConstraint::Fixed(5))
52        .constrain(1, WidthConstraint::Min(15))
53        .align(2, Alignment::Right)
54        .rows([
55            ["1", "Kelana", "95.5"],
56            ["2", "Kata", "87.2"],
57            ["3", "Cherry Blossom", "92.0"],
58        ])
59        .print();
60}
61
62fn demo_colspan() {
63    // Colspan example
64    println!("\n=== Colspan Example ===");
65    let mut table = Table::new();
66    table.set_style(TableStyle::Modern);
67
68    // Header row spanning all 5 columns
69    let mut header = Row::new();
70    let mut title_cell = Cell::new("span all 5 columns", Alignment::Left);
71    title_cell.set_span(5);
72    header.push(title_cell);
73    table.set_headers(header);
74
75    // Row: span 4 columns + 1 column
76    let mut row1 = Row::new();
77    let mut span4 = Cell::new("span 4 columns", Alignment::Left);
78    span4.set_span(4);
79    row1.push(span4);
80    row1.push(Cell::new("just 1 column", Alignment::Left));
81    table.add_row(row1);
82
83    // Row: span 3 columns + span 2 columns
84    let mut row2 = Row::new();
85    let mut span3_left = Cell::new("span 3 columns", Alignment::Left);
86    span3_left.set_span(3);
87    row2.push(span3_left);
88    let mut span2_right = Cell::new("span 2 columns", Alignment::Left);
89    span2_right.set_span(2);
90    row2.push(span2_right);
91    table.add_row(row2);
92
93    // Row: span 2 columns + span 3 columns
94    let mut row3 = Row::new();
95    let mut span2_left = Cell::new("span 2 columns", Alignment::Left);
96    span2_left.set_span(2);
97    row3.push(span2_left);
98    let mut span3_right = Cell::new("span 3 columns", Alignment::Left);
99    span3_right.set_span(3);
100    row3.push(span3_right);
101    table.add_row(row3);
102
103    // Row: 1 column + span 4 columns
104    let mut row4 = Row::new();
105    row4.push(Cell::new("just 1 column", Alignment::Left));
106    let mut span4_right = Cell::new("span 4 columns", Alignment::Left);
107    span4_right.set_span(4);
108    row4.push(span4_right);
109    table.add_row(row4);
110
111    // Row: all 5 individual columns
112    table.add_row([
113        "just 1 column",
114        "just 1 column",
115        "just 1 column",
116        "just 1 column",
117        "just 1 column",
118    ]);
119
120    table.print();
121}
122
123fn demo_invoice() {
124    // Invoice style colspan example
125    println!("\n=== Invoice Style with Colspan ===");
126    let mut invoice = Table::new();
127    invoice.set_style(TableStyle::Modern);
128
129    // Invoice header spanning all columns
130    let mut inv_header = Row::new();
131    let mut inv_title = Cell::new("INVOICE #2024-001", Alignment::Center);
132    inv_title.set_span(4);
133    inv_header.push(inv_title);
134    invoice.set_headers(inv_header);
135
136    // Column headers
137    invoice.add_row(Row::with_alignment(
138        ["Item", "Qty", "Price", "Total"],
139        Alignment::Center,
140    ));
141
142    // Line items
143    invoice.add_row(["Widget A", "5", "$10", "$50"]);
144    invoice.add_row(["Widget B", "3", "$15", "$45"]);
145    invoice.add_row(["Service", "1", "$25", "$25"]);
146
147    // Subtotal row
148    let mut subtotal = Row::new();
149    let mut subtotal_label = Cell::new("Subtotal:", Alignment::Right);
150    subtotal_label.set_span(3);
151    subtotal.push(subtotal_label);
152    subtotal.push(Cell::new("$120", Alignment::Right));
153    invoice.add_row(subtotal);
154
155    // Tax row
156    let mut tax = Row::new();
157    let mut tax_label = Cell::new("Tax (10%):", Alignment::Right);
158    tax_label.set_span(3);
159    tax.push(tax_label);
160    tax.push(Cell::new("$12", Alignment::Right));
161    invoice.add_row(tax);
162
163    // Grand total row
164    let mut grand_total = Row::new();
165    let mut total_label = Cell::new("TOTAL:", Alignment::Right);
166    total_label.set_span(3);
167    grand_total.push(total_label);
168    grand_total.push(Cell::new("$132", Alignment::Right));
169    invoice.add_row(grand_total);
170
171    invoice.print();
172}
Source

pub fn render_into(&self, buf: &mut Vec<u8>) -> Result

Renders the table into a provided byte buffer, reusing the allocation.

This method allows for zero-allocation rendering when the buffer is reused across multiple renders, making it ideal for repeated rendering scenarios like pagination or filtering UI.

§Arguments
  • buf - A buffer to render into. Will be cleared and reused.
§Returns
  • Ok(()) if rendering succeeded
§Errors

This function currently never returns an error, but returns Result for future compatibility with potential I/O operations.

§Examples
let mut buffer = Vec::with_capacity(4096);
for item in items {
    buffer.clear();
    table.render_into(&mut buffer)?;
    stdout.write_all(&buffer)?;
}
Source

pub fn format_cell(content: &str, width: usize, alignment: Alignment) -> String

Formats a cell’s content with the given width and alignment.

This is a lower-level function that can be useful for custom formatting needs.

§Arguments
  • content - The cell content to format
  • width - The target width for the formatted output
  • alignment - The alignment to use
§Returns

The formatted string with appropriate padding

§Examples
let formatted = Table::format_cell("test", 10, Alignment::Left);
assert_eq!(formatted, "test      ");
Source

pub fn render(&self) -> String

Source

pub fn render_cached(&self) -> String

Renders the table using cached column widths if available.

This method provides improved performance for repeated renders of the same table. The first call calculates and caches column widths. Subsequent calls reuse the cache until the table is modified.

§Returns

The rendered table as a String

§Examples
let table = Table::new().header(&["A", "B"]).row(&["1", "2"]);
let _first = table.render_cached(); // Calculates and caches widths
let _second = table.render_cached(); // Uses cached widths (faster)
Source

pub fn recalculate_widths(&mut self)

Forces recalculation of column widths on the next render.

This method is primarily useful when the table has been modified externally or when you want to ensure fresh calculations.

§Examples
let mut table = Table::new().header(&["A", "B"]).row(&["1", "2"]);
table.recalculate_widths();

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