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::fs;
use std::path::Path;

pub fn edit(vault: &Vault, name: String) -> Result<bool> {
  check_filename(&name)?;
  let base = Path::new(&vault.ideas);
  let path = base.join(name).with_extension("md");
  let content_before = read_safe(&path);
  editor::run(&path)?;
  let content_after = read_safe(&path);
  let saved = content_before != content_after;
  // cleanup if necessary
  if content_after == "" && path.exists() {
    fs::remove_file(&path)?;
  }
  Ok(saved)
}

pub fn remove(vault: &Vault, name: String) -> Result<()> {
  check_filename(&name)?;
  let base = Path::new(&vault.ideas);
  let path = base.join(&name).with_extension("md");
  if path.exists() {
    let question = format!("Are you sure you want to remove [{}] idea?", &name);
    match prompt_yes_no(question.as_str(), YesNo::No)? {
      YesNo::Yes => {
        fs::remove_file(&path)?;
        println!("Idea [{}] was successfully removed", &name);
      }
      YesNo::No => println!("Nothing was removed"),
    }
  } else {
    println!("Idea [{}] was not found", &name);
  }
  Ok(())
}

pub fn remove_noprompt(vault: &Vault, name: String) -> Result<bool> {
  check_filename(&name)?;
  let base = Path::new(&vault.ideas);
  let path = base.join(&name).with_extension("md");
  if path.exists() {
    fs::remove_file(&path)?;
    Ok(true)
  } else {
    Ok(false)
  }
}

pub fn list(vault: &Vault) -> Result<()> {
  for (idea, _) in vault.list_ideas()? {
    println!("{}", idea);
  }
  Ok(())
}