summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2020-06-09 03:00:38 -0400
committerBen Morrison <ben@gbmor.dev>2020-06-09 03:00:38 -0400
commit9b749b946412b6f3d18c5d7997dc798f3947bb1e (patch)
tree798983aad1cddc135f5551c43691c9cb5bd3fce8
parent1e6f1de062771a59d45acd3f7c123c10285b2f3e (diff)
downloadclinte-9b749b946412b6f3d18c5d7997dc798f3947bb1e.tar.gz
configurable line wrapping (80 default)
disabled when set to a value below 10
-rw-r--r--src/conf.rs8
-rw-r--r--src/posts.rs25
2 files changed, 32 insertions, 1 deletions
diff --git a/src/conf.rs b/src/conf.rs
index 2a26a92..c392621 100644
--- a/src/conf.rs
+++ b/src/conf.rs
@@ -16,6 +16,14 @@ fn get_config() -> clap::ArgMatches<'static> {
                 .long("verbose")
                 .help("Verbose logging"),
         )
+        .arg(
+            Arg::with_name("line")
+                .short("l")
+                .long("line")
+                .value_name("LENGTH")
+                .takes_value(true)
+                .help("Line length (default: 80; a value <10 disables wrapping)"),
+        )
         .subcommand(clap::SubCommand::with_name("post").about("Post a new notice"))
         .subcommand(
             clap::SubCommand::with_name("update")
diff --git a/src/posts.rs b/src/posts.rs
index 4553b3e..c44e0f5 100644
--- a/src/posts.rs
+++ b/src/posts.rs
@@ -1,5 +1,6 @@
 use std::io;
 
+use crate::conf;
 use crate::db;
 use crate::error;
 use crate::user;
@@ -74,16 +75,38 @@ pub fn create() -> error::Result<()> {
 
 // Shows the most recent posts.
 pub fn display() -> error::Result<()> {
+    let args = &*conf::ARGS;
+    let line_len = args
+        .value_of("line")
+        .unwrap_or_else(|| "80")
+        .parse::<usize>()
+        .unwrap_or_else(|_| 80);
+
     let all = db::Posts::get_all(db::PATH);
 
     let mut postvec = Vec::new();
     all.posts().iter().enumerate().for_each(|(id, post)| {
+        let body = post
+            .body
+            .trim()
+            .chars()
+            .into_iter()
+            .enumerate()
+            .map(|(i, e)| {
+                let i = i + 1;
+                if line_len > 9 && i % line_len == 0 {
+                    return format!("{}\n", e);
+                }
+                e.to_string()
+            })
+            .collect::<String>();
+
         let newpost = format!(
             "{}. {} -> by {}\n{}\n\n",
             id + 1,
             post.title.trim(),
             post.author,
-            post.body.trim()
+            body
         );
         postvec.push(newpost);
     });