From cfe9065243ac5f56edee6b16e46cf44f039b265b Mon Sep 17 00:00:00 2001 From: Andrei Stoica Date: Mon, 18 Mar 2024 22:02:24 -0400 Subject: [PATCH] using new file selection logic --- src/cli/mod.rs | 8 +++++++- src/file/mod.rs | 12 ++---------- src/main.rs | 34 +++++++++++++++++++++------------- src/todo/file.rs | 12 +----------- 4 files changed, 31 insertions(+), 35 deletions(-) diff --git a/src/cli/mod.rs b/src/cli/mod.rs index c902f9d..2dcbf11 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -11,7 +11,13 @@ pub struct Args { #[arg(short = 'C', long)] pub current_config: bool, - /// veiw previous day's notes + /// view previous day's notes #[arg(short = 'p', long, default_value_t = 0)] pub previous: u16, + /// list closest files to date + #[arg(short, long)] + pub list: bool, + /// list closest files to date + #[arg(short = 'L', long)] + pub list_all: bool, } diff --git a/src/file/mod.rs b/src/file/mod.rs index 247bf8e..3fa4c76 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -6,9 +6,9 @@ use comrak::nodes::{AstNode, NodeValue}; use comrak::parse_document; use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions}; use std::collections::HashMap; -use std::fs::{read, read_dir, File}; +use std::fs::{read, File}; use std::io::Write; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use std::str; #[derive(Debug)] @@ -111,11 +111,3 @@ pub fn extract_secitons<'a>( } groups } - -pub fn get_latest_file(dir: &Path) -> Result { - let dir = read_dir(dir).expect(format!("Could not find notes folder: {:?}", dir).as_str()); - dir.filter_map(|f| f.ok()) - .filter_map(|file| TodoFile::try_from(file).ok()) - .reduce(|a, b| TodoFile::latest_file(a, b)) - .ok_or("Could not reduce items".to_string()) -} diff --git a/src/main.rs b/src/main.rs index b18bd52..5013b8d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -7,7 +7,7 @@ use crate::cli::Args; use crate::config::Config; use crate::todo::{File as TodoFile, TaskGroup}; use chrono::naive::NaiveDate; -use chrono::{Datelike, Local, TimeDelta}; +use chrono::{Local, TimeDelta}; use clap::Parser; use comrak::Arena; use resolve_path::PathResolveExt; @@ -17,7 +17,7 @@ use std::process::Command; fn main() { let args = Args::parse(); - println!("previous = {}", args.previous); + println!("{:?}", args); let expected_cfg_files = match Config::expected_locations() { Ok(cfg_files) => cfg_files, @@ -70,28 +70,36 @@ fn main() { .expect(format!("Could not find notes folder: {:?}", &data_dir).as_str()) .filter_map(|f| f.ok()) .map(|file| file.path()); + if args.list_all { + files + .into_iter() + .for_each(|f| println!("{}", f.canonicalize().unwrap().to_string_lossy())); + return (); + } + let today = Local::now().date_naive(); let target = today - TimeDelta::try_days(args.previous.into()).unwrap(); let closest_files = TodoFile::get_closest_files(files.collect(), target, 5); - println!("{:?}", closest_files); - - let latest_file = closest_files.first().ok_or(""); + if args.list { + closest_files + .into_iter() + .for_each(|f| println!("{}", f.file.canonicalize().unwrap().to_string_lossy())); + return (); + } + let latest_file = closest_files.first(); let current_file = match latest_file { - Ok(todo_file) if todo_file.date < today => { + Some(todo_file) if todo_file.date < today && args.previous == 0 => { + let sections = &cfg.sections; let arena = Arena::new(); + let root = { let contents = file::load_file(&todo_file); let root = file::parse_todo_file(&contents, &arena); root }; - - let sections = &cfg.sections; - let groups = file::extract_secitons(root, sections); - let level = groups.values().map(|group| group.level).min().unwrap_or(2); - let data = sections .iter() .map(|section| match groups.get(section) { @@ -105,7 +113,8 @@ fn main() { file::write_file(&file_path, &content); file_path } - Err(_) => { + Some(todo_file) => todo_file.file.clone(), + None => { let sections = &cfg.sections; let data = sections .iter() @@ -116,7 +125,6 @@ fn main() { file::write_file(&file_path, &content); file_path } - Ok(todo_file) => todo_file.file.clone(), }; Command::new(&cfg.editor) diff --git a/src/todo/file.rs b/src/todo/file.rs index bb9c0a1..7fba99f 100644 --- a/src/todo/file.rs +++ b/src/todo/file.rs @@ -30,14 +30,6 @@ impl File { .ok_or("Something went wrong".to_owned())?) } - pub fn latest_file(a: File, b: File) -> File { - if a.date > b.date { - a - } else { - b - } - } - fn get_file_regex() -> Regex { //TODO This would ideally be configurable Regex::new(r"(?P\d{4})-(?P\d{2})-(?P\d{2}).md") @@ -171,9 +163,7 @@ mod test { NaiveDate::from_ymd_opt(2023, 12, 30).unwrap(), 3, ); - let expected_res = vec![ - File::try_from(PathBuf::from("./2024-01-01.md")).unwrap(), - ]; + let expected_res = vec![File::try_from(PathBuf::from("./2024-01-01.md")).unwrap()]; assert_eq!(res, expected_res); } }