getting editor from environment

This commit is contained in:
Andrei Stoica 2023-06-13 08:41:37 -04:00
parent 5ba22cb6c9
commit ff25155aed
1 changed files with 24 additions and 3 deletions

View File

@ -13,23 +13,37 @@ use todo_file::TodoFile;
fn main() {
let data_dir = get_data_dir("notes");
println!("{}", data_dir.to_str().unwrap());
let latest_file =
get_latest_file(&data_dir).expect(format!("Could not find any notes files").as_str());
println!("Latest file: {:?}", latest_file);
let mut editor = Command::new(get_editor("vim".to_string()));
let now = Local::now();
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day());
match today {
Some(today) if latest_file.date < today => {
println!("Today's file does not exist, creating");
let today_file_name = format!("{}-{:02}-{:02}.md", today.year(), today.month(), today.day());
let today_file_name = format!(
"{}-{:02}-{:02}.md",
today.year(),
today.month(),
today.day()
);
let mut today_file_path = data_dir.clone();
today_file_path.push(today_file_name);
copy(latest_file.file.path(), today_file_path).unwrap();
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());
}
Some(_) => {
println!("Todays file was created");
Command::new("vim")
editor
.args([latest_file.file.path()])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
@ -112,6 +126,13 @@ mod todo_file {
}
}
fn get_editor(fallback: String) -> String {
match env::var("EDITOR") {
Ok(editor) => editor,
_ => fallback,
}
}
fn get_data_dir(dir_name: &str) -> PathBuf {
let mut dir = if let Ok(home) = env::var("HOME") {
let mut x = PathBuf::new();