~gbmor/clinte

16fbf516e3b28d19570dd6e93e63b0efb9c249ff — Ben Morrison 1 year, 2 months ago 7d132af
moved check for val of $EDITOR into ed::call()

Also removed some allocations from call() in the form of unnecessary
string clones.
1 files changed, 19 insertions(+), 19 deletions(-)

M src/ed.rs
M src/ed.rs => src/ed.rs +19 -19
@@ 7,22 7,6 @@ use chrono::prelude::*;
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_rfc3339();
    let file_name = format!("/tmp/clinte_ed_{}_{}", *user::NAME, the_time);


@@ 36,17 20,33 @@ fn create_tmp_file<'a>() -> Result<String, &'a str> {
}

pub fn call() -> String {
    // If they don't have $EDITOR set, just default to nano
    // instead of assuming vim or emacs.
    let editor = match env::var("EDITOR") {
        Ok(ed) => {
            if &ed == "" {
                "nano".into()
            } else {
                ed
            }
        }
        Err(_) => {
            log::warn!("Couldn't get value of $EDITOR, defaulting to nano");
            "nano".into()
        }
    };

    let tmp_loc = error::helper(create_tmp_file());

    error::helper(
        process::Command::new(VAR.clone())
            .arg(tmp_loc.clone())
        process::Command::new(editor)
            .arg(&tmp_loc)
            .stdin(process::Stdio::inherit())
            .stdout(process::Stdio::inherit())
            .output(),
    );

    let body = error::helper(fs::read_to_string(tmp_loc.clone()));
    let body = error::helper(fs::read_to_string(&tmp_loc));
    error::helper(fs::remove_file(tmp_loc));
    body
}