~gbmor/clinte

c0ce5eff2797dd7e5652fd15594f170cd90d8ac9 — Ben Morrison 1 year, 10 months ago 8e3e31e
now opens $EDITOR to write post body
5 files changed, 56 insertions(+), 6 deletions(-)

M Cargo.lock
M Cargo.toml
A src/ed.rs
M src/main.rs
M src/posts.rs
M Cargo.lock => Cargo.lock +1 -1
@@ 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)",

M Cargo.toml => Cargo.toml +1 -1
@@ 1,6 1,6 @@
[package]
name = "clinte"
version = "0.3.3"
version = "0.4.0"
authors = ["Ben Morrison <ben@gbmor.dev>"]
edition = "2018"


A src/ed.rs => src/ed.rs +51 -0
@@ 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))
}

M src/main.rs => src/main.rs +1 -0
@@ 8,6 8,7 @@ use log::info;

mod db;
mod ed;
mod error;
mod logging;
mod posts;
mod user;

M src/posts.rs => src/posts.rs +2 -4
@@ 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 {