pub struct Table { /* private fields */ }Implementations§
Source§impl Table
impl Table
Sourcepub fn new() -> Self
pub fn new() -> Self
Examples found in repository?
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}Sourcepub fn set_headers<R: Into<Row>>(&mut self, headers: R)
pub fn set_headers<R: Into<Row>>(&mut self, headers: R)
Examples found in repository?
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}Sourcepub fn add_row<R: Into<Row>>(&mut self, row: R)
pub fn add_row<R: Into<Row>>(&mut self, row: R)
Examples found in repository?
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}pub fn insert_row<R: Into<Row>>(&mut self, index: usize, row: R)
pub fn remove_row(&mut self, index: usize) -> Option<Row>
Sourcepub fn sort(&mut self, column: usize)
pub fn sort(&mut self, column: usize)
Sorts the rows by the content of the specified column in ascending order. Uses lexicographic (string) comparison.
Sourcepub fn sort_desc(&mut self, column: usize)
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.
Sourcepub fn sort_num(&mut self, column: usize)
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.
Sourcepub fn sort_num_desc(&mut self, column: usize)
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.
Sourcepub fn filter<F>(&mut self, predicate: F)
pub fn filter<F>(&mut self, predicate: F)
Filters rows in place, keeping only those for which the predicate returns true. Headers are not affected by filtering.
Sourcepub fn filter_eq(&mut self, column: usize, value: &str)
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.
Sourcepub fn filter_col<F>(&mut self, column: usize, predicate: F)
pub fn filter_col<F>(&mut self, column: usize, predicate: F)
Filters rows by the content of a specific column using a predicate. Keeps rows where the predicate returns true for the column content.
Sourcepub fn filter_has(&mut self, column: usize, substring: &str)
pub fn filter_has(&mut self, column: usize, substring: &str)
Filters rows where the specified column content contains the given substring.
Sourcepub fn filtered<F>(&self, predicate: F) -> Self
pub fn filtered<F>(&self, predicate: F) -> Self
Returns a new table containing only rows that match the predicate. The original table is not modified. Headers, style, and other settings are copied.
Sourcepub fn add_column(&mut self, values: &[&str], alignment: Alignment)
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.
Sourcepub fn insert_column(
&mut self,
index: usize,
values: &[&str],
alignment: Alignment,
)
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.
Sourcepub fn remove_column(&mut self, index: usize) -> bool
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.
Sourcepub fn cols(&self) -> usize
pub fn cols(&self) -> usize
Returns the number of columns in the table. Based on the maximum cell count across headers and all rows.
Sourcepub fn set_style(&mut self, style: TableStyle)
pub fn set_style(&mut self, style: TableStyle)
Examples found in repository?
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}pub fn set_padding(&mut self, padding: Padding)
pub fn spacing(&mut self, spacing: usize)
pub fn align(&mut self, column: usize, alignment: Alignment)
pub fn valign(&mut self, alignment: VerticalAlignment)
pub fn constrain(&mut self, constraint: WidthConstraint)
pub fn set_constraint(&mut self, column: usize, constraint: WidthConstraint)
pub fn constraints(&self) -> &[WidthConstraint]
pub fn rows(&self) -> &[Row]
pub fn headers(&self) -> Option<&Row>
pub fn style(&self) -> TableStyle
pub fn padding(&self) -> Padding
pub fn get_spacing(&self) -> usize
pub fn get_align(&self, column: usize) -> Option<Alignment>
pub fn get_valign(&self) -> VerticalAlignment
pub fn len(&self) -> usize
pub fn is_empty(&self) -> bool
pub fn row<R: Into<Row>>(self, cells: R) -> Self
pub fn header<R: Into<Row>>(self, headers: R) -> Self
pub fn truncate(self, limit: usize) -> Self
Sourcepub fn print(&self)
pub fn print(&self)
Examples found in repository?
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}Sourcepub fn render_into(&self, buf: &mut Vec<u8>) -> Result
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)?;
}Sourcepub fn format_cell(content: &str, width: usize, alignment: Alignment) -> String
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 formatwidth- The target width for the formatted outputalignment- 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 ");pub fn render(&self) -> String
Sourcepub fn render_cached(&self) -> String
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)Sourcepub fn recalculate_widths(&mut self)
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();