From b499e7c68283e67bb89c6a9e63b171c83387732e Mon Sep 17 00:00:00 2001
From: Maxim Zhiburt <zhiburt@gmail.com>
Date: Sun, 25 Dec 2022 03:27:34 +0300
Subject: [PATCH] Fix some issues with table wrapping of lists (#7598)

close #7591

I tend to think it must be addressed.
But I'd verify it @rgwood.

PS: I've noticed how `table -e` and `table` with the same width wraps a
bit differently sometimes. (I guess it also must be addressed......)

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
---
 crates/nu-command/src/viewers/table.rs | 26 +++++++++++++++++++-------
 crates/nu-table/src/table.rs           |  2 +-
 crates/nu-table/src/util.rs            |  8 +++++---
 3 files changed, 25 insertions(+), 11 deletions(-)

diff --git a/crates/nu-command/src/viewers/table.rs b/crates/nu-command/src/viewers/table.rs
index 3843619fe8..6fddbd4010 100644
--- a/crates/nu-command/src/viewers/table.rs
+++ b/crates/nu-command/src/viewers/table.rs
@@ -550,13 +550,13 @@ fn build_expanded_table(
                                 style_computer,
                             );
 
-                            nu_table::wrap_string(&failed_value.0, remaining_width)
+                            wrap_text(failed_value.0, remaining_width)
                         }
                     }
                 }
                 val => {
                     let text = value_to_styled_string(&val, config, style_computer).0;
-                    nu_table::wrap_string(&text, remaining_width)
+                    wrap_text(text, remaining_width)
                 }
             }
         };
@@ -1321,10 +1321,22 @@ fn error_sign(style_computer: &StyleComputer) -> (String, TextStyle) {
 }
 
 fn wrap_nu_text(mut text: NuText, width: usize) -> NuText {
+    if string_width(&text.0) <= width {
+        return text;
+    }
+
     text.0 = nu_table::wrap_string(&text.0, width);
     text
 }
 
+fn wrap_text(text: String, width: usize) -> String {
+    if string_width(&text) <= width {
+        return text;
+    }
+
+    nu_table::wrap_string(&text, width)
+}
+
 #[allow(clippy::too_many_arguments)]
 fn convert_to_table2_entry(
     item: &Value,
@@ -1432,7 +1444,10 @@ fn convert_to_table2_entry(
                 }
             }
         }
-        _ => wrap_nu_text(value_to_styled_string(item, config, style_computer), width), // unknown type.
+        _ => {
+            let text = value_to_styled_string(item, config, style_computer);
+            wrap_nu_text(text, width)
+        } // unknown type.
     }
 }
 
@@ -1551,7 +1566,6 @@ impl PagingTableCreator {
         let config = self.engine_state.get_config();
         let style_computer = StyleComputer::from_config(&self.engine_state, &self.stack);
         let term_width = get_width_param(self.width_param);
-        let theme = load_theme_from_config(config);
 
         let table = convert_to_table2(
             self.row_offset,
@@ -1566,13 +1580,11 @@ impl PagingTableCreator {
             term_width,
         )?;
 
-        let (mut table, with_header, with_index) = match table {
+        let (table, with_header, with_index) = match table {
             Some(table) => table,
             None => return Ok(None),
         };
 
-        table.truncate(term_width, &theme);
-
         let table_config = create_table_config(
             config,
             &style_computer,
diff --git a/crates/nu-table/src/table.rs b/crates/nu-table/src/table.rs
index 0e2ebf50fc..84c5ab75f0 100644
--- a/crates/nu-table/src/table.rs
+++ b/crates/nu-table/src/table.rs
@@ -450,7 +450,7 @@ fn truncate_columns_by_content(data: &mut Data, theme: &TableTheme, termwidth: u
     }
 
     // we don't need any truncation then (is it possible?)
-    if truncate_pos + 1 == data.count_columns() {
+    if truncate_pos == data.count_columns() {
         return false;
     }
 
diff --git a/crates/nu-table/src/util.rs b/crates/nu-table/src/util.rs
index 58e143dbf4..2439e52c55 100644
--- a/crates/nu-table/src/util.rs
+++ b/crates/nu-table/src/util.rs
@@ -1,4 +1,4 @@
-use tabled::{builder::Builder, Padding, Style, Width};
+use tabled::{builder::Builder, object::Cell, Modify, Padding, Style, Width};
 
 pub fn string_width(text: &str) -> usize {
     tabled::papergrid::util::string_width_multiline_tab(text, 4)
@@ -16,9 +16,11 @@ pub fn wrap_string(text: &str, width: usize) -> String {
 
     Builder::from_iter([[text]])
         .build()
-        .with(Padding::zero())
         .with(Style::empty())
-        .with(Width::wrap(width))
+        .with(Padding::zero())
+        .with(Modify::new(Cell(0, 0)).with(Width::wrap(width)))
+        .to_string()
+        .trim_end()
         .to_string()
 }