1use terminal_size::{Width, terminal_size};
2
3
4pub fn render_table(headers: &[&str], rows: &[Vec<&str>]) {
5 let term_width = terminal_size().map(|(Width(w), _)| w as usize).unwrap_or(80);
6 let col_count = headers.len().max(1);
7 let padding = 3; let col_width = ((term_width - (col_count - 1) * padding).saturating_sub(1)) / col_count;
9
10 let header_line: Vec<String> = headers
12 .iter()
13 .map(|h| format!("{:^width$}", h, width = col_width))
14 .collect();
15 println!("{}", header_line.join(" | "));
16
17 println!("{}", "-".repeat(term_width.min(col_count * (col_width + padding))));
19
20 for row in rows {
22 let row_line: Vec<String> = row
23 .iter()
24 .map(|cell| {
25 if cell.len() > col_width {
26 format!("{:.width$}", cell, width = col_width - 1).to_owned() + "…"
27 } else {
28 format!("{:<width$}", cell, width = col_width)
29 }
30 })
31 .collect();
32 println!("{}", row_line.join(" | "));
33 }
34}