Compare commits

...

2 Commits

Author SHA1 Message Date
Andrei Stoica 48df1fd9e0 v0.1.2 2024-03-18 22:02:46 -04:00
Andrei Stoica cfe9065243 using new file selection logic 2024-03-18 22:02:24 -04:00
5 changed files with 32 additions and 36 deletions

View File

@ -1,6 +1,6 @@
[package] [package]
name = "rusty-tasks" name = "rusty-tasks"
version = "0.1.1" version = "0.1.2"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

View File

@ -11,7 +11,13 @@ pub struct Args {
#[arg(short = 'C', long)] #[arg(short = 'C', long)]
pub current_config: bool, pub current_config: bool,
/// veiw previous day's notes /// view previous day's notes
#[arg(short = 'p', long, default_value_t = 0)] #[arg(short = 'p', long, default_value_t = 0)]
pub previous: u16, 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,
} }

View File

@ -6,9 +6,9 @@ use comrak::nodes::{AstNode, NodeValue};
use comrak::parse_document; use comrak::parse_document;
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions}; use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
use std::collections::HashMap; use std::collections::HashMap;
use std::fs::{read, read_dir, File}; use std::fs::{read, File};
use std::io::Write; use std::io::Write;
use std::path::{Path, PathBuf}; use std::path::PathBuf;
use std::str; use std::str;
#[derive(Debug)] #[derive(Debug)]
@ -111,11 +111,3 @@ pub fn extract_secitons<'a>(
} }
groups groups
} }
pub fn get_latest_file(dir: &Path) -> Result<TodoFile, String> {
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())
}

View File

@ -7,7 +7,7 @@ use crate::cli::Args;
use crate::config::Config; use crate::config::Config;
use crate::todo::{File as TodoFile, TaskGroup}; use crate::todo::{File as TodoFile, TaskGroup};
use chrono::naive::NaiveDate; use chrono::naive::NaiveDate;
use chrono::{Datelike, Local, TimeDelta}; use chrono::{Local, TimeDelta};
use clap::Parser; use clap::Parser;
use comrak::Arena; use comrak::Arena;
use resolve_path::PathResolveExt; use resolve_path::PathResolveExt;
@ -17,7 +17,7 @@ use std::process::Command;
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
println!("previous = {}", args.previous); println!("{:?}", args);
let expected_cfg_files = match Config::expected_locations() { let expected_cfg_files = match Config::expected_locations() {
Ok(cfg_files) => cfg_files, Ok(cfg_files) => cfg_files,
@ -70,28 +70,36 @@ fn main() {
.expect(format!("Could not find notes folder: {:?}", &data_dir).as_str()) .expect(format!("Could not find notes folder: {:?}", &data_dir).as_str())
.filter_map(|f| f.ok()) .filter_map(|f| f.ok())
.map(|file| file.path()); .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 today = Local::now().date_naive();
let target = today - TimeDelta::try_days(args.previous.into()).unwrap(); let target = today - TimeDelta::try_days(args.previous.into()).unwrap();
let closest_files = TodoFile::get_closest_files(files.collect(), target, 5); let closest_files = TodoFile::get_closest_files(files.collect(), target, 5);
println!("{:?}", closest_files); if args.list {
closest_files
let latest_file = closest_files.first().ok_or(""); .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 { 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 arena = Arena::new();
let root = { let root = {
let contents = file::load_file(&todo_file); let contents = file::load_file(&todo_file);
let root = file::parse_todo_file(&contents, &arena); let root = file::parse_todo_file(&contents, &arena);
root root
}; };
let sections = &cfg.sections;
let groups = file::extract_secitons(root, sections); let groups = file::extract_secitons(root, sections);
let level = groups.values().map(|group| group.level).min().unwrap_or(2); let level = groups.values().map(|group| group.level).min().unwrap_or(2);
let data = sections let data = sections
.iter() .iter()
.map(|section| match groups.get(section) { .map(|section| match groups.get(section) {
@ -105,7 +113,8 @@ fn main() {
file::write_file(&file_path, &content); file::write_file(&file_path, &content);
file_path file_path
} }
Err(_) => { Some(todo_file) => todo_file.file.clone(),
None => {
let sections = &cfg.sections; let sections = &cfg.sections;
let data = sections let data = sections
.iter() .iter()
@ -116,7 +125,6 @@ fn main() {
file::write_file(&file_path, &content); file::write_file(&file_path, &content);
file_path file_path
} }
Ok(todo_file) => todo_file.file.clone(),
}; };
Command::new(&cfg.editor) Command::new(&cfg.editor)

View File

@ -30,14 +30,6 @@ impl File {
.ok_or("Something went wrong".to_owned())?) .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 { fn get_file_regex() -> Regex {
//TODO This would ideally be configurable //TODO This would ideally be configurable
Regex::new(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md") Regex::new(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md")
@ -171,9 +163,7 @@ mod test {
NaiveDate::from_ymd_opt(2023, 12, 30).unwrap(), NaiveDate::from_ymd_opt(2023, 12, 30).unwrap(),
3, 3,
); );
let expected_res = vec![ let expected_res = vec![File::try_from(PathBuf::from("./2024-01-01.md")).unwrap()];
File::try_from(PathBuf::from("./2024-01-01.md")).unwrap(),
];
assert_eq!(res, expected_res); assert_eq!(res, expected_res);
} }
} }