From 85eaf14e16335bed23c3f192945b92617cefdb91 Mon Sep 17 00:00:00 2001 From: Volodymyr Patuta <6977238-fiplox@users.noreply.gitlab.com> Date: Fri, 27 Nov 2020 15:08:39 +0100 Subject: [PATCH] prototype --- src/main.rs | 2 ++ src/notes.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 src/notes.rs diff --git a/src/main.rs b/src/main.rs index e7a11a9..2ba387f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +mod notes; + fn main() { println!("Hello, world!"); } diff --git a/src/notes.rs b/src/notes.rs new file mode 100644 index 0000000..8811e78 --- /dev/null +++ b/src/notes.rs @@ -0,0 +1,35 @@ +use anyhow::Result; +use chrono::Utc; +use std::{env, fs, process::Command}; + +fn get_base_path() -> Result { + let home = env::var("HOME")?; + Ok(format!("{}/.rnote/", home)) +} + +fn get_path(category: &str) -> Result { + let base = get_base_path()?; + let date = Utc::now().format("%Y-%m-%d"); + match category.is_empty() { + true => Ok(format!("{}{}", base, date)), + false => Ok(format!("{}{}", base, category)), + } +} + +pub fn create_dir(category: &str) -> Result<()> { + let path = get_base_path()?; + let date = Utc::now().format("%Y-%m-%d"); + match category.is_empty() { + true => fs::create_dir_all(format!("{}{}", path, date))?, + false => fs::create_dir_all(format!("{}{}", path, category))?, + } + Ok(()) +} + +pub fn create_note(header: &str, category: &str) -> Result<()> { + let editor = env::var("EDITOR").unwrap_or("/bin/vi".to_owned()); + let path = format!("{}{}", get_path(category)?, header); + // TODO: check if duplicate + Command::new(editor).arg(&path).status()?; + Ok(()) +}