summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock2
-rw-r--r--Cargo.toml2
-rw-r--r--src/ed.rs51
-rw-r--r--src/main.rs1
-rw-r--r--src/posts.rs6
5 files changed, 56 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index efa98b6..e4cc6e1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -119,7 +119,7 @@ dependencies = [
 
 [[package]]
 name = "clinte"
-version = "0.3.3"
+version = "0.4.0"
 dependencies = [
  "chrono 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)",
  "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)",
diff --git a/Cargo.toml b/Cargo.toml
index 0b72d94..3e53b9f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -1,6 +1,6 @@
 [package]
 name = "clinte"
-version = "0.3.3"
+version = "0.4.0"
 authors = ["Ben Morrison <ben@gbmor.dev>"]
 edition = "2018"
 
diff --git a/src/ed.rs b/src/ed.rs
new file mode 100644
index 0000000..17b73fa
--- /dev/null
+++ b/src/ed.rs
@@ -0,0 +1,51 @@
+use std::env;
+use std::fs;
+use std::process;
+
+use chrono::prelude::*;
+use log;
+
+use crate::error;
+use crate::user;
+
+lazy_static! {
+    static ref VAR: String = match env::var("EDITOR") {
+        Ok(ed) => {
+            if &ed == "" {
+                "nano".into()
+            } else {
+                ed
+            }
+        }
+        Err(err) => {
+            log::warn!("{:?}", err);
+            "nano".into()
+        }
+    };
+}
+
+fn create_tmp_file<'a>() -> Result<String, &'a str> {
+    let the_time = Utc::now().to_rfc2822();
+    let file_name = format!("/tmp/clinte_ed_{}_{}", *user::NAME, the_time);
+    match fs::write(&file_name, "") {
+        Ok(_) => Ok(file_name),
+        Err(err) => {
+            log::warn!("{:?}", err);
+            Err("Unable to create temp file")
+        }
+    }
+}
+
+pub fn call() -> String {
+    let tmp_loc = error::helper(create_tmp_file());
+
+    error::helper(
+        process::Command::new(VAR.clone())
+            .arg(tmp_loc.clone())
+            .stdin(process::Stdio::inherit())
+            .stdout(process::Stdio::inherit())
+            .output(),
+    );
+
+    error::helper(fs::read_to_string(tmp_loc))
+}
diff --git a/src/main.rs b/src/main.rs
index fcb7535..35df02f 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,6 +8,7 @@ use log::info;
 
 mod db;
 mod ed;
+mod error;
 mod logging;
 mod posts;
 mod user;
diff --git a/src/posts.rs b/src/posts.rs
index 27327ff..cb546ef 100644
--- a/src/posts.rs
+++ b/src/posts.rs
@@ -4,6 +4,7 @@ use std::io;
 use rusqlite;
 
 use crate::db;
+use crate::ed;
 use crate::user;
 
 type Result<T> = std::result::Result<T, Box<dyn Error>>;
@@ -50,10 +51,7 @@ pub fn create(db: &db::Conn) {
     };
 
     println!();
-    println!("Body of the new post: ");
-    let mut body = String::new();
-    io::stdin().read_line(&mut body).unwrap();
-    let body = str_to_utf8(body.trim());
+    let body = str_to_utf8(&ed::call());
     let body = if body.len() > 500 {
         &body[..500]
     } else {
'n298' href='#n298'>298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362