2023-06-12 23:46:40 -04:00
|
|
|
use chrono::naive::NaiveDate;
|
2023-06-13 07:59:09 -04:00
|
|
|
use chrono::{Datelike, Local};
|
2023-06-12 23:46:40 -04:00
|
|
|
use std::env;
|
2023-06-13 08:30:59 -04:00
|
|
|
use std::fs::{copy, read_dir};
|
2023-06-13 07:59:09 -04:00
|
|
|
use std::path::{Path, PathBuf};
|
2023-06-13 08:10:18 -04:00
|
|
|
use std::process::Command;
|
2023-06-13 07:59:09 -04:00
|
|
|
use todo_file::TodoFile;
|
2023-06-12 23:46:40 -04:00
|
|
|
|
|
|
|
|
//TODO handle unwraps and errors more uniformly
|
2023-06-13 07:59:09 -04:00
|
|
|
//TODO move TodoFile into its file
|
2023-06-13 08:30:59 -04:00
|
|
|
//TODO clean up verbose printing
|
2023-06-12 23:46:40 -04:00
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
|
let data_dir = get_data_dir("notes");
|
|
|
|
|
println!("{}", data_dir.to_str().unwrap());
|
2023-06-13 08:41:37 -04:00
|
|
|
|
2023-06-13 08:10:18 -04:00
|
|
|
let latest_file =
|
|
|
|
|
get_latest_file(&data_dir).expect(format!("Could not find any notes files").as_str());
|
2023-06-12 23:46:40 -04:00
|
|
|
println!("Latest file: {:?}", latest_file);
|
2023-06-13 08:41:37 -04:00
|
|
|
|
|
|
|
|
let mut editor = Command::new(get_editor("vim".to_string()));
|
|
|
|
|
|
2023-06-12 23:46:40 -04:00
|
|
|
let now = Local::now();
|
|
|
|
|
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day());
|
|
|
|
|
match today {
|
2023-06-13 08:30:59 -04:00
|
|
|
Some(today) if latest_file.date < today => {
|
|
|
|
|
println!("Today's file does not exist, creating");
|
2023-06-13 08:41:37 -04:00
|
|
|
let today_file_name = format!(
|
|
|
|
|
"{}-{:02}-{:02}.md",
|
|
|
|
|
today.year(),
|
|
|
|
|
today.month(),
|
|
|
|
|
today.day()
|
|
|
|
|
);
|
2023-06-13 08:30:59 -04:00
|
|
|
let mut today_file_path = data_dir.clone();
|
|
|
|
|
today_file_path.push(today_file_name);
|
|
|
|
|
|
2023-06-13 08:41:37 -04:00
|
|
|
copy(latest_file.file.path(), today_file_path.clone()).unwrap();
|
|
|
|
|
|
|
|
|
|
editor
|
|
|
|
|
.args([today_file_path])
|
|
|
|
|
.status()
|
|
|
|
|
.expect(format!("failed to launch editor {}", "vim").as_str());
|
2023-06-13 08:30:59 -04:00
|
|
|
}
|
|
|
|
|
Some(_) => {
|
2023-06-13 08:10:18 -04:00
|
|
|
println!("Todays file was created");
|
2023-06-13 08:41:37 -04:00
|
|
|
editor
|
2023-06-13 08:10:18 -04:00
|
|
|
.args([latest_file.file.path()])
|
|
|
|
|
.status()
|
|
|
|
|
.expect(format!("failed to launch editor {}", "vim").as_str());
|
|
|
|
|
}
|
2023-06-13 08:30:59 -04:00
|
|
|
_ => println!("Could not get today's date"),
|
2023-06-13 07:59:09 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mod todo_file {
|
|
|
|
|
use chrono::naive::NaiveDate;
|
2023-06-13 08:10:18 -04:00
|
|
|
use regex::Regex;
|
2023-06-13 07:59:09 -04:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
use std::fs::DirEntry;
|
|
|
|
|
use std::str::FromStr;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct TodoFile {
|
|
|
|
|
pub file: DirEntry,
|
|
|
|
|
pub date: NaiveDate,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TodoFile {
|
|
|
|
|
fn capture_as_number<T: FromStr>(
|
|
|
|
|
capture: ®ex::Captures,
|
|
|
|
|
name: &str,
|
|
|
|
|
) -> Result<T, String> {
|
|
|
|
|
Ok(capture
|
|
|
|
|
.name(name)
|
|
|
|
|
.unwrap()
|
|
|
|
|
.as_str()
|
|
|
|
|
.parse::<T>()
|
|
|
|
|
.ok()
|
|
|
|
|
.ok_or("Something went wrong".to_owned())?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn latest_file(a: TodoFile, b: TodoFile) -> TodoFile {
|
|
|
|
|
if a.date > b.date {
|
|
|
|
|
a
|
|
|
|
|
} else {
|
|
|
|
|
b
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_file_regex() -> Regex {
|
2023-06-13 08:10:18 -04:00
|
|
|
//TODO This would ideally be configurable
|
2023-06-13 07:59:09 -04:00
|
|
|
Regex::new(r"(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2}).md")
|
|
|
|
|
.expect("could not create regex")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl TryFrom<DirEntry> for TodoFile {
|
|
|
|
|
type Error = String;
|
|
|
|
|
|
|
|
|
|
fn try_from(direntry: DirEntry) -> Result<Self, Self::Error> {
|
|
|
|
|
let re = TodoFile::get_file_regex();
|
|
|
|
|
println!("{:?}", re);
|
|
|
|
|
let file_name = direntry.file_name();
|
|
|
|
|
let file_name_str = match file_name.to_str() {
|
|
|
|
|
Some(name) => name,
|
|
|
|
|
_ => "",
|
|
|
|
|
};
|
|
|
|
|
println!("{:?}", file_name_str);
|
|
|
|
|
|
|
|
|
|
if let Some(caps) = re.captures(file_name_str) {
|
|
|
|
|
let year: i32 = Self::capture_as_number(&caps, "year").unwrap();
|
|
|
|
|
let month: u32 = Self::capture_as_number(&caps, "month").unwrap();
|
|
|
|
|
let day: u32 = Self::capture_as_number(&caps, "day").unwrap();
|
|
|
|
|
|
|
|
|
|
return Ok(Self {
|
|
|
|
|
file: direntry,
|
|
|
|
|
date: NaiveDate::from_ymd_opt(year, month, day).unwrap(),
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
Err(format!(
|
|
|
|
|
"Could not parse file name => {{ name: {:?}, re: {:?} }}",
|
|
|
|
|
file_name, re
|
|
|
|
|
)
|
|
|
|
|
.to_string())
|
|
|
|
|
}
|
2023-06-12 23:46:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-13 08:41:37 -04:00
|
|
|
fn get_editor(fallback: String) -> String {
|
|
|
|
|
match env::var("EDITOR") {
|
|
|
|
|
Ok(editor) => editor,
|
|
|
|
|
_ => fallback,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-12 23:46:40 -04:00
|
|
|
fn get_data_dir(dir_name: &str) -> PathBuf {
|
|
|
|
|
let mut dir = if let Ok(home) = env::var("HOME") {
|
|
|
|
|
let mut x = PathBuf::new();
|
|
|
|
|
x.push(home);
|
|
|
|
|
x
|
|
|
|
|
} else {
|
|
|
|
|
env::current_dir().expect("PWD environment variable not set")
|
|
|
|
|
};
|
|
|
|
|
dir = dir.join(dir_name);
|
|
|
|
|
dir
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn get_latest_file(dir: &Path) -> Result<TodoFile, String> {
|
2023-06-13 08:30:59 -04:00
|
|
|
let dir = read_dir(dir).expect(format!("Could not find notes folder: {:?}", dir).as_str());
|
2023-06-12 23:46:40 -04:00
|
|
|
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())
|
|
|
|
|
}
|