launching editor for current file

This commit is contained in:
Andrei Stoica 2023-06-13 08:10:18 -04:00
parent 175a6c20a8
commit 9c96744bda
1 changed files with 12 additions and 11 deletions

View File

@ -3,6 +3,7 @@ use chrono::{Datelike, Local};
use std::env; use std::env;
use std::fs; use std::fs;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::process::Command;
use todo_file::TodoFile; use todo_file::TodoFile;
//TODO handle unwraps and errors more uniformly //TODO handle unwraps and errors more uniformly
@ -12,19 +13,19 @@ fn main() {
let data_dir = get_data_dir("notes"); let data_dir = get_data_dir("notes");
println!("{}", data_dir.to_str().unwrap()); println!("{}", data_dir.to_str().unwrap());
let latest_file = get_latest_file(&data_dir).unwrap(); let latest_file =
//.expect( get_latest_file(&data_dir).expect(format!("Could not find any notes files").as_str());
// format!(
// "Could not find any notes files please use format: {}",
// get_file_regex().to_string()
// )
// .as_str(),
//);
println!("Latest file: {:?}", latest_file); println!("Latest file: {:?}", latest_file);
let now = Local::now(); let now = Local::now();
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day()); let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day());
match today { match today {
Some(today) if latest_file.date == today => println!("Todays file was created"), Some(today) if latest_file.date == today => {
println!("Todays file was created");
Command::new("vim")
.args([latest_file.file.path()])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
}
Some(today) if latest_file.date < today => println!("Todays file was not created"), Some(today) if latest_file.date < today => println!("Todays file was not created"),
Some(today) if latest_file.date > today => println!("Future files were created"), Some(today) if latest_file.date > today => println!("Future files were created"),
@ -34,10 +35,10 @@ fn main() {
mod todo_file { mod todo_file {
use chrono::naive::NaiveDate; use chrono::naive::NaiveDate;
use regex::Regex;
use std::convert::TryFrom; use std::convert::TryFrom;
use std::fs::DirEntry; use std::fs::DirEntry;
use std::str::FromStr; use std::str::FromStr;
use regex::Regex;
#[derive(Debug)] #[derive(Debug)]
pub struct TodoFile { pub struct TodoFile {
@ -68,7 +69,7 @@ mod todo_file {
} }
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")
.expect("could not create regex") .expect("could not create regex")
} }