(&self, truncate: bool)
| 54 | /// * `truncate` - Whether to truncate the table if it is too wide for the console |
| 55 | #[allow(clippy::format_push_string)] |
| 56 | pub fn print(&self, truncate: bool) { |
| 57 | let (width, _) = size().unwrap_or((80, 25)); |
| 58 | // make header bright green |
| 59 | println!("\x1b[1;32m"); |
| 60 | let mut header_row = String::with_capacity(width as usize); |
| 61 | let last_column = self.header.len() - 1; |
| 62 | for (i, column) in self.header.iter().enumerate() { |
| 63 | header_row.push_str(&format!("{:<width$}", column, width = self.column_widths[i])); |
| 64 | if i != last_column { |
| 65 | header_row.push_str(" "); |
| 66 | } |
| 67 | } |
| 68 | // if header row is too wide, truncate |
| 69 | if truncate && header_row.len() > width as usize { |
| 70 | header_row.truncate(width as usize); |
| 71 | } |
| 72 | |
| 73 | println!("{header_row}"); |
| 74 | println!("{}\x1b[0m", "-".repeat(header_row.len())); |
| 75 | |
| 76 | // TODO: make this smarter by splitting long text to multiple lines |
| 77 | for row in &self.rows { |
| 78 | let mut row_str = String::with_capacity(header_row.len()); |
| 79 | for (i, column) in row.iter().enumerate() { |
| 80 | row_str.push_str(&format!("{:<width$}", column, width = self.column_widths[i])); |
| 81 | if i != last_column { |
| 82 | row_str.push_str(" "); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | // if row is too wide and last character is not a space, truncate and add ellipsis unicode character |
| 87 | if truncate && row_str.len() > width as usize { |
| 88 | row_str.truncate(width as usize); |
| 89 | if !row_str.ends_with(' ') { |
| 90 | row_str.pop(); |
| 91 | row_str.push('…'); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | println!("{row_str}"); |
| 96 | } |
| 97 | } |
| 98 | } |
no test coverage detected