šØļø Project: Table Printer
š Lesson 8: Polish Your CLI
ā± 25-30 min
š Intermediate
šÆ Objective
Build a formatted table printer that displays data in clean, aligned columns with borders, headers, and customizable alignment. Perfect for CLI data display.
Requirements
- Create a
TablePrinterclass that formats data into tables - Support column headers with customizable alignment (left, center, right)
- Use
StringBufferfor efficient table building - Support different border styles (single, double, rounded, none)
- Auto-size columns based on content width
// Step 1: Alignment enum
enum TextAlignment { left, center, right }
// Step 2: Border style enum
enum BorderStyle { single, double, rounded, simple, none }
// Step 3: Column definition
class Column {
final String header;
final TextAlignment alignment;
final int? fixedWidth;
Column(this.header, {this.alignment = TextAlignment.left, this.fixedWidth});
}
// Step 4: Table printer class
class TablePrinter {
final List columns;
final BorderStyle borderStyle;
String _title = '';
TablePrinter(this.columns, {this.borderStyle = BorderStyle.single});
void setTitle(String title) {
_title = title;
}
String render(List
- > rows) {
final buffer = StringBuffer();
// Calculate column widths
List
- > rows) {
List