mj 0.4.3

My Journal - personal tool to capture ideas, work with journals, notes and tasks in your favourite text $EDITOR.
Documentation
use crate::core::*;
use std::env::var;
use std::path::PathBuf;
use std::process::*;

const DEFAULT_EDITOR: &str = "vi";

fn find_editor() -> (String, Vec<String>) {
  match var("EDITOR") {
    Ok(value) => {
      let mut parts = value.split(' ');
      let editor = parts.next().unwrap();
      let args = parts.map(|x| x.to_owned()).collect::<Vec<String>>();
      (editor.to_owned(), args)
    },
    Err(_) => (DEFAULT_EDITOR.to_owned(), vec![])
  }
}

pub fn run(path: &PathBuf) -> Result<()> {
  let (editor, args) = find_editor();
  Command::new(editor.to_owned())
    .args(args)
    .arg(path)
    .spawn()?
    .wait_with_output()?;
  Ok(())
}