add proof of concept for patterns

This commit is contained in:
2026-02-04 17:30:46 +01:00
parent 1a6d05c05f
commit 44616be8f6
4 changed files with 118 additions and 62 deletions

View File

@@ -7,5 +7,5 @@ edition = "2024"
chrono = { version = "0.4.43", features = ["serde"] } chrono = { version = "0.4.43", features = ["serde"] }
clap = { version = "4.5.54", features = ["derive"] } clap = { version = "4.5.54", features = ["derive"] }
env = "1.0.1" env = "1.0.1"
ron = "0.12.0" ron = { version = "0.12.0" }
serde = { version = "1.0.228", features = ["derive"] } serde = { version = "1.0.228", features = ["derive"] }

View File

@@ -16,8 +16,12 @@
- [ ] Test 2 - [ ] Test 2
- [ ] Test 3 - [ ] Test 3
# 2026-02-03 # 2026-02-04
- [ ] Rechnung Gertrudenhof
- [ ] Rechnung an lustige Leute
- [ ] Cornelius hauen
- [ ] Rechnung STG
- [ ] Test 2 - [ ] Test 2
- [ ] Test 3 - [ ] Test 3

View File

@@ -3,9 +3,17 @@
name: "Rechnung Gertrudenhof", name: "Rechnung Gertrudenhof",
pattern: Monthly(dom: 1), pattern: Monthly(dom: 1),
), ),
(
name: "Rechnung an lustige Leute",
pattern: Monthly(dom: 4),
),
(
name: "Cornelius hauen",
pattern: Daily,
),
( (
name: "Rechnung STG", name: "Rechnung STG",
pattern: MonthlyOpt(dom: 1, months: [February, May, August, November]), pattern: MonthlyOpt(dom: 1, months: ["February", "May", "August", "November"]),
), ),
( (
name: "Kalender durchlesen", name: "Kalender durchlesen",

View File

@@ -1,12 +1,14 @@
use chrono::{self, Local, NaiveDate}; use chrono::{self, Datelike, Duration, Local, NaiveDate};
use clap::Parser; use clap::Parser;
use std::{ use std::{
collections::HashSet, collections::HashSet,
fmt::Write as _, fmt::Write as _,
fs::OpenOptions, fs::OpenOptions,
io::{BufRead, BufReader, Read, Seek, Write as _}, io::{BufRead, BufReader, Read, Seek, Write as _},
path::{Path, PathBuf}, path::{PathBuf},
}; };
use ron;
use serde::{Serialize, Deserialize};
#[derive(Parser, Debug)] #[derive(Parser, Debug)]
#[command(version, about, long_about = None)] #[command(version, about, long_about = None)]
@@ -15,75 +17,79 @@ struct Args {
patterns: Option<PathBuf>, patterns: Option<PathBuf>,
} }
// #[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
// enum Pattern { enum Pattern {
// Yearly { Yearly {
// doy: u32, doy: u32,
// }, },
// MonthlyOpt { MonthlyOpt {
// dom: u32, dom: u32,
// months: Vec<chrono::Month>, months: Vec<chrono::Month>,
// }, },
// Monthly { Monthly {
// dom: u32, dom: u32,
// }, },
// Weekly { Weekly {
// dow: u32, dow: u32,
// }, },
// Daily, Daily,
// } }
// #[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
// struct Position { struct Position {
// name: String, name: String,
// pattern: Pattern, pattern: Pattern,
// } }
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
}
fn is_done(bytes: &[u8]) -> bool { fn is_done(bytes: &[u8]) -> bool {
return bytes == b"- [x]" // done return bytes == b"- [x]" // done
|| bytes == b"- [n]" // no || bytes == b"- [n]" // no
|| bytes == b"- [i]" // into issue || bytes == b"- [i]" // into issue
|| bytes == b"- [d]" // someone else did it / took "assignment" (spiritual or actually in Gitea) || bytes == b"- [d]" // someone else did it / took "assignment" (spiritual or actually in Gitea)
|| bytes == b"- [r]" // tried unsucessfully, have to retry
|| bytes == b"- [m]" // moved to a later, specified time || bytes == b"- [m]" // moved to a later, specified time
|| bytes == b"- [c]" // i will come back to this when i feel like it, but no need to track it now || bytes == b"- [c]" // i will come back to this when i feel like it, but no need to track it now
|| bytes == b"- [p]" // Made progress, but not done
|| bytes == b"- [q]"; // deprecated / morphed into another todo || bytes == b"- [q]"; // deprecated / morphed into another todo
} }
fn update_notes(notes_path: &Path) { fn update_notes(notes_path: &PathBuf, patterns_path: Option<PathBuf>) {
let mut file = OpenOptions::new() let mut notes_file = OpenOptions::new()
.read(true) .read(true)
.write(true) .write(true)
.open(notes_path) .open(notes_path)
.expect("file should exist"); .expect("file should exist");
let mut bufreader = BufReader::new(&file); let mut notes_bufreader = BufReader::new(&notes_file);
let mut linebuf = String::new(); let mut str_buf1 = String::new();
let mut all_lines = String::new(); let mut str_buf2 = String::new();
let mut latest_date = NaiveDate::MIN; let mut latest_date = NaiveDate::MIN;
let mut lines_to_not_use: Vec<usize> = Vec::new(); let mut lines_to_not_use: Vec<usize> = Vec::new();
// Find todo-type lines // Find todo-type lines
let mut n_lines = 0; let mut n_lines = 0;
while let Ok(n) = bufreader.read_line(&mut linebuf) { while let Ok(n) = notes_bufreader.read_line(&mut str_buf1) {
if n == 0 { if n == 0 {
break; break;
} }
if linebuf == "\n" { if str_buf1 == "\n" {
linebuf.clear(); str_buf1.clear();
continue; continue;
} }
let trimmed_str = linebuf.trim(); let trimmed_str = str_buf1.trim();
let trimmed_bytes = trimmed_str.as_bytes(); let trimmed_bytes = trimmed_str.as_bytes();
dbg!(&linebuf, trimmed_str, trimmed_bytes);
let Some(prefix) = trimmed_bytes.get(0..5) else { let Some(prefix) = trimmed_bytes.get(0..5) else {
continue; continue;
}; };
if prefix == b"- [ ]" || is_done(prefix) { if is_todo(prefix) || is_done(prefix) {
all_lines.push_str(&linebuf); str_buf2.push_str(&str_buf1);
n_lines += 1; n_lines += 1;
} else if trimmed_bytes.get(0) == Some(&b'#') { } else if trimmed_bytes.get(0) == Some(&b'#') {
if let Some(header_cont) = trimmed_str.get(1..) if let Some(header_cont) = trimmed_str.get(1..)
@@ -94,11 +100,11 @@ fn update_notes(notes_path: &Path) {
} }
} }
} }
linebuf.clear(); str_buf1.clear();
} }
let mut todos_hashset: HashSet<&str> = HashSet::new(); let mut todos_hashset: HashSet<&str> = HashSet::new();
let split_lines = all_lines.rsplit("\n"); let split_lines = str_buf2.rsplit("\n");
for (rev_idx, line) in split_lines.enumerate() { for (rev_idx, line) in split_lines.enumerate() {
let org_idx = n_lines - rev_idx; let org_idx = n_lines - rev_idx;
let trimmed_str = line.trim(); let trimmed_str = line.trim();
@@ -115,43 +121,81 @@ fn update_notes(notes_path: &Path) {
} }
// line break as needed // line break as needed
bufreader.seek(std::io::SeekFrom::End(-2)).unwrap_or_else(|e| panic!("file is shorter than two characters. you dont need a todo program for that amount of todos, enjoy your life. Error is: {e}")); notes_bufreader.seek(std::io::SeekFrom::End(-2)).unwrap_or_else(|e| panic!("file is shorter than two characters. you dont need a todo program for that amount of todos, enjoy your life. Error is: {e}"));
let mut last_two_bytes = [0u8, 0u8]; let mut last_two_bytes = [0u8, 0u8];
bufreader.read_exact(&mut last_two_bytes).unwrap_or_else(|e| panic!("file is shorter than two characters. you dont need a todo program for that amount of todos, enjoy your life. Error is: {e}")); notes_bufreader.read_exact(&mut last_two_bytes).unwrap_or_else(|e| panic!("file is shorter than two characters. you dont need a todo program for that amount of todos, enjoy your life. Error is: {e}"));
if last_two_bytes != *b"\n\n" { if last_two_bytes != *b"\n\n" {
write!(linebuf, "\n").unwrap_or_else(|e| panic!("{e}")); write!(str_buf1, "\n").unwrap_or_else(|e| panic!("{e}"));
} }
// Header line // Header line
let today = Local::now().date_naive(); let today = Local::now().date_naive();
if latest_date < today { if latest_date < today {
writeln!(linebuf, "# {}\n", today).unwrap_or_else(|e| panic!("{e}")); writeln!(str_buf1, "# {}\n", today).unwrap_or_else(|e| panic!("{e}"));
} else { } else {
println!("Come back tomorrow!"); // TODO Handle this println!("Come back tomorrow!"); // TODO Handle this
return; return;
} }
// Add patternized todos
if let Some(patterns) = patterns_path {
let patterns_file = OpenOptions::new()
.read(true)
.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");
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}"));
break;
}
date_idx += Duration::days(1);
}
}
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}"));
break;
}
date_idx += Duration::days(1);
}
}
Pattern::Weekly { dow } => {}
Pattern::Daily => {
writeln!(str_buf1, "- [ ] {}", &p.name).unwrap_or_else(|e| panic!("{e}"));
}
}
}
}
// Add todos from prev days
let mut cur = lines_to_not_use.len() - 1; let mut cur = lines_to_not_use.len() - 1;
for (idx, line) in all_lines.split("\n").enumerate() { for (idx, line) in str_buf2.split("\n").enumerate() {
if lines_to_not_use.get(cur).unwrap() == &idx { if lines_to_not_use.get(cur).unwrap() == &idx {
cur = cur.saturating_sub(1); cur = cur.saturating_sub(1);
} else { } else {
writeln!(linebuf, "{line}").unwrap_or_else(|e| panic!("{e}")); writeln!(str_buf1, "{line}").unwrap_or_else(|e| panic!("{e}"));
} }
} }
file.write_all(linebuf.as_bytes()).unwrap_or_else(|e| panic!("Maaan look out for these files. Error is: {e}")); 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}"));
} }
// fn parse_patterns(notes_path: &Path, patterns_path: &Path) {
// // Put all patternized todos into today
// }
fn main() { fn main() {
let args = Args::parse(); let args = Args::parse();
update_notes(&args.notes); update_notes(&args.notes, args.patterns);
// if let Some(patterns_path) = args.patterns {
// parse_patterns(&args.notes, &patterns_path);
// }
} }
// TODO für Patterns alle Tage zwischen heute und letztem Mal ausführen scannen