summary refs log tree commit diff stats
path: root/src/ed.rs
diff options
context:
space:
mode:
authorBen Morrison <ben@gbmor.dev>2019-09-04 13:52:44 -0400
committerBen Morrison <ben@gbmor.dev>2019-09-04 13:52:44 -0400
commitc0ce5eff2797dd7e5652fd15594f170cd90d8ac9 (patch)
tree9996f3db059672a704f9b9942c4e67d7867bbb30 /src/ed.rs
parent8e3e31ee2b6721ed4ccb958fefec1431372e39ba (diff)
downloadclinte-c0ce5eff2797dd7e5652fd15594f170cd90d8ac9.tar.gz
now opens $EDITOR to write post body
Diffstat (limited to 'src/ed.rs')
-rw-r--r--src/ed.rs51
1 files changed, 51 insertions, 0 deletions
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))
+}