Compare commits

...

2 Commits

Author SHA1 Message Date
Andrei Stoica 3b1485d2c0 getting closest file to date 2024-03-06 14:50:03 -05:00
Andrei Stoica e4be0fb471 cli arguments 2024-03-06 14:31:53 -05:00
3 changed files with 110 additions and 13 deletions

View File

@ -10,4 +10,8 @@ pub struct Args {
/// show current config file
#[arg(short = 'C', long)]
pub current_config: bool,
/// veiw previous day's notes
#[arg(short = 'p', long, default_value_t = 0)]
pub previous: u16,
}

View File

@ -1,21 +1,32 @@
use chrono::Datelike;
use crate::TaskGroup;
use crate::todo::{File as TodoFile, Status as TaskStatus};
use crate::NaiveDate;
use crate::todo::{Status as TaskStatus,File as TodoFile};
use crate::TaskGroup;
use chrono::Datelike;
use comrak::nodes::{AstNode, NodeValue};
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
use std::str;
use std::io::Write;
use std::collections::HashMap;
use comrak::parse_document;
use std::path::{Path, PathBuf};
use comrak::{Arena, ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
use std::collections::HashMap;
use std::fs::{read, read_dir, File};
use std::io::Write;
use std::path::{Path, PathBuf};
use std::str;
#[derive(Debug, Clone, PartialEq)]
pub struct DatedPathBuf {
path: PathBuf,
date: NaiveDate,
}
#[derive(Debug)]
pub enum FileNameParseError {
TypeConversionError(&'static str),
ParseError(chrono::ParseError),
}
pub fn get_filepath(data_dir: &PathBuf, date: &NaiveDate) -> PathBuf {
let file_name = format!("{}-{:02}-{:02}.md", date.year(), date.month(), date.day());
let mut file_path = data_dir.clone();
file_path.push(file_name);
file_path
}
@ -114,3 +125,86 @@ pub fn get_latest_file(dir: &Path) -> Result<TodoFile, String> {
.reduce(|a, b| TodoFile::latest_file(a, b))
.ok_or("Could not reduce items".to_string())
}
fn try_get_date(file: &PathBuf) -> Result<NaiveDate, FileNameParseError> {
let file_name = file
.file_name()
.ok_or(FileNameParseError::TypeConversionError(
"Could not get filename from path: {:?}",
))?
.to_str()
.ok_or(FileNameParseError::TypeConversionError(
"Could not get filename from path: {:?}",
))?;
NaiveDate::parse_from_str(file_name, "%Y-%m-%d.md")
.or_else(|e| Err(FileNameParseError::ParseError(e)))
}
impl TryFrom<PathBuf> for DatedPathBuf {
type Error = FileNameParseError;
fn try_from(path: PathBuf) -> Result<Self, Self::Error> {
Ok(Self {
date: try_get_date(&path)?,
path,
})
}
}
pub fn get_closest_files(files: &Vec<PathBuf>, target: NaiveDate, n: usize) -> Vec<DatedPathBuf> {
let mut dated_files = files
.clone()
.into_iter()
.filter_map(|file| DatedPathBuf::try_from(file).ok())
.collect::<Vec<_>>();
dated_files.sort_by_cached_key(|dated_file| (dated_file.date - target).num_days().abs());
dated_files[..n].to_vec()
}
#[cfg(test)]
mod test {
use super::*;
use chrono::NaiveDate;
use std::path::PathBuf;
#[test]
fn test_get_closest_date() {
let files = vec![
PathBuf::from("./2024-01-01.md"),
PathBuf::from("./2024-01-02.md"),
PathBuf::from("./2024-01-03.md"),
PathBuf::from("./2024-02-01.md"),
PathBuf::from("./2024-03-01.md"),
PathBuf::from("./2024-04-01.md"),
PathBuf::from("./2024-04-02.md"),
PathBuf::from("./2024-04-03.md"),
PathBuf::from("./2024-04-04.md"),
];
let res = get_closest_files(&files, NaiveDate::from_ymd_opt(2023, 12, 30).unwrap(), 3);
let expected_res = vec![
DatedPathBuf::try_from(PathBuf::from("./2024-01-01.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-01-02.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-01-03.md")).unwrap(),
];
assert_eq!(res, expected_res);
let res = get_closest_files(&files, NaiveDate::from_ymd_opt(2024, 2, 1).unwrap(), 3);
let expected_res = vec![
DatedPathBuf::try_from(PathBuf::from("./2024-02-01.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-01-03.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-03-01.md")).unwrap(),
];
assert_eq!(res, expected_res);
let res = get_closest_files(&files, NaiveDate::from_ymd_opt(2024, 5, 2).unwrap(), 3);
let expected_res = vec![
DatedPathBuf::try_from(PathBuf::from("./2024-04-04.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-04-03.md")).unwrap(),
DatedPathBuf::try_from(PathBuf::from("./2024-04-02.md")).unwrap(),
];
assert_eq!(res, expected_res);
}
}

View File

@ -15,10 +15,9 @@ use std::fs;
use std::path::Path;
use std::process::Command;
//TODO refactor creating new file
fn main() {
let args = Args::parse();
println!("previous = {}", args.previous);
let expected_cfg_files = match Config::expected_locations() {
Ok(cfg_files) => cfg_files,
@ -113,8 +112,8 @@ fn main() {
Ok(todo_file) => todo_file.file.path(),
};
Command::new(cfg.editor)
Command::new(&cfg.editor)
.args([current_file])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
.expect(format!("failed to launch editor {}", &cfg.editor).as_str());
}