From 66c2a36123451d58a5f6020f7b68021d6f6763d3 Mon Sep 17 00:00:00 2001 From: Maxim Zhiburt Date: Fri, 28 Oct 2022 15:00:10 +0300 Subject: [PATCH] table: Show truncated record differently (#6884) Signed-off-by: Maxim Zhiburt Signed-off-by: Maxim Zhiburt --- crates/nu-command/src/viewers/table.rs | 53 +++++++++++++++++++++----- crates/nu-table/src/table.rs | 8 ++++ 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs index 9afd9127e5..2ce9cc362a 100644 --- a/crates/nu-command/src/viewers/table.rs +++ b/crates/nu-command/src/viewers/table.rs @@ -443,9 +443,13 @@ fn build_expanded_table( let value = if is_limited { value_to_styled_string(&value, 0, config, &color_hm).0 } else { - let vals = match value { + let mut is_record = false; + let mut vals = match value { Value::List { vals, .. } => vals, - value => vec![value], + value => { + is_record = true; + vec![value] + } }; let deep = expand_limit.map(|i| i - 1); @@ -465,15 +469,44 @@ fn build_expanded_table( match table { Some(mut table) => { // controll width via removing table columns. - let theme = load_theme_from_config(config); - table.truncate(remaining_width, &theme); + let count_cols = table.size().1; + let is_empty = table.truncate(remaining_width, &theme); + let was_left_only_index = + table.is_with_index() && table.size().1 == 2 && count_cols != 2; + let was_truncated = is_empty || was_left_only_index; - let result = - table.draw_table(config, &color_hm, alignments, &theme, remaining_width); - is_expanded = true; - match result { - Some(result) => result, - None => return Ok(None), + if is_record && vals.len() == 1 && was_truncated { + match vals.remove(0) { + Value::Record { cols, vals, .. } => { + let t = build_general_table2( + cols, + vals, + ctrlc.clone(), + config, + remaining_width, + )?; + + match t { + Some(val) => val, + None => return Ok(None), + } + } + _ => unreachable!(), + } + } else { + let theme = load_theme_from_config(config); + let result = table.draw_table( + config, + &color_hm, + alignments, + &theme, + remaining_width, + ); + is_expanded = true; + match result { + Some(result) => result, + None => return Ok(None), + } } } None => { diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs index 5da62419ac..cee8de6e04 100644 --- a/crates/nu-table/src/table.rs +++ b/crates/nu-table/src/table.rs @@ -71,6 +71,14 @@ impl Table { self.is_empty } + pub fn size(&self) -> (usize, usize) { + (self.data.count_rows(), self.data.count_columns()) + } + + pub fn is_with_index(&self) -> bool { + self.with_index + } + pub fn truncate(&mut self, width: usize, theme: &TableTheme) -> bool { let mut truncated = false; while self.data.count_rows() > 0 && self.data.count_columns() > 0 {