From 23a7db0f19f82168e8a9324f7e3e88c9701046cc Mon Sep 17 00:00:00 2001 From: Ben Morrison Date: Mon, 20 Jan 2020 19:54:39 -0500 Subject: trimming whitespace both when posting new item and when displaying items. prevents posts from messing up the display of other posts with extraneous whitespace. --- src/posts.rs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) (limited to 'src') 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); } }); -- cgit 1.4.1-2-gfad0