summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-01-20 19:54:39 -0500
committerBen Morrison <ben@gbmor.dev>2020-01-20 19:54:39 -0500
commit23a7db0f19f82168e8a9324f7e3e88c9701046cc (patch)
tree2d2bd247bf45f02322e4b46f28b77058383c8566
parentd88b80df01fb5630b4a7e5370135ec15c2d22646 (diff)
downloadclinte-23a7db0f19f82168e8a9324f7e3e88c9701046cc.tar.gz
trimming whitespace
both when posting new item and when displaying items.
prevents posts from messing up the display of other
posts with extraneous whitespace.
-rw-r--r--src/posts.rs24
1 files changed, 15 insertions, 9 deletions
diff --git a/src/posts.rs b/src/posts.rs
index efcb574..30738f0 100644
--- a/src/posts.rs
+++ b/src/posts.rs
@@ -48,14 +48,16 @@ pub fn create(db: &db::Conn) -> error::Result<()> {
     };
 
     println!();
-    let body = str_to_utf8(&ed::call());
-    let body = if body.len() > 500 {
-        &body[..500]
+    let body_raw = str_to_utf8(&ed::call());
+    let body = if body_raw.len() > 500 {
+        &body_raw[..500]
     } else {
-        &body
+        &body_raw
     };
 
-    exec_new(&mut stmt, title, body)?;
+    let trimmed_body = body.trim();
+
+    exec_new(&mut stmt, title, trimmed_body)?;
 
     println!();
     Ok(())
@@ -80,10 +82,14 @@ pub fn display(db: &db::Conn) -> error::Result<()> {
     let mut postvec = Vec::new();
     out.for_each(|row| {
         if let Ok(post) = row {
-            postvec.push(format!(
-                "{}. {} -> by {}\n{}\n",
-                post.id, post.title, post.author, post.body
-            ));
+            let newpost = format!(
+                "{}. {} -> by {}\n{}\n\n",
+                post.id,
+                post.title.trim(),
+                post.author,
+                post.body.trim()
+            );
+            postvec.push(newpost);
         }
     });