new notes

This commit is contained in:
2026-02-09 19:17:25 +01:00
parent 44616be8f6
commit a316a1ebc4
4 changed files with 473 additions and 45 deletions

View File

@@ -1,14 +1,14 @@
use chrono::{self, Datelike, Duration, Local, NaiveDate};
use chrono::{self, Datelike, Duration, Local, Month, NaiveDate, Weekday};
use clap::Parser;
use ron;
use serde::{Deserialize, Serialize};
use std::{
collections::HashSet,
fmt::Write as _,
fs::OpenOptions,
io::{BufRead, BufReader, Read, Seek, Write as _},
path::{PathBuf},
path::PathBuf,
};
use ron;
use serde::{Serialize, Deserialize};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
@@ -19,19 +19,9 @@ struct Args {
#[derive(Serialize, Deserialize, Debug)]
enum Pattern {
Yearly {
doy: u32,
},
MonthlyOpt {
dom: u32,
months: Vec<chrono::Month>,
},
Monthly {
dom: u32,
},
Weekly {
dow: u32,
},
MonthlyOpt { dom: u32, months: Vec<Month> },
Monthly { dom: u32 },
Weekly { dow: Weekday },
Daily,
}
@@ -41,10 +31,18 @@ struct Position {
pattern: Pattern,
}
fn month_from_date(date: NaiveDate) -> Month {
return Month::try_from(
u8::try_from(date.month())
.expect("This should never happen: month into u8 should never fail"),
)
.expect("This should never happen: month from u8 that is a month should never fail");
}
fn is_todo(bytes: &[u8]) -> bool {
return bytes == b"- [ ]" // done
|| bytes == b"- [r]" // tried unsucessfully, have to retry
|| bytes == b"- [p]" // Made progress, but not done
|| bytes == b"- [p]"; // Made progress, but not done
}
fn is_done(bytes: &[u8]) -> bool {
@@ -69,7 +67,6 @@ fn update_notes(notes_path: &PathBuf, patterns_path: Option<PathBuf>) {
let mut latest_date = NaiveDate::MIN;
let mut lines_to_not_use: Vec<usize> = Vec::new();
// Find todo-type lines
let mut n_lines = 0;
while let Ok(n) = notes_bufreader.read_line(&mut str_buf1) {
@@ -144,15 +141,16 @@ fn update_notes(notes_path: &PathBuf, patterns_path: Option<PathBuf>) {
.open(patterns)
.expect("file should exist");
let notes_bufreader = BufReader::new(&patterns_file);
let positions: Vec<Position> = ron::de::from_reader(notes_bufreader).expect("File should be of correct ron syntax");
let positions: Vec<Position> =
ron::de::from_reader(notes_bufreader).expect("File should be of correct ron syntax");
for p in &positions {
match p.pattern {
Pattern::Yearly { doy } => {}
Pattern::Monthly { dom } => {
let mut date_idx = latest_date.clone();
while date_idx <= today {
if date_idx.day() == dom {
writeln!(str_buf1, "- [ ] {}", &p.name).unwrap_or_else(|e| panic!("{e}"));
writeln!(str_buf1, "- [ ] {}", &p.name)
.unwrap_or_else(|e| panic!("{e}"));
break;
}
date_idx += Duration::days(1);
@@ -161,14 +159,27 @@ fn update_notes(notes_path: &PathBuf, patterns_path: Option<PathBuf>) {
Pattern::MonthlyOpt { dom, ref months } => {
let mut date_idx = latest_date.clone();
while date_idx <= today {
if date_idx.day() == dom && months.contains(&chrono::Month::try_from(u8::try_from(date_idx.month()).unwrap()).unwrap()) {
writeln!(str_buf1, "- [ ] {}", &p.name).unwrap_or_else(|e| panic!("{e}"));
if date_idx.day() == dom && months.contains(&month_from_date(date_idx)) {
writeln!(str_buf1, "- [ ] {}", &p.name)
.unwrap_or_else(|e| panic!("{e}"));
break;
}
date_idx += Duration::days(1);
}
}
Pattern::Weekly { dow } => {
dbg!(dow, today);
let mut date_idx = latest_date.clone();
while date_idx <= today {
dbg!(date_idx);
if date_idx.weekday() == dow {
writeln!(str_buf1, "- [ ] {}", &p.name)
.unwrap_or_else(|e| panic!("{e}"));
break;
}
date_idx += Duration::days(1);
}
}
Pattern::Weekly { dow } => {}
Pattern::Daily => {
writeln!(str_buf1, "- [ ] {}", &p.name).unwrap_or_else(|e| panic!("{e}"));
}
@@ -188,14 +199,15 @@ fn update_notes(notes_path: &PathBuf, patterns_path: Option<PathBuf>) {
str_buf2.clear();
notes_file.write_all(str_buf1.as_bytes()).unwrap_or_else(|e| panic!("Maaan look out for these files. Error is: {e}"));
notes_file
.write_all(str_buf1.as_bytes())
.unwrap_or_else(|e| panic!("Maaan look out for these files. Error is: {e}"));
}
fn main() {
let args = Args::parse();
if args.patterns == None {
dbg!("Warning: No patterns argument given. Will not generate any pattern-type todos");
}
update_notes(&args.notes, args.patterns);
}
// TODO für Patterns alle Tage zwischen heute und letztem Mal ausführen scannen