retrieving node dir from config and create new file when dir is empty

This commit is contained in:
Andrei Stoica 2023-06-23 10:47:43 -04:00
parent 145905066c
commit 02ed04fad5
3 changed files with 46 additions and 14 deletions

5
.gitignore vendored
View File

@ -17,3 +17,8 @@ Cargo.lock
# Added by cargo # Added by cargo
/target /target
# config file
.rusty_task.json

View File

@ -59,7 +59,6 @@ impl Config {
home_cfg.push(format!(".{}", cfg_name)); home_cfg.push(format!(".{}", cfg_name));
let mut pwd_cfg = PathBuf::from(pwd.clone()); let mut pwd_cfg = PathBuf::from(pwd.clone());
pwd_cfg.push(cfg_name);
pwd_cfg.push(format!(".{}", cfg_name)); pwd_cfg.push(format!(".{}", cfg_name));
Ok(vec![home_config_cfg, home_cfg, pwd_cfg]) Ok(vec![home_config_cfg, home_cfg, pwd_cfg])

View File

@ -15,17 +15,18 @@ use std::collections::HashMap;
use std::env; use std::env;
use std::fs::{read, read_dir, File}; use std::fs::{read, read_dir, File};
use std::io::Write; use std::io::Write;
use std::iter::FromIterator;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command; use std::process::Command;
use std::str; use std::str;
//TODO handle unwraps and errors more uniformly //TODO handle unwraps and errors more uniformly
//TODO refactor creating new file
//TODO clean up verbose printing //TODO clean up verbose printing
//TODO create custom errors for better error handling //TODO create custom errors for better error handling
fn main() { fn main() {
let expected_cfg_files = Config::expected_locations().unwrap(); let expected_cfg_files = Config::expected_locations().unwrap();
println!("{:#?}", expected_cfg_files);
let cfg_files: Vec<&Path> = expected_cfg_files let cfg_files: Vec<&Path> = expected_cfg_files
.iter() .iter()
.map(|file| Path::new(file)) .map(|file| Path::new(file))
@ -42,17 +43,17 @@ fn main() {
let cfg = Config::load(cfg_files.last().unwrap().to_str().unwrap()).unwrap(); let cfg = Config::load(cfg_files.last().unwrap().to_str().unwrap()).unwrap();
println!("{:#?}", cfg); println!("{:#?}", cfg);
let data_dir = get_data_dir("notes"); let data_dir = get_data_dir(&cfg.notes_dir.clone().expect("Could not get notes dir from config"));
println!("dir = {}", data_dir.to_str().unwrap()); println!("dir = {}", data_dir.to_str().unwrap());
let latest_file = let latest_file =
get_latest_file(&data_dir).expect(format!("Could not find any notes files").as_str()); get_latest_file(&data_dir);
println!("Latest file: {:?}", latest_file); println!("Latest file: {:?}", latest_file);
let now = Local::now(); let now = Local::now();
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day()); let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day()).unwrap();
let current_file = match today { let current_file = match latest_file {
Some(today) if latest_file.date < today => { Ok(file) if file.date < today => {
println!("Today's file does not exist, creating"); println!("Today's file does not exist, creating");
let today_file_name = format!( let today_file_name = format!(
"{}-{:02}-{:02}.md", "{}-{:02}-{:02}.md",
@ -64,7 +65,7 @@ fn main() {
today_file_path.push(today_file_name); today_file_path.push(today_file_name);
let arena = Arena::new(); let arena = Arena::new();
let root = parse_todo_file(&latest_file, &arena); let root = parse_todo_file(&file, &arena);
//println!("{:#?}", root); //println!("{:#?}", root);
//println!("======================================================="); //println!("=======================================================");
@ -82,7 +83,6 @@ fn main() {
None => TaskGroup::empty(section.to_string(), level), None => TaskGroup::empty(section.to_string(), level),
}) })
.for_each(|task_group| println!("{}", task_group.to_string())); .for_each(|task_group| println!("{}", task_group.to_string()));
let mut content = format!( let mut content = format!(
"# Today's tasks {}-{:02}-{:02}\n", "# Today's tasks {}-{:02}-{:02}\n",
today.year(), today.year(),
@ -106,13 +106,41 @@ fn main() {
Some(today_file_path) Some(today_file_path)
} }
Some(_) => { Ok(file) => {
println!("Today's file was created"); println!("Today's file was created");
Some(latest_file.file.path()) Some(file.file.path())
} }
_ => { Err(_) => {
println!("Could not get today's date"); println!("No files in dir: {:}", cfg.notes_dir.unwrap());
None
let today_file_name = format!(
"{}-{:02}-{:02}.md",
today.year(),
today.month(),
today.day()
);
let mut today_file_path = data_dir.clone();
today_file_path.push(today_file_name);
let sections = &cfg.sections.unwrap();
let mut content = format!(
"# Today's tasks {}-{:02}-{:02}\n",
today.year(),
today.month(),
today.day()
);
sections
.iter()
.map(|section| TaskGroup::empty(section.to_string(), 2))
.for_each(|task_group| {
content.push_str(format!("\n{}", task_group.to_string()).as_str())
});
let mut file = File::create(today_file_path.clone())
.expect("Could not open today's file: {today_file_path}");
write!(file, "{}", content).expect("Could not write to file: {today_file_path}");
Some(today_file_path)
} }
}; };