Rough in new file header and journal links

This commit is contained in:
Thomas Gideon 2024-02-19 17:43:08 -05:00
parent 888ff7dea6
commit 70d5aa194b

View file

@ -1,5 +1,5 @@
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use chrono::{DateTime, Local, Utc}; use chrono::{DateTime, Duration, Local, Utc};
use clap::{arg, command, Parser}; use clap::{arg, command, Parser};
use log::{debug, trace}; use log::{debug, trace};
use megalodon::{ use megalodon::{
@ -126,15 +126,44 @@ async fn main() -> Result<()> {
let mut f = match try_exists(&output).await { let mut f = match try_exists(&output).await {
Ok(exists) if exists => { Ok(exists) if exists => {
debug!("Appending {}", output); debug!("Appending {}", output);
File::options().append(true).open(&output)? let mut file = File::options().append(true).open(&output)?;
file.write("\n".as_bytes())?;
file
} }
_ => { _ => {
debug!("Writing {}", output); debug!("Writing {}", output);
File::options() let mut file = File::options()
.create(true) .create(true)
.append(true) .append(true)
.open(&output) .open(&output)
.with_context(|| format!("Failed to create {}", output))? .with_context(|| format!("Failed to create {}", output))?;
file.write(format!("# {}\n\n", day.end.format("%Y-%m-%d")).as_bytes())?;
// TODO extract into a more general function
let week_ago = day.end - Duration::days(7);
// TODO check if the file exists
file.write(
format!("[One week ago](diary:{})\n", week_ago.format("%Y-%m-%d")).as_bytes(),
)?;
let month_ago = day.end - Duration::days(30);
// TODO check if the file exists
file.write(
format!("[One month ago](diary:{})\n", month_ago.format("%Y-%m-%d")).as_bytes(),
)?;
let half_year_ago = day.end - Duration::days(6 * 30);
// TODO check if the file exists
file.write(
format!(
"[Six months ago](diary:{})\n",
half_year_ago.format("%Y-%m-%d")
)
.as_bytes(),
)?;
let year_ago = day.end - Duration::days(365);
// TODO check if the file exists
file.write(
format!("[One year ago](diary:{})\n", year_ago.format("%Y-%m-%d")).as_bytes(),
)?;
file
} }
}; };
f.write_all(&reversed.join("\n\n").as_bytes()) f.write_all(&reversed.join("\n\n").as_bytes())