Repository: zhiburt/tabled
Pull request #98 (2022-03-23 23:20:27)

papergrid/ Remove a trim on alignement.
It breaks `MinWidth` logic if used with `Center`/`Right` `Alignement`



Patch:
diff --git a/examples/terminal_table/Cargo.toml b/examples/terminal_table/Cargo.toml
index 13f8bb1..6ee1868 100644
--- a/examples/terminal_table/Cargo.toml
+++ b/examples/terminal_table/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "terminal_table"
-version = "0.1.0"
+version = "0.0.0"
 edition = "2021"
 publish = false
 
diff --git a/examples/terminal_table/src/main.rs b/examples/terminal_table/src/main.rs
index cb1a581..dd13cd7 100644
--- a/examples/terminal_table/src/main.rs
+++ b/examples/terminal_table/src/main.rs
@@ -40,7 +40,7 @@ fn main() {
     let table = DATA
         .table()
         .with(Style::extended())
-        .with(Modify::new(Full).with(Alignment::center_horizontal()))
+        .with(Modify::new(Full).with(Alignment::left()))
         .with(MaxWidth::wrapping(width as usize).keep_words())
         .with(MinWidth::new(width as usize));
 
diff --git a/papergrid/src/lib.rs b/papergrid/src/lib.rs
index 7084dab..627ca3f 100644
--- a/papergrid/src/lib.rs
+++ b/papergrid/src/lib.rs
@@ -806,10 +806,6 @@ pub enum AlignmentHorizontal {
 
 impl AlignmentHorizontal {
     fn align(&self, f: &mut std::fmt::Formatter<'_>, text: &str, width: usize) -> fmt::Result {
-        // it's important step
-        // we are ignoring trailing spaces which allows us to do alignment with more space
-        // example: tests::grid_2x2_alignment_test
-        let text = text.trim();
         let text_width = string_width(text);
         let diff = width - text_width;
         match self {


Test patch:
diff --git a/papergrid/tests/styling.rs b/papergrid/tests/styling.rs
index ac73671..2009b74 100644
--- a/papergrid/tests/styling.rs
+++ b/papergrid/tests/styling.rs
@@ -112,7 +112,6 @@ fn grid_2x2_change_cell_border_test() {
 #[test]
 fn grid_2x2_alignment_test() {
     let mut grid = util::new_grid::<2, 2>();
-    grid.set(&Entity::Global, Settings::new().text("asd    "));
     grid.set(
         &Entity::Column(0),
         Settings::new().alignment(AlignmentHorizontal::Left),
@@ -121,16 +120,29 @@ fn grid_2x2_alignment_test() {
         &Entity::Column(1),
         Settings::new().alignment(AlignmentHorizontal::Right),
     );
-    let str = grid.to_string();
+
+    grid.set(&Entity::Cell(0, 0), Settings::new().text("asd    "));
+    grid.set(&Entity::Cell(0, 1), Settings::new().text("asd    "));
 
     assert_eq!(
-        str,
+        grid.to_string(),
         "+-------+-------+\n\
-         |asd    |    asd|\n\
+         |asd    |asd    |\n\
          +-------+-------+\n\
-         |asd    |    asd|\n\
+         |1-0    |    1-1|\n\
          +-------+-------+\n"
-    )
+    );
+
+    grid.set(&Entity::Global, Settings::new().text("asd    "));
+
+    assert_eq!(
+        grid.to_string(),
+        "+-------+-------+\n\
+         |asd    |asd    |\n\
+         +-------+-------+\n\
+         |asd    |asd    |\n\
+         +-------+-------+\n"
+    );
 }
 
 #[test]
diff --git a/tests/table_test.rs b/tests/table_test.rs
index 9b76c0c..7252ca3 100644
--- a/tests/table_test.rs
+++ b/tests/table_test.rs
@@ -687,15 +687,15 @@ fn table_emojie_multiline() {
 
     // Note: it looks OK in a terminal
     let expected =
-        "+------------------------------------+-----------------+-------------------------------+--------+\n\
-         |                name                |     author      |             text              | rating |\n\
-         +------------------------------------+-----------------+-------------------------------+--------+\n\
-         | Rebase vs Merge commit in depth 👋 | Rose Kuphal DVM |          A multiline          |   43   |\n\
-         |                                    |                 |     text with 🤯 😳 🥵 🥶     |        |\n\
-         |                                    |                 | a bunch of emojies ☄️ 💥 🔥 🌪  |        |\n\
-         +------------------------------------+-----------------+-------------------------------+--------+\n\
-         |           Keep it simple           |     Unknown     |              🍳               |  100   |\n\
-         +------------------------------------+-----------------+-------------------------------+--------+\n";
+    "+------------------------------------+-----------------+-------------------------------+--------+\n\
+     |                name                |     author      |             text              | rating |\n\
+     +------------------------------------+-----------------+-------------------------------+--------+\n\
+     | Rebase vs Merge commit in depth 👋 | Rose Kuphal DVM |          A multiline          |   43   |\n\
+     |                                    |                 |     text with 🤯 😳 🥵 🥶     |        |\n\
+     |                                    |                 |  a bunch of emojies ☄\u{fe0f} 💥 🔥 🌪 |        |\n\
+     +------------------------------------+-----------------+-------------------------------+--------+\n\
+     |           Keep it simple           |     Unknown     |              🍳               |  100   |\n\
+     +------------------------------------+-----------------+-------------------------------+--------+\n";
 
     let table = Table::new(&languages).to_string();
 
diff --git a/tests/width_test.rs b/tests/width_test.rs
index 40c4143..8a160be 100644
--- a/tests/width_test.rs
+++ b/tests/width_test.rs
@@ -169,7 +169,7 @@ fn max_width_wrapped_keep_words() {
         "| &str              |\n",
         "|-------------------|\n",
         "| this is a long    |\n",
-        "| sentence          |\n",
+        "|  sentence         |\n",
     );
 
     assert_eq!(table, expected);
@@ -423,7 +423,7 @@ fn min_width() {
         .to_string();
 
     let expected = concat!(
-        "|      N       |   column 0   |   column 1   |   column 2   |\n",
+        "| N            | column 0     | column 1     | column 2     |\n",
         "|--------------+--------------+--------------+--------------|\n",
         "|      0       |     0-0      |     0-1      |     0-2      |\n",
         "|      1       |     1-0      |     1-1      |     1-2      |\n",
@@ -461,7 +461,7 @@ fn min_width_one_column() {
         .to_string();
 
     let expected = concat!(
-        "|   N   | column 0 | column 1 | column 2 |\n",
+        "| N     | column 0 | column 1 | column 2 |\n",
         "|-------+----------+----------+----------|\n",
         "|   0   |   0-0    |   0-1    |   0-2    |\n",
         "|   1   |   1-0    |   1-1    |   1-2    |\n",
@@ -494,7 +494,7 @@ fn min_with_max_width() {
         .to_string();
 
     let expected = concat!(
-        "|  N  | col | col | col |\n",
+        "| N   | col | col | col |\n",
         "|-----+-----+-----+-----|\n",
         "|  0  | 0-0 | 0-1 | 0-2 |\n",
         "|  1  | 1-0 | 1-1 | 1-2 |\n",
@@ -514,7 +514,7 @@ fn min_with_max_width_truncate_suffix() {
         .to_string();
 
     let expected = concat!(
-        "|  N  | col... | col... | col... |\n",
+        "| N   | col... | col... | col... |\n",
         "|-----+--------+--------+--------|\n",
         "|  0  |  0-0   |  0-1   |  0-2   |\n",
         "|  1  |  1-0   |  1-1   |  1-2   |\n",
@@ -536,11 +536,11 @@ fn min_width_color() {
     ];
 
     let expected = concat!(
-        "|   String   |\n",
+        "| String     |\n",
         "|------------|\n",
-        "|    \u{1b}[31masd\u{1b}[0m     |\n",
-        "|    \u{1b}[34mzxc\u{1b}[0m     |\n",
-        "|   \u{1b}[32m\u{1b}[40masdasd\u{1b}[0m\u{1b}[0m   |\n",
+        "| \u{1b}[31masd\u{1b}[0m        |\n",
+        "| \u{1b}[34mzxc\u{1b}[0m        |\n",
+        "| \u{1b}[32m\u{1b}[40masdasd\u{1b}[0m\u{1b}[0m     |\n",
     );
 
     let table = Table::new(data)
@@ -580,11 +580,11 @@ fn total_width_big() {
         .to_string();
 
     let expected = concat!(
-        "|      N       |      column 0       |      column 1      |      column 2      |\n",
+        "| N            | column 0            | column 1           | column 2           |\n",
         "|--------------+---------------------+--------------------+--------------------|\n",
-        "|      0       |         0-0         |        0-1         |        0-2         |\n",
-        "|      1       |         1-0         |        1-1         |        1-2         |\n",
-        "|      2       |         2-0         |        2-1         |        2-2         |\n",
+        "| 0            |   0-0               |   0-1              |   0-2              |\n",
+        "| 1            |   1-0               |   1-1              |   1-2              |\n",
+        "| 2            |   2-0               |   2-1              |   2-2              |\n",
     );
 
     assert_eq!(table, expected);
@@ -602,12 +602,12 @@ fn total_width_big_with_panel() {
         .to_string();
 
     let expected = concat!(
-        "|                                 Hello World                                  |\n",
+        "|                            Hello World                                       |\n",
         "|--------------+---------------------+--------------------+--------------------|\n",
-        "|      N       |      column 0       |      column 1      |      column 2      |\n",
-        "|      0       |         0-0         |        0-1         |        0-2         |\n",
-        "|      1       |         1-0         |        1-1         |        1-2         |\n",
-        "|      2       |         2-0         |        2-1         |        2-2         |\n",
+        "| N            | column 0            | column 1           | column 2           |\n",
+        "| 0            |   0-0               |   0-1              |   0-2              |\n",
+        "| 1            |   1-0               |   1-1              |   1-2              |\n",
+        "| 2            |   2-0               |   2-1              |   2-2              |\n",
     );
 
     assert_eq!(table, expected);
@@ -715,7 +715,7 @@ fn total_width_small_with_panel_using_wrapping() {
         "| N | co | co | co |\n",
         "|   | lu | lu | lu |\n",
         "|   | mn | mn | mn |\n",
-        "|   | 0  | 1  | 2  |\n",
+        "|   |  0 |  1 |  2 |\n",
         "| 0 | 0- | 0- | 0- |\n",
         "|   | 0  | 1  | 2  |\n",
         "| 1 | 1- | 1- | 1- |\n",