From e1cb02618453407eb7faa0b1a7f7ff08c03fc4b4 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 16 Feb 2021 07:15:16 +1300 Subject: [PATCH] Add back in column truncation (#3061) --- crates/nu-table/src/table.rs | 39 +++++++++++++++++++++++++++++++++--- 1 file changed, 36 insertions(+), 3 deletions(-) diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index 7a3798e5a2..e0077abb7d 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -963,6 +963,38 @@ fn get_max_column_widths(processed_table: &ProcessedTable) -> Vec { output } +pub fn maybe_truncate_columns(termwidth: usize, processed_table: &mut ProcessedTable) { + // Make sure we have enough space for the columns we have + let max_num_of_columns = termwidth / 10; + + // If we have too many columns, truncate the table + if max_num_of_columns < processed_table.headers.len() { + processed_table.headers.truncate(max_num_of_columns); + + for entry in processed_table.data.iter_mut() { + entry.truncate(max_num_of_columns); + } + + processed_table.headers.push(ProcessedCell { + contents: vec![vec![Subline { + subline: "...", + width: 3, + }]], + style: TextStyle::basic_center(), + }); + + for entry in processed_table.data.iter_mut() { + entry.push(ProcessedCell { + contents: vec![vec![Subline { + subline: "...", + width: 3, + }]], + style: TextStyle::basic_center(), + }); // ellipsis is centred + } + } +} + pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap) { // Remove the edges, if used let termwidth = if table.theme.print_left_border && table.theme.print_right_border { @@ -973,12 +1005,13 @@ pub fn draw_table(table: &Table, termwidth: usize, color_hm: &HashMap