Repository: zhiburt/tabled
Pull request #258 (2022-12-26 12:09:57)

Fix wrap keeping words not colored version issue with space absence


Patch:
diff --git a/Cargo.toml b/Cargo.toml
index 495c488..a375bf7 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -169,7 +169,7 @@ macros = []
 [dependencies]
 papergrid = "0.7.1"
 unicode-width = "0.1.9"
-tabled_derive = { version = "0.5.0", optional = true }
+tabled_derive = { path = "./tabled_derive", optional = true }
 ansi-str = { version = "0.5.0", optional = true }
 
 [dev-dependencies]
diff --git a/benches/benches/strings.rs b/benches/benches/strings.rs
index 6d2df3d..2db6986 100644
--- a/benches/benches/strings.rs
+++ b/benches/benches/strings.rs
@@ -58,7 +58,7 @@ pub fn wrap(c: &mut Criterion) {
                 let text = black_box(build_string(size));
                 b.iter(|| {
                     black_box(
-                        tabled::Table::new(&[&text])
+                        tabled::Table::new([&text])
                             .with(tabled::Width::wrap(1))
                             .to_string(),
                     )
@@ -73,7 +73,7 @@ pub fn wrap(c: &mut Criterion) {
                 let text = black_box(build_string(size));
                 b.iter(|| {
                     black_box(
-                        tabled_master::Table::new(&[&text])
+                        tabled_master::Table::new([&text])
                             .with(tabled_master::Width::wrap(1))
                             .to_string(),
                     )
diff --git a/examples/border_text.rs b/examples/border_text.rs
index 64ed060..665a171 100644
--- a/examples/border_text.rs
+++ b/examples/border_text.rs
@@ -8,7 +8,7 @@ use tabled::{
 fn main() {
     let data = [[5, 6, 7, 8, 9], [10, 11, 12, 13, 14]];
 
-    let table = Table::new(&data)
+    let table = Table::new(data)
         .with(
             Style::modern()
                 .off_horizontal()
diff --git a/examples/expanded_display.rs b/examples/expanded_display.rs
index 4144153..4d15700 100644
--- a/examples/expanded_display.rs
+++ b/examples/expanded_display.rs
@@ -23,13 +23,13 @@ impl Distribution {
 }
 
 fn main() {
-    let data = [
+    let data = &[
         Distribution::new("Manjaro", "Arch", true, true),
         Distribution::new("Arch", "", true, true),
         Distribution::new("Debian", "", true, true),
     ];
 
-    let table = ExpandedDisplay::new(&data);
+    let table = ExpandedDisplay::new(data);
 
     println!("{}", table);
 }
diff --git a/examples/matrix.rs b/examples/matrix.rs
index e324701..6c94ffb 100644
--- a/examples/matrix.rs
+++ b/examples/matrix.rs
@@ -18,7 +18,7 @@ fn matrix<const N: usize>() -> [[usize; N]; N] {
 
 fn main() {
     let data = matrix::<10>();
-    let table = Table::new(&data).with(Style::modern()).to_string();
+    let table = Table::new(data).with(Style::modern()).to_string();
 
     println!("{}", table);
 }
diff --git a/examples/padding_color.rs b/examples/padding_color.rs
index 8e31048..8147ad2 100644
--- a/examples/padding_color.rs
+++ b/examples/padding_color.rs
@@ -153,7 +153,7 @@ where
         let (count_rows, count_cols) = table.shape();
         for (row, col) in entity.iter(count_rows, count_cols) {
             let column_width = widths[col];
-            let width = table.get_records().get_width((row, col), &ctrl);
+            let width = table.get_records().get_width((row, col), ctrl.clone());
 
             if width < column_width {
                 let available_width = column_width - width;
diff --git a/papergrid/examples/hello_world.rs b/papergrid/examples/hello_world.rs
index 25efbf5..bcede69 100644
--- a/papergrid/examples/hello_world.rs
+++ b/papergrid/examples/hello_world.rs
@@ -47,7 +47,7 @@ fn main() {
         ["", "Just like this", "", ""],
     ];
 
-    let records = VecRecords::new(&data, (2, 4), CfgWidthFunction::from_cfg(&cfg));
+    let records = VecRecords::new(data, (2, 4), CfgWidthFunction::from_cfg(&cfg));
 
     let mut width = WidthEstimator::default();
     width.estimate(&records, &cfg);
diff --git a/src/features/width/wrap.rs b/src/features/width/wrap.rs
index d41a396..77abacc 100644
--- a/src/features/width/wrap.rs
+++ b/src/features/width/wrap.rs
@@ -400,6 +400,7 @@ fn split_keeping_words(s: &str, width: usize, sep: &str) -> String {
 
                 word_part = &rhs[split_char..];
                 line_width += unicode_width::UnicodeWidthStr::width(lhs) + unknowns;
+                is_first_word = false;
 
                 line.push_str(lhs);
                 line.extend(std::iter::repeat(REPLACEMENT).take(unknowns));
diff --git a/tabled_derive/src/lib.rs b/tabled_derive/src/lib.rs
index cced614..210482c 100644
--- a/tabled_derive/src/lib.rs
+++ b/tabled_derive/src/lib.rs
@@ -165,9 +165,11 @@ fn info_from_fields(
     let mut values = Vec::new();
     let mut reorder = HashMap::new();
 
+    let mut skipped = 0;
     for result in fields {
         let (i, field, attributes) = result?;
         if attributes.is_ignored() {
+            skipped += 1;
             continue;
         }
 
@@ -179,16 +181,14 @@ fn info_from_fields(
                 )));
             }
 
-            reorder.insert(order, i);
+            reorder.insert(order, i - skipped);
         }
 
         let header = field_headers(field, i, &attributes, header_prefix);
-
         headers.push(header);
 
         let field_name = field_name(i, field);
         let value = get_field_fields(&field_name, &attributes);
-
         values.push(value);
     }
 


Test patch:
diff --git a/papergrid/tests/util.rs b/papergrid/tests/util.rs
index eaece20..1019845 100644
--- a/papergrid/tests/util.rs
+++ b/papergrid/tests/util.rs
@@ -81,9 +81,9 @@ fn build_grid(
     let records = VecRecords::new(records, (rows, cols), CfgWidthFunction::from_cfg(cfg));
     let records = Box::leak(Box::new(records));
 
-    let width = Box::leak(Box::new(WidthEstimator::default()));
+    let width: &mut WidthEstimator = Box::leak(Box::default());
     width.estimate(&*records, cfg);
-    let height = Box::leak(Box::new(HeightEstimator::default()));
+    let height: &mut HeightEstimator = Box::leak(Box::default());
     height.estimate(&*records, cfg);
 
     Grid::new(&*records, cfg, &*width, &*height)
diff --git a/tests/derive_test.rs b/tests/derive_test.rs
index 664fd93..0c9450c 100644
--- a/tests/derive_test.rs
+++ b/tests/derive_test.rs
@@ -834,3 +834,27 @@ fn rename_all_gets_last_value() {
     assert_eq!(Struct1::headers(), vec!["Field"],);
     assert_eq!(st.fields(), vec!["789"]);
 }
+
+#[test]
+fn test_order_skip_usage() {
+    #[derive(Tabled, Default)]
+    pub struct Example {
+        #[tabled(skip)]
+        id: usize,
+        name: String,
+        #[tabled(order = 0)]
+        details: String,
+    }
+
+    #[derive(Tabled, Default)]
+    pub struct Example2 {
+        #[tabled(skip)]
+        id: usize,
+        name: String,
+        #[tabled(order = 1)]
+        details: String,
+    }
+
+    assert_eq!(Example::headers(), vec!["details", "name"],);
+    assert_eq!(Example::default().fields(), vec!["", ""]);
+}
diff --git a/tests/panel_test.rs b/tests/panel_test.rs
index b4ddee4..c784d5d 100644
--- a/tests/panel_test.rs
+++ b/tests/panel_test.rs
@@ -126,7 +126,7 @@ test_table!(
 
 test_table!(
     panel_style_uses_most_left_and_right_cell_styles,
-    new_table(&[(0, 1)]).with(Panel::horizontal(0).text("Numbers")).with(Style::modern()),
+    new_table([(0, 1)]).with(Panel::horizontal(0).text("Numbers")).with(Style::modern()),
     "┌─────┬─────┐"
     "│  Numbers  │"
     "├─────┼─────┤"
@@ -138,7 +138,7 @@ test_table!(
 
 test_table!(
     panel_style_change,
-    new_table(&[(0, 1)])
+    new_table([(0, 1)])
         .with(Panel::horizontal(0).text("Numbers"))
         .with(Style::modern().top_intersection('─').horizontals([HorizontalLine::new(1, Style::modern().get_horizontal()).intersection(Some('┬'))]))
         .with(Modify::new(Cell(0, 0)).with(Alignment::center())),
@@ -153,7 +153,7 @@ test_table!(
 
 test_table!(
     panel_style_uses_most_left_and_right_cell_styles_correct,
-    new_table(&[(0, 1)])
+    new_table([(0, 1)])
         .with(Panel::horizontal(0).text("Numbers"))
         .with(Style::modern())
         .with(Style::correct_spans()),
@@ -168,7 +168,7 @@ test_table!(
 
 test_table!(
     panel_style_change_correct,
-    new_table(&[(0, 1)])
+    new_table([(0, 1)])
         .with(Panel::horizontal(0).text("Numbers"))
         .with(Style::modern().top_intersection('─').horizontals([HorizontalLine::new(1, Style::modern().get_horizontal()).intersection(Some('┬'))]))
         .with(Style::correct_spans())
@@ -184,7 +184,7 @@ test_table!(
 
 test_table!(
     panel_in_single_column,
-    new_table(&[(0)]).with(Panel::horizontal(0).text("Numbers")).with(Style::modern()),
+    new_table([(0)]).with(Panel::horizontal(0).text("Numbers")).with(Style::modern()),
     "┌─────────┐"
     "│ Numbers │"
     "├─────────┤"
diff --git a/tests/render_settings.rs b/tests/render_settings.rs
index e7b3606..bccc764 100644
--- a/tests/render_settings.rs
+++ b/tests/render_settings.rs
@@ -164,7 +164,7 @@ test_table!(
 
 test_table!(
     test_top_alignment_and_vertical_trim_1,
-    new_table(&["   \n\n\n    Hello World"])
+    new_table(["   \n\n\n    Hello World"])
         .with(Style::modern())
         .with(Modify::new(Segment::all()).with(Alignment::top()).with(TrimStrategy::Vertical)),
     "┌─────────────────┐"
diff --git a/tests/table_test.rs b/tests/table_test.rs
index 77aaa18..a6aee6c 100644
--- a/tests/table_test.rs
+++ b/tests/table_test.rs
@@ -234,7 +234,7 @@ mod default_types {
 
 test_table!(
     table_tuple,
-    Table::new(&[("we are in", 2020)]),
+    Table::new([("we are in", 2020)]),
     "+-----------+------+"
     "| &str      | i32  |"
     "+-----------+------+"
@@ -244,7 +244,7 @@ test_table!(
 
 test_table!(
     table_single_tuple,
-    Table::new(&[(2020,)]),
+    Table::new([(2020,)]),
     "+------+"
     "| i32  |"
     "+------+"
@@ -254,7 +254,7 @@ test_table!(
 
 test_table!(
     table_tuple_vec,
-    Table::new(&[(0, "Monday"), (1, "Thursday")]),
+    Table::new([(0, "Monday"), (1, "Thursday")]),
     "+-----+----------+"
     "| i32 | &str     |"
     "+-----+----------+"
@@ -276,7 +276,7 @@ test_table!(
 
 test_table!(
     multiline_table_test_0,
-    Table::new(&[["This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\r\n\r\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\r\n"]])
+    Table::new([["This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\r\n\r\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\r\n"]])
         .with(tabled::Style::modern()),
     r#"┌──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐"#
     r#"│ 0                                                                                                                                                                                                                                                                    │"#
@@ -290,7 +290,7 @@ test_table!(
 
 test_table!(
     multiline_table_test_1,
-    Table::new(&[["This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\r\n\r\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\r\n"]])
+    Table::new([["This is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\r\n\r\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\r\n"]])
         .with(tabled::Style::modern())
         .with(Width::wrap(100)),
     "┌──────────────────────────────────────────────────────────────────────────────────────────────────┐"
@@ -722,11 +722,11 @@ mod derived {
 #[cfg(feature = "color")]
 #[test]
 fn multiline_table_test2() {
-    let data = [
+    let data = &[
         ["\u{1b}[37mThis is the 0.19 release of Nushell. If you'd like to read more about it, please check out: https://www.nushell.sh/blog/2020/09/01/nushell_0_19.html\n\nFor convenience, we are providing full builds for Windows, Linux, and macOS. These are the \"all extra features\" builds, so be sure you have the requirements to enable all capabilities: https://github.com/nushell/book/blob/master/en/installation.md#dependencies\n\u{1b}[0m"],
     ];
 
-    let mut table = Table::new(&data);
+    let mut table = Table::new(data);
     table.with(tabled::Style::modern());
 
     assert_eq!(
diff --git a/tests/width_test.rs b/tests/width_test.rs
index 0ef3165..636042c 100644
--- a/tests/width_test.rs
+++ b/tests/width_test.rs
@@ -9,7 +9,7 @@ use tabled::{
     papergrid::util::string_width_multiline,
     peaker::{PriorityMax, PriorityMin},
     width::{Justify, MinWidth, SuffixLimit, Width},
-    Alignment, Margin, Modify, Padding, Panel, Span, Style,
+    Alignment, Margin, Modify, Padding, Panel, Span, Style, Table,
 };
 
 use crate::util::{create_table, init_table, is_lines_equal, new_table, static_table};
@@ -401,7 +401,7 @@ fn max_width_wrapped_keep_words_long_word_color() {
 fn max_width_keep_words_1() {
     use tabled::style::HorizontalLine;
 
-    let table = new_table(&["asdf"])
+    let table = new_table(["asdf"])
         .with(Width::wrap(7).keep_words())
         .to_string();
 
@@ -418,7 +418,7 @@ fn max_width_keep_words_1() {
         )
     );
 
-    let table = new_table(&["qweqw eqwe"])
+    let table = new_table(["qweqw eqwe"])
         .with(Width::wrap(8).keep_words())
         .to_string();
 
@@ -2221,7 +2221,7 @@ fn min_width_priority_min() {
 #[test]
 fn max_width_tab_0() {
     let table =
-        new_table(&["\t\tTigre Ecuador\tOMYA Andina\t3824909999\tCalcium carbonate\tColombia\t"])
+        new_table(["\t\tTigre Ecuador\tOMYA Andina\t3824909999\tCalcium carbonate\tColombia\t"])
             .with(Style::markdown())
             .with(Width::wrap(60))
             .to_string();
@@ -2282,6 +2282,43 @@ fn min_width_is_used_after_margin() {
     );
 }
 
+#[test]
+fn wrap_keeping_words_0() {
+    let data = vec![["Hello world"]];
+    let table = Table::new(data)
+        .with(Width::wrap(8).keep_words())
+        .to_string();
+
+    println!("{}", table);
+
+    assert_eq!(papergrid::util::string_width_multiline(&table), 8);
+
+    #[cfg(feature = "color")]
+    let expected = static_table!(
+        "+------+"
+        "| 0    |"
+        "+------+"
+        "| Hell |"
+        "| o    |"
+        "| worl |"
+        "| d    |"
+        "+------+"
+    );
+
+    #[cfg(not(feature = "color"))]
+    let expected = static_table!(
+        "+------+"
+        "| 0    |"
+        "+------+"
+        "| Hell |"
+        "| o wo |"
+        "| rld  |"
+        "+------+"
+    );
+
+    assert_eq!(table, expected,);
+}
+
 #[cfg(feature = "derive")]
 mod derived {
     use super::*;