Compare commits
No commits in common. "48df1fd9e01356c620607fb5e1cd22c915e5d4f5" and "c92da26ee79a53ccd17eac20ec14aa03c7e3cd02" have entirely different histories.
48df1fd9e0
...
c92da26ee7
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "rusty-tasks"
|
||||
version = "0.1.2"
|
||||
version = "0.1.1"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
|
|
|||
|
|
@ -11,13 +11,7 @@ pub struct Args {
|
|||
#[arg(short = 'C', long)]
|
||||
pub current_config: bool,
|
||||
|
||||
/// view previous day's notes
|
||||
/// veiw 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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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, File};
|
||||
use std::fs::{read, read_dir, File};
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::str;
|
||||
|
||||
#[derive(Debug)]
|
||||
|
|
@ -111,3 +111,11 @@ pub fn extract_secitons<'a>(
|
|||
}
|
||||
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())
|
||||
}
|
||||
|
|
|
|||
34
src/main.rs
34
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::{Local, TimeDelta};
|
||||
use chrono::{Datelike, 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!("{:?}", args);
|
||||
println!("previous = {}", args.previous);
|
||||
|
||||
let expected_cfg_files = match Config::expected_locations() {
|
||||
Ok(cfg_files) => cfg_files,
|
||||
|
|
@ -70,36 +70,28 @@ 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);
|
||||
if args.list {
|
||||
closest_files
|
||||
.into_iter()
|
||||
.for_each(|f| println!("{}", f.file.canonicalize().unwrap().to_string_lossy()));
|
||||
return ();
|
||||
}
|
||||
println!("{:?}", closest_files);
|
||||
|
||||
let latest_file = closest_files.first().ok_or("");
|
||||
|
||||
let latest_file = closest_files.first();
|
||||
let current_file = match latest_file {
|
||||
Some(todo_file) if todo_file.date < today && args.previous == 0 => {
|
||||
let sections = &cfg.sections;
|
||||
Ok(todo_file) if todo_file.date < today => {
|
||||
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) {
|
||||
|
|
@ -113,8 +105,7 @@ fn main() {
|
|||
file::write_file(&file_path, &content);
|
||||
file_path
|
||||
}
|
||||
Some(todo_file) => todo_file.file.clone(),
|
||||
None => {
|
||||
Err(_) => {
|
||||
let sections = &cfg.sections;
|
||||
let data = sections
|
||||
.iter()
|
||||
|
|
@ -125,6 +116,7 @@ fn main() {
|
|||
file::write_file(&file_path, &content);
|
||||
file_path
|
||||
}
|
||||
Ok(todo_file) => todo_file.file.clone(),
|
||||
};
|
||||
|
||||
Command::new(&cfg.editor)
|
||||
|
|
|
|||
|
|
@ -30,6 +30,14 @@ 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<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md")
|
||||
|
|
@ -163,7 +171,9 @@ 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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue