This commit is contained in:
2026-01-23 20:03:48 +01:00
parent 1967a3e1d6
commit fee91edb1a
3 changed files with 128 additions and 88 deletions

View File

@@ -1,32 +0,0 @@
i: into issue
d: someone else did it / took "assignment" (spiritual or actually in Gitea)
r: tried unsucessfully, have to retry
m: moved to a later, specified time
c: i will come back to this when i feel like it, but no need to track it now
p: Made progress, but not done
n: no (catchall for not doing it)
# 2026-01-20
- [ ] Test 1
- [ ] Test 2
- [x] Test 3
- [ ] Test 4
- [ ] Test 5
# 2026-01-21
- [x] Test 1
- [ ] Test 2
- [ ] Test 6
- [ ] Test 7
# 2026-01-22
- [ ] Test 1
- [ ] Test 2
- [ ] Test 4
- [ ] Test 5
- [ ] Test 6
- [ ] Test 7

View File

@@ -1,8 +1,12 @@
use chrono::{self, NaiveDate, Local};
use chrono::{self, Local, NaiveDate};
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::{
collections::HashSet, fmt::Write as _, fs::{File, OpenOptions}, io::{BufRead, BufReader, Read, Seek, Write as _}, path::{Path, PathBuf}
collections::HashSet,
fmt::Write as _,
fs::OpenOptions,
io::{BufRead, BufReader, Read, Seek, Write as _},
path::{Path, PathBuf},
};
#[derive(Parser, Debug)]
@@ -12,52 +16,77 @@ struct Args {
patterns: Option<PathBuf>,
}
#[derive(Serialize, Deserialize, Debug)]
enum Pattern {
Yearly {
doy: u32,
},
MonthlyOpt {
dom: u32,
months: Vec<chrono::Month>,
},
Monthly {
dom: u32,
},
Weekly {
dow: u32,
},
Daily,
}
// #[derive(Serialize, Deserialize, Debug)]
// enum Pattern {
// Yearly {
// doy: u32,
// },
// MonthlyOpt {
// dom: u32,
// months: Vec<chrono::Month>,
// },
// Monthly {
// dom: u32,
// },
// Weekly {
// dow: u32,
// },
// Daily,
// }
#[derive(Serialize, Deserialize, Debug)]
struct Position {
name: String,
pattern: Pattern,
// #[derive(Serialize, Deserialize, Debug)]
// struct Position {
// name: String,
// pattern: Pattern,
// }
fn is_done(bytes: &[u8]) -> bool {
return bytes == b"- [x]" // done
|| bytes == b"- [n]" // no
|| bytes == b"- [i]" // into issue
|| 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"- [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
}
fn update_notes(notes_path: &Path) {
let mut file = OpenOptions::new().read(true).write(true).open(notes_path).expect("file should exist");
let mut file = OpenOptions::new()
.read(true)
.write(true)
.open(notes_path)
.expect("file should exist");
let mut bufreader = BufReader::new(&file);
let mut linebuf = String::new();
let mut all_lines = String::new();
let mut latest_date = NaiveDate::MIN;
let mut lines_with_duplicates: Vec<usize> = Vec::new();
let mut lines_to_not_use: Vec<usize> = Vec::new();
// Find todo-type lines
let mut n_lines = 0;
while let Ok(n) = bufreader.read_line(&mut linebuf) {
if n == 0 {
break;
}
if linebuf == "\n" {
linebuf.clear();
continue;
}
let trimmed_str = linebuf.trim();
let trimmed_bytes = trimmed_str.as_bytes();
let Some(sub) = trimmed_bytes.get(0..5) else { continue; };
println!("cur line is {:?}. Sub is {:?}", trimmed_str, sub);
if sub == b"- [ ]" {
let Some(prefix) = trimmed_bytes.get(0..5) else {
continue;
};
if prefix == b"- [ ]" || is_done(prefix) {
all_lines.push_str(&linebuf);
n_lines += 1;
} else if trimmed_bytes.get(0) == Some(&b'#') {
if let Ok(date) = NaiveDate::parse_from_str(trimmed_str.get(1..).unwrap(), "%Y-%m-%d") {
if let Some(header_cont) = trimmed_str.get(1..)
&& let Ok(date) = NaiveDate::parse_from_str(header_cont, "%Y-%m-%d")
{
if date > latest_date {
latest_date = date;
}
@@ -68,53 +97,66 @@ fn update_notes(notes_path: &Path) {
// Find duplicates
let mut todos_hashset: HashSet<&str> = HashSet::new();
for (idx, line) in all_lines.split("\n").enumerate() {
if !todos_hashset.insert(line) {
lines_with_duplicates.push(idx);
let split_lines = all_lines.rsplit("\n");
for (rev_idx, line) in split_lines.enumerate() {
let org_idx = n_lines - rev_idx;
let trimmed_str = line.trim();
let trimmed_bytes = trimmed_str.as_bytes();
let Some(prefix) = trimmed_bytes.get(0..5) else {
continue;
};
let Some(todotext) = trimmed_str.get(6..) else {
continue;
};
if !todos_hashset.insert(todotext) {
// false if dupl
lines_to_not_use.push(org_idx);
} else {
if is_done(prefix) {
lines_to_not_use.push(org_idx);
}
}
}
// Prepare buffer
let res1 = bufreader.seek(std::io::SeekFrom::End(-2));
// 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}"));
let mut last_two_bytes = [0u8, 0u8];
let res2 = bufreader.read_exact(&mut last_two_bytes);
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" {
write!(linebuf, "\n");
write!(linebuf, "\n").unwrap_or_else(|e| panic!("{e}"));
}
// Header line
let today = Local::now().date_naive();
if latest_date < today {
writeln!(linebuf, "# {}", today);
writeln!(linebuf, "# {}\n", today).unwrap_or_else(|e| panic!("{e}"));
} else {
println!("Come back tomorrow!"); // TODO Handle this
return;
}
let mut cur = 0;
let mut cur = lines_to_not_use.len() - 1;
for (idx, line) in all_lines.split("\n").enumerate() {
if cur < lines_with_duplicates.len() && lines_with_duplicates[cur] == idx {
cur += 1;
if cur > 0 && lines_to_not_use.get(cur).unwrap() == &idx {
cur -= 1;
} else {
writeln!(linebuf, "{line}");
writeln!(linebuf, "{line}").unwrap_or_else(|e| panic!("{e}"));
}
}
println!("{:?}", file.write_all(linebuf.as_bytes()));
// TODO: Handle case if today already exists
// TODO: Dont put once-open but now closed todos in
// Opt: Create new date for today
// Put all open todos into today
file.write_all(linebuf.as_bytes()).unwrap_or_else(|e| panic!("Maaan look out for these files. Error is: {e}"));
// dbg!(&linebuf);
}
fn parse_patterns(notes_path: &Path, patterns_path: &Path) {
// Put all patternized todos into today
}
// fn parse_patterns(notes_path: &Path, patterns_path: &Path) {
// // Put all patternized todos into today
// }
fn main() {
let args = Args::parse();
update_notes(&args.notes);
if let Some(patterns_path) = args.patterns {
parse_patterns(&args.notes, &patterns_path);
}
// if let Some(patterns_path) = args.patterns {
// parse_patterns(&args.notes, &patterns_path);
// }
}