Compare commits

..

No commits in common. "3b1485d2c0f4c211047e472730130056d15c2bce" and "bce8f1e4b8de52a2ed7a5e05b52bf183b80d23b4" have entirely different histories.

3 changed files with 13 additions and 110 deletions

View File

@ -10,8 +10,4 @@ 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,32 +1,21 @@
use crate::todo::{File as TodoFile, Status as TaskStatus};
use crate::NaiveDate;
use crate::TaskGroup;
use chrono::Datelike;
use crate::TaskGroup;
use crate::NaiveDate;
use crate::todo::{Status as TaskStatus,File as TodoFile};
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::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),
}
use std::io::Write;
use std::collections::HashMap;
use comrak::parse_document;
use std::path::{Path, PathBuf};
use std::fs::{read, read_dir, File};
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
}
@ -125,86 +114,3 @@ 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,9 +15,10 @@ 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,
@ -112,8 +113,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 {}", &cfg.editor).as_str());
.expect(format!("failed to launch editor {}", "vim").as_str());
}