rusty-tasks/src/main.rs

149 lines
4.8 KiB
Rust
Raw Normal View History

2023-06-15 08:57:19 -04:00
mod todo_file;
use crate::todo_file::TodoFile;
2023-06-12 23:46:40 -04:00
use chrono::naive::NaiveDate;
2023-06-13 07:59:09 -04:00
use chrono::{Datelike, Local};
2023-06-15 20:54:33 -04:00
use comrak::nodes::{AstNode, NodeValue};
2023-06-15 12:14:36 -04:00
use comrak::{
format_commonmark, parse_document, Arena, ComrakExtensionOptions, ComrakOptions,
ComrakParseOptions,
};
2023-06-15 20:54:33 -04:00
use std::borrow::Borrow;
2023-06-12 23:46:40 -04:00
use std::env;
2023-06-15 12:14:36 -04:00
use std::fs::{copy, read, read_dir, File};
use std::io::Write;
2023-06-13 07:59:09 -04:00
use std::path::{Path, PathBuf};
2023-06-13 08:10:18 -04:00
use std::process::Command;
2023-06-15 12:14:36 -04:00
use std::str;
2023-06-12 23:46:40 -04:00
//TODO handle unwraps and errors more uniformly
2023-06-13 08:30:59 -04:00
//TODO clean up verbose printing
2023-06-15 12:14:36 -04:00
//TODO create config for passing options to different files
2023-06-12 23:46:40 -04:00
fn main() {
let data_dir = get_data_dir("notes");
println!("{}", data_dir.to_str().unwrap());
2023-06-13 08:41:37 -04:00
2023-06-13 08:10:18 -04:00
let latest_file =
get_latest_file(&data_dir).expect(format!("Could not find any notes files").as_str());
2023-06-12 23:46:40 -04:00
println!("Latest file: {:?}", latest_file);
2023-06-13 08:41:37 -04:00
let mut editor = Command::new(get_editor("vim".to_string()));
2023-06-12 23:46:40 -04:00
let now = Local::now();
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day());
match today {
2023-06-13 08:30:59 -04:00
Some(today) if latest_file.date < today => {
println!("Today's file does not exist, creating");
2023-06-13 08:41:37 -04:00
let today_file_name = format!(
"{}-{:02}-{:02}.md",
today.year(),
today.month(),
today.day()
);
2023-06-13 08:30:59 -04:00
let mut today_file_path = data_dir.clone();
today_file_path.push(today_file_name);
2023-06-15 12:14:36 -04:00
let arena = Arena::new();
let root = parse_todo_file(&latest_file, &arena);
2023-06-15 20:54:33 -04:00
cleanup_sections(&root, &["Hello world!", "Hi there"], 2);
2023-06-15 12:14:36 -04:00
2023-06-15 20:54:33 -04:00
let mut new_doc = vec![];
format_commonmark(root, &ComrakOptions::default(), &mut new_doc).unwrap();
2023-06-15 12:14:36 -04:00
let mut new_file = File::create(today_file_path.clone()).unwrap();
2023-06-15 20:54:33 -04:00
new_file.write_all(&new_doc).unwrap();
2023-06-13 08:41:37 -04:00
editor
.args([today_file_path])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
2023-06-13 08:30:59 -04:00
}
Some(_) => {
2023-06-13 08:10:18 -04:00
println!("Todays file was created");
2023-06-13 08:41:37 -04:00
editor
2023-06-13 08:10:18 -04:00
.args([latest_file.file.path()])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
}
2023-06-13 08:30:59 -04:00
_ => println!("Could not get today's date"),
2023-06-13 07:59:09 -04:00
}
}
2023-06-15 12:14:36 -04:00
fn parse_todo_file<'a>(file: &TodoFile, arena: &'a Arena<AstNode<'a>>) -> &'a AstNode<'a> {
let options = &ComrakOptions {
extension: ComrakExtensionOptions {
tasklist: true,
..ComrakExtensionOptions::default()
},
parse: ComrakParseOptions {
relaxed_tasklist_matching: true,
..ComrakParseOptions::default()
},
..ComrakOptions::default()
};
let contents = read(file.file.path()).unwrap();
parse_document(arena, str::from_utf8(&contents).unwrap(), options)
}
2023-06-15 20:54:33 -04:00
fn cleanup_sections<'a>(
root: &'a AstNode<'a>,
sections: &[&str],
target_level: u8,
) -> &'a AstNode<'a> {
for node in root.children() {
match &node.data.borrow().value {
NodeValue::Heading(heading) if heading.level == target_level => {
if let NodeValue::Text(title) =
&node.first_child().borrow().unwrap().data.borrow().value
{
if !sections.contains(&title.as_str()) {
let level = heading.level;
let mut following = node.following_siblings();
following.next(); // Skip self
for node in following {
// remove everthing under this heading
match &node.data.borrow().value {
NodeValue::Heading(heading) if heading.level <= level => break,
_ => node.detach(),
}
}
node.detach(); // remove heading as well
};
};
}
_ => (),
}
}
root
}
2023-06-13 08:41:37 -04:00
fn get_editor(fallback: String) -> String {
match env::var("EDITOR") {
Ok(editor) => editor,
_ => fallback,
}
}
2023-06-12 23:46:40 -04:00
fn get_data_dir(dir_name: &str) -> PathBuf {
let mut dir = if let Ok(home) = env::var("HOME") {
let mut x = PathBuf::new();
x.push(home);
x
} else {
env::current_dir().expect("PWD environment variable not set")
};
dir = dir.join(dir_name);
dir
}
fn get_latest_file(dir: &Path) -> Result<TodoFile, String> {
2023-06-13 08:30:59 -04:00
let dir = read_dir(dir).expect(format!("Could not find notes folder: {:?}", dir).as_str());
2023-06-12 23:46:40 -04:00
dir.filter_map(|f| f.ok())
.filter_map(|file| TodoFile::try_from(file).ok())
.reduce(|a, b| TodoFile::latest_file(a, b))
.ok_or("Could not reduce items".to_string())
}