Repository: Nukesor/comfy-table (rust)

Issue:
Trimming trailing whitespace in borderless tables
I am using comfy-table to build tables without borders. It all works great until I start to unnittest my tables. In my unit tests I am comparing the tables' String representations, with the expected result stored in a file (which I read with `include_str!`). Because my editor is configured to trim trailing whitespace on save, the expected result file does not have them, but the generated table does. So in order to do the comparison I first need to trim all trailing whitespace from the generated table, which I find a little cumbersome and annoying.

[This](https://github.com/Nukesor/comfy-table/blob/33739f9d7fe238c116801d86ffbaafe18b76dde4/tests/presets_test.rs#L20) test illustrate what I mean. In my opinion all whitespace between `3` and `\n` should be gone.

Now, I do realize that this is a "problem" of my editor, and others would likely never encounter it. However I would still like to suggest trimming trailing whitespace from borderless tables, just because trailing whitespace is usually useless.

I would be more than happy to make a PR, but wanted to first double-check if such a change would be OK.



Pull request:
Add trim_fmt function to table
Resolves #20

Patch:
diff --git a/src/table.rs b/src/table.rs
index 1993e0c..9a0f042 100644
--- a/src/table.rs
+++ b/src/table.rs
@@ -69,6 +69,20 @@ impl Table {
         table
     }
 
+    /// This is an alternative `fmt` function, which simply removes any trailing whitespaces.
+    /// Trailing whitespaces often occur, when using tables without a right border.
+    pub fn trim_fmt(&self) -> String {
+        let display_info = arrange_content(self);
+        let content = format_content(&self, &display_info);
+        let mut lines = draw_borders(&self, content, &display_info);
+        lines = lines
+            .iter()
+            .map(|line| line.trim_end().to_string())
+            .collect();
+
+        lines.join("\n")
+    }
+
     /// Set the header row of the table. This is usually the title of each column.\
     /// There'll be no header unless you explicitly set it with this function.
     ///


Test patch:
diff --git a/tests/presets_test.rs b/tests/presets_test.rs
index 87fd25c..6af15b7 100644
--- a/tests/presets_test.rs
+++ b/tests/presets_test.rs
@@ -16,15 +16,14 @@ fn get_preset_table() -> Table {
 fn ascii_no_borders() {
     let mut table = get_preset_table();
     table.load_preset(ASCII_NO_BORDERS);
-    let expected = concat!(
-        " Header1 | Header2 | Header3   \n",
-        "===============================\n",
-        " One One | One Two | One Three \n",
-        "---------+---------+-----------\n",
-        " One One | One Two | One Three ",
-    );
+    let expected = "
+ Header1 | Header2 | Header3
+===============================
+ One One | One Two | One Three
+---------+---------+-----------
+ One One | One Two | One Three";
     println!("{}", table.to_string());
-    assert_eq!(&table.to_string(), expected);
+    assert_eq!("\n".to_string() + &table.trim_fmt(), expected);
 }
 
 #[test]
@@ -46,17 +45,16 @@ fn ascii_borders_only() {
 fn ascii_horizontal_borders_only() {
     let mut table = get_preset_table();
     table.load_preset(ASCII_HORIZONTAL_BORDERS_ONLY);
-    let expected = concat!(
-        "-------------------------------\n",
-        " Header1   Header2   Header3   \n",
-        "===============================\n",
-        " One One   One Two   One Three \n",
-        "-------------------------------\n",
-        " One One   One Two   One Three \n",
-        "-------------------------------",
-    );
+    let expected = "
+-------------------------------
+ Header1   Header2   Header3
+===============================
+ One One   One Two   One Three
+-------------------------------
+ One One   One Two   One Three
+-------------------------------";
     println!("{}", table.to_string());
-    assert_eq!(&table.to_string(), expected);
+    assert_eq!("\n".to_string() + &table.trim_fmt(), expected);
 }
 
 #[test]
@@ -94,15 +92,14 @@ fn utf8_full() {
 fn utf8_no_borders() {
     let mut table = get_preset_table();
     table.load_preset(UTF8_NO_BORDERS);
-    let expected = concat!(
-        " Header1 ┆ Header2 ┆ Header3   \n",
-        "═════════╪═════════╪═══════════\n",
-        " One One ┆ One Two ┆ One Three \n",
-        "╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌\n",
-        " One One ┆ One Two ┆ One Three ",
-    );
+    let expected = "
+ Header1 ┆ Header2 ┆ Header3
+═════════╪═════════╪═══════════
+ One One ┆ One Two ┆ One Three
+╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌┼╌╌╌╌╌╌╌╌╌╌╌
+ One One ┆ One Two ┆ One Three";
     println!("{}", table.to_string());
-    assert_eq!(&table.to_string(), expected);
+    assert_eq!("\n".to_string() + &table.trim_fmt(), expected);
 }
 
 #[test]
@@ -125,15 +122,14 @@ fn utf8_borders_only() {
 fn utf8_horizontal_borders_only() {
     let mut table = get_preset_table();
     table.load_preset(UTF8_HORIZONTAL_BORDERS_ONLY);
-    let expected = concat!(
-        "───────────────────────────────\n",
-        " Header1   Header2   Header3   \n",
-        "═══════════════════════════════\n",
-        " One One   One Two   One Three \n",
-        "───────────────────────────────\n",
-        " One One   One Two   One Three \n",
-        "───────────────────────────────",
-    );
+    let expected = "
+───────────────────────────────
+ Header1   Header2   Header3
+═══════════════════════════════
+ One One   One Two   One Three
+───────────────────────────────
+ One One   One Two   One Three
+───────────────────────────────";
     println!("{}", table.to_string());
-    assert_eq!(&table.to_string(), expected);
+    assert_eq!("\n".to_string() + &table.trim_fmt(), expected);
 }