demo/
demo.rs

1use pinax::prelude::*;
2
3#[derive(Debug)]
4struct MLPrediction {
5    height: f64,
6    weight: f64,
7    raw_pred: f64,
8    actual: &'static str,
9}
10
11#[derive(Debug)]
12struct Student {
13    id: &'static str,
14    name: &'static str,
15    math: u32,
16    science: u32,
17    english: u32,
18}
19
20#[derive(Debug)]
21struct Task {
22    id: &'static str,
23    priority: &'static str,
24    description: &'static str,
25    status: &'static str,
26    due_date: &'static str,
27}
28
29fn create_ml_table() -> Table {
30    Table::builder()
31        .add_column(Column::new("Height").with_alignment(Alignment::Right))
32        .add_column(Column::new("Weight").with_alignment(Alignment::Right))
33        .add_column(Column::new("Raw Prediction").with_alignment(Alignment::Center))
34        .add_column(Column::new("Predicted").with_alignment(Alignment::Center))
35        .add_column(Column::new("Actual").with_alignment(Alignment::Center))
36        .style(BorderStyle::Double)
37        .build()
38}
39
40fn create_student_table() -> Table {
41    Table::builder()
42        .add_column(Column::new("Student ID").with_alignment(Alignment::Center))
43        .add_column(Column::new("Name").with_alignment(Alignment::Left))
44        .add_column(Column::new("Math").with_alignment(Alignment::Right))
45        .add_column(Column::new("Science").with_alignment(Alignment::Right))
46        .add_column(Column::new("English").with_alignment(Alignment::Right))
47        .add_column(Column::new("Average").with_alignment(Alignment::Right))
48        .style(BorderStyle::Rounded)
49        .build()
50}
51
52fn create_task_table() -> Table {
53    Table::builder()
54        .add_column(Column::new("Task ID").with_alignment(Alignment::Center))
55        .add_column(Column::new("Priority").with_alignment(Alignment::Center))
56        .add_column(Column::new("Description").with_alignment(Alignment::Left))
57        .add_column(Column::new("Status").with_alignment(Alignment::Center))
58        .add_column(Column::new("Due Date").with_alignment(Alignment::Center))
59        .style(BorderStyle::Single)
60        .build()
61}
62
63fn print_ml_predictions() {
64    let predictions = vec![
65        MLPrediction {
66            height: 184.3,
67            weight: 89.5,
68            raw_pred: 0.0000,
69            actual: "Male",
70        },
71        MLPrediction {
72            height: 176.8,
73            weight: 79.2,
74            raw_pred: 0.0036,
75            actual: "Male",
76        },
77        MLPrediction {
78            height: 165.9,
79            weight: 58.7,
80            raw_pred: 0.9214,
81            actual: "Female",
82        },
83        MLPrediction {
84            height: 164.1,
85            weight: 57.8,
86            raw_pred: 0.9441,
87            actual: "Female",
88        },
89        MLPrediction {
90            height: 181.9,
91            weight: 85.8,
92            raw_pred: 0.0001,
93            actual: "Male",
94        },
95        MLPrediction {
96            height: 157.9,
97            weight: 50.6,
98            raw_pred: 0.9893,
99            actual: "Female",
100        },
101        MLPrediction {
102            height: 178.4,
103            weight: 81.6,
104            raw_pred: 0.0010,
105            actual: "Male",
106        },
107    ];
108
109    let mut table = create_ml_table();
110
111    for pred in predictions {
112        let predicted = if pred.raw_pred > 0.5 {
113            "Female"
114        } else {
115            "Male"
116        };
117        table.add_row(vec![
118            format!("{:.1}", pred.height),
119            format!("{:.1}", pred.weight),
120            format!("{:.4}", pred.raw_pred),
121            predicted.to_string(),
122            pred.actual.to_string(),
123        ]);
124    }
125
126    println!("\nšŸ“Š Machine Learning Gender Predictions");
127    println!("=====================================");
128    println!("{}", table);
129}
130
131fn print_student_grades() {
132    let students = vec![
133        Student {
134            id: "2024001",
135            name: "Alice Johnson",
136            math: 95,
137            science: 92,
138            english: 88,
139        },
140        Student {
141            id: "2024002",
142            name: "Bob Smith",
143            math: 88,
144            science: 90,
145            english: 85,
146        },
147        Student {
148            id: "2024003",
149            name: "Carol Williams",
150            math: 92,
151            science: 95,
152            english: 94,
153        },
154        Student {
155            id: "2024004",
156            name: "David Brown",
157            math: 78,
158            science: 82,
159            english: 80,
160        },
161        Student {
162            id: "2024005",
163            name: "Emma Davis",
164            math: 98,
165            science: 96,
166            english: 92,
167        },
168    ];
169
170    let mut table = create_student_table();
171
172    for student in students {
173        let avg = (student.math + student.science + student.english) as f64 / 3.0;
174        table.add_row(vec![
175            student.id.to_string(),
176            student.name.to_string(),
177            student.math.to_string(),
178            student.science.to_string(),
179            student.english.to_string(),
180            format!("{:.1}", avg),
181        ]);
182    }
183
184    println!("\nšŸ“š Student Grade Report");
185    println!("=====================");
186    println!("{}", table);
187}
188
189fn print_project_status() {
190    let tasks = vec![
191        Task {
192            id: "TASK-01",
193            priority: "HIGH",
194            description: "Implement user authentication",
195            status: "In Progress",
196            due_date: "2024-04-01",
197        },
198        Task {
199            id: "TASK-02",
200            priority: "MEDIUM",
201            description: "Design database schema",
202            status: "Completed",
203            due_date: "2024-03-25",
204        },
205        Task {
206            id: "TASK-03",
207            priority: "HIGH",
208            description: "API documentation",
209            status: "Pending",
210            due_date: "2024-04-05",
211        },
212        Task {
213            id: "TASK-04",
214            priority: "LOW",
215            description: "Unit test coverage",
216            status: "Not Started",
217            due_date: "2024-04-10",
218        },
219        Task {
220            id: "TASK-05",
221            priority: "MEDIUM",
222            description: "Performance optimization",
223            status: "In Progress",
224            due_date: "2024-04-15",
225        },
226    ];
227
228    let mut table = create_task_table();
229
230    for task in tasks {
231        table.add_row(vec![
232            task.id.to_string(),
233            task.priority.to_string(),
234            task.description.to_string(),
235            task.status.to_string(),
236            task.due_date.to_string(),
237        ]);
238    }
239
240    println!("\nšŸ“‹ Project Task Status");
241    println!("====================");
242    println!("{}", table);
243}
244
245fn print_grid_demo() {
246    // Simple 3x3 Grid Example
247    let mut simple_grid = Grid::builder()
248        .dimensions(3, 3)
249        .style(BorderStyle::Single)
250        .build();
251
252    simple_grid.set(0, 0, "1");
253    simple_grid.set(0, 1, "2");
254    simple_grid.set(0, 2, "3");
255    simple_grid.set(1, 1, "Center");
256    simple_grid.set(2, 0, "7");
257    simple_grid.set(2, 1, "8");
258    simple_grid.set(2, 2, "9");
259
260    println!("\nšŸ“± Simple 3x3 Grid");
261    println!("================");
262    println!("{}", simple_grid);
263
264    // Calendar-like Grid
265    let mut calendar = Grid::builder()
266        .dimensions(5, 7)
267        .style(BorderStyle::Double)
268        .build();
269
270    // Headers
271    let days = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
272    for (i, day) in days.iter().enumerate() {
273        calendar.set(0, i, day.to_string());
274    }
275
276    // Calendar dates
277    let dates = [
278        ["1", "2", "3", "4", "5", "6", "7"],
279        ["8", "9", "10", "11", "12", "13", "14"],
280        ["15", "16", "17", "18", "19", "20", "21"],
281        ["22", "23", "24", "25", "26", "27", "28"],
282    ];
283
284    for (row, week) in dates.iter().enumerate() {
285        for (col, date) in week.iter().enumerate() {
286            calendar.set(row + 1, col, date.to_string());
287        }
288    }
289
290    println!("\nšŸ“… Calendar Grid Example");
291    println!("=====================");
292    println!("{}", calendar);
293}
294
295fn print_panel_demo() {
296    let welcome_panel = Panel::new(
297        "Welcome to Pinax - a powerful terminal UI toolkit for Rust! \
298        This panel demonstrates text wrapping and border styling capabilities. \
299        Long text will automatically wrap to fit within the specified width.",
300    )
301    .with_title("Welcome")
302    .with_width(60);
303
304    let stats_panel = Panel::new(
305        "šŸ“ˆ Active Users: 1,234\n\
306         šŸ”„ Server Uptime: 99.9%\n\
307         šŸ’¾ Memory Usage: 2.1GB\n\
308         šŸš€ Response Time: 45ms",
309    )
310    .with_title("System Stats")
311    .with_width(30);
312
313    println!("\nšŸ“¦ Panel Examples");
314    println!("===============");
315    println!("{}", welcome_panel);
316    println!("\n{}", stats_panel);
317}
318
319fn print_chart_demo() {
320    // Line Chart Example
321    let mut line_chart = Chart::new(ChartType::Line)
322        .with_height(15)
323        .with_title("Monthly Revenue (in $K)");
324
325    line_chart.add_data_point("Jan", 10.0);
326    line_chart.add_data_point("Feb", 15.0);
327    line_chart.add_data_point("Mar", 13.0);
328    line_chart.add_data_point("Apr", 17.0);
329    line_chart.add_data_point("May", 20.0);
330    line_chart.add_data_point("Jun", 18.0);
331
332    println!("\n{}", line_chart);
333
334    // Bar Chart Example
335    let mut bar_chart = Chart::new(ChartType::Bar)
336        .with_title("Daily Activity (Tasks Completed)");
337        
338    bar_chart.add_data_point("Mon", 45.0);
339    bar_chart.add_data_point("Tue", 30.0);
340    bar_chart.add_data_point("Wed", 60.0);
341    bar_chart.add_data_point("Thu", 35.0);
342    bar_chart.add_data_point("Fri", 50.0);
343
344    println!("\n{}", bar_chart);
345}
346
347fn main() {
348    println!("🌟 Pinax Table Formatting Demo 🌟");
349    println!("================================");
350
351    print_ml_predictions();
352    println!();
353
354    print_student_grades();
355    println!();
356
357    print_project_status();
358    println!();
359
360    print_grid_demo();
361    println!();
362
363    // Add the new demo functions
364    print_panel_demo();
365    println!();
366
367    print_chart_demo();
368}