1pub fn print_table(headers: &[&str], rows: &[Vec<String>]) {
4 if rows.is_empty() {
5 return;
6 }
7 let ncols = headers.len();
8 let widths: Vec<usize> = (0..ncols)
9 .map(|i| {
10 let data_max = rows.iter().map(|r| r[i].len()).max().unwrap_or(0);
11 data_max.max(headers[i].len())
12 })
13 .collect();
14
15 let mut line = String::new();
17 for (i, hdr) in headers.iter().enumerate() {
18 if i > 0 {
19 line.push_str(" ");
20 }
21 if i < ncols - 1 {
22 line.push_str(&format!("{:<width$}", hdr, width = widths[i]));
23 } else {
24 line.push_str(hdr);
25 }
26 }
27 println!("{line}");
28
29 for row in rows {
31 let mut line = String::new();
32 for (i, cell) in row.iter().enumerate() {
33 if i > 0 {
34 line.push_str(" ");
35 }
36 if i < ncols - 1 {
37 line.push_str(&format!("{:<width$}", cell, width = widths[i]));
38 } else {
39 line.push_str(cell);
40 }
41 }
42 println!("{line}");
43 }
44}