1use crabular::{Alignment, Cell, Row, Table, TableBuilder, TableStyle, WidthConstraint};
4
5fn main() {
6 demo_styles();
7 demo_builder();
8 demo_colspan();
9 demo_invoice();
10 demo_truncate();
11}
12
13fn demo_styles() {
14 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 println!("\n=== Classic Style ===");
26 table.set_style(TableStyle::Classic);
27 table.print();
28
29 println!("\n=== Minimal Style ===");
31 table.set_style(TableStyle::Minimal);
32 table.print();
33
34 println!("\n=== Compact Style ===");
36 table.set_style(TableStyle::Compact);
37 table.print();
38
39 println!("\n=== Markdown Style ===");
41 table.set_style(TableStyle::Markdown);
42 table.print();
43}
44
45fn demo_builder() {
46 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 println!("\n=== Colspan Example ===");
65 let mut table = Table::new();
66 table.set_style(TableStyle::Modern);
67
68 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 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 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 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 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 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 println!("\n=== Invoice Style with Colspan ===");
126 let mut invoice = Table::new();
127 invoice.set_style(TableStyle::Modern);
128
129 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 invoice.add_row(Row::with_alignment(
138 ["Item", "Qty", "Price", "Total"],
139 Alignment::Center,
140 ));
141
142 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 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 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 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}
173
174fn demo_truncate() {
175 println!("\n=== Truncate Example ===");
176 TableBuilder::new()
177 .style(TableStyle::Modern)
178 .header(["ID", "Name", "Description", "Score"])
179 .truncate(20)
180 .rows([
181 [
182 "1",
183 "Kata",
184 "A very long description that should be truncated",
185 "95.5",
186 ],
187 ["2", "Kelana", "Short desc", "87.2"],
188 [
189 "3",
190 "Squidward",
191 "Another extremely long description text here",
192 "92.0",
193 ],
194 ])
195 .print();
196}