2024-02-22 01:34:11 -05:00
|
|
|
mod cli;
|
2023-06-22 13:44:06 -04:00
|
|
|
mod config;
|
2023-06-23 09:41:27 -04:00
|
|
|
mod todo;
|
2023-06-15 08:57:19 -04:00
|
|
|
|
2024-02-22 01:34:11 -05:00
|
|
|
use crate::cli::Args;
|
|
|
|
|
use clap::Parser;
|
|
|
|
|
|
2024-02-28 15:12:19 -05:00
|
|
|
use crate::config::{Config, ConfigError};
|
2023-06-25 06:58:08 -04:00
|
|
|
use crate::todo::File as TodoFile;
|
2023-06-26 09:40:56 -04:00
|
|
|
use crate::todo::{Status as TaskStatus, TaskGroup};
|
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-23 09:41:27 -04:00
|
|
|
use comrak::nodes::{AstNode, NodeValue};
|
|
|
|
|
use comrak::{parse_document, Arena};
|
2023-06-22 13:44:06 -04:00
|
|
|
use comrak::{ComrakExtensionOptions, ComrakOptions, ComrakParseOptions};
|
2023-06-23 09:41:27 -04:00
|
|
|
use std::collections::HashMap;
|
2023-10-06 12:20:36 -04:00
|
|
|
use std::fs::{create_dir_all, metadata, read, read_dir, File};
|
2024-02-22 01:49:57 -05:00
|
|
|
use std::io::{self, 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;
|
2024-02-22 01:34:11 -05:00
|
|
|
use std::{env, str};
|
2023-06-12 23:46:40 -04:00
|
|
|
|
|
|
|
|
//TODO handle unwraps and errors more uniformly
|
2023-06-23 10:47:43 -04:00
|
|
|
//TODO refactor creating new file
|
2023-06-13 08:30:59 -04:00
|
|
|
//TODO clean up verbose printing
|
2023-06-23 09:41:27 -04:00
|
|
|
//TODO create custom errors for better error handling
|
2024-02-22 01:34:11 -05:00
|
|
|
//TODO Default path for note_dir should start with curent path not home
|
|
|
|
|
|
2024-02-28 15:12:19 -05:00
|
|
|
// Nested errors look bad, code smell?
|
2024-02-22 01:34:11 -05:00
|
|
|
#[derive(Debug)]
|
|
|
|
|
enum ExitError {
|
2024-02-28 15:12:19 -05:00
|
|
|
ConfigError(ConfigError),
|
2024-02-22 01:49:57 -05:00
|
|
|
IOError(String, io::Error),
|
2024-02-22 01:34:11 -05:00
|
|
|
}
|
2023-06-12 23:46:40 -04:00
|
|
|
|
2024-02-22 01:34:11 -05:00
|
|
|
fn main() -> Result<(), ExitError> {
|
2024-02-22 02:16:58 -05:00
|
|
|
let args = Args::parse();
|
|
|
|
|
|
2024-02-28 15:12:19 -05:00
|
|
|
let expected_cfg_files = match Config::expected_locations() {
|
|
|
|
|
Err(e) => return Err(ExitError::ConfigError(e)),
|
|
|
|
|
Ok(cfg_files) => cfg_files,
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-22 13:44:06 -04:00
|
|
|
let cfg_files: Vec<&Path> = expected_cfg_files
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|file| Path::new(file))
|
|
|
|
|
.filter(|file| file.exists())
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
if cfg_files.len() <= 0 {
|
2024-02-28 15:12:19 -05:00
|
|
|
match Config::write_default(expected_cfg_files[0].to_str()?) {
|
|
|
|
|
Err(e) => return Err(ExitError::ConfigError(e)),
|
|
|
|
|
_ => (),
|
2023-06-22 13:44:06 -04:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-06 11:46:21 -04:00
|
|
|
|
2024-02-22 02:16:58 -05:00
|
|
|
let cfg_file = match args.config {
|
|
|
|
|
Some(file) => file,
|
|
|
|
|
None => match cfg_files.last() {
|
|
|
|
|
None => expected_cfg_files[0].to_string_lossy().to_string(),
|
|
|
|
|
Some(file) => file.to_string_lossy().to_string(),
|
|
|
|
|
},
|
2023-10-06 11:46:21 -04:00
|
|
|
};
|
|
|
|
|
|
2024-02-22 02:16:58 -05:00
|
|
|
if args.current_config {
|
|
|
|
|
println!("{}", &cfg_file);
|
|
|
|
|
return Ok(());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let cfg = Config::load(&cfg_file).unwrap();
|
2023-06-22 13:44:06 -04:00
|
|
|
|
|
|
|
|
println!("{:#?}", cfg);
|
2024-02-22 01:49:57 -05:00
|
|
|
let data_dir = match &cfg.notes_dir {
|
|
|
|
|
Some(dir) => get_data_dir(dir),
|
|
|
|
|
_ => {
|
2024-02-28 15:12:19 -05:00
|
|
|
return Err(ExitError::ConfigError(ConfigError::IOError(
|
|
|
|
|
"Could not get notes dir from config",
|
|
|
|
|
)))
|
2024-02-22 01:49:57 -05:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2023-10-06 11:46:21 -04:00
|
|
|
if !metadata(&data_dir).is_ok() {
|
2024-02-22 01:49:57 -05:00
|
|
|
match create_dir_all(&data_dir) {
|
|
|
|
|
Err(e) => {
|
|
|
|
|
return Err(ExitError::IOError(
|
|
|
|
|
format!(
|
|
|
|
|
"Could not create defult directory: {}",
|
|
|
|
|
&data_dir.to_str().unwrap(),
|
|
|
|
|
),
|
|
|
|
|
e,
|
|
|
|
|
))
|
|
|
|
|
}
|
|
|
|
|
_ => (),
|
2023-10-06 12:20:36 -04:00
|
|
|
};
|
2023-10-06 11:46:21 -04:00
|
|
|
}
|
2023-06-23 09:41:27 -04:00
|
|
|
println!("dir = {}", data_dir.to_str().unwrap());
|
2023-06-13 08:41:37 -04:00
|
|
|
|
2023-06-24 08:44:07 -04:00
|
|
|
let latest_file = get_latest_file(&data_dir);
|
2023-06-12 23:46:40 -04:00
|
|
|
println!("Latest file: {:?}", latest_file);
|
2023-06-13 08:41:37 -04:00
|
|
|
|
2023-06-12 23:46:40 -04:00
|
|
|
let now = Local::now();
|
2023-06-23 10:47:43 -04:00
|
|
|
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day()).unwrap();
|
|
|
|
|
let current_file = match latest_file {
|
2023-06-26 09:40:56 -04:00
|
|
|
Ok(todo_file) if todo_file.date < today => {
|
2023-06-13 08:30:59 -04:00
|
|
|
println!("Today's file does not exist, creating");
|
2023-06-15 12:14:36 -04:00
|
|
|
let arena = Arena::new();
|
2023-06-26 09:40:56 -04:00
|
|
|
let root = {
|
|
|
|
|
let contents = load_file(&todo_file);
|
|
|
|
|
let root = parse_todo_file(&contents, &arena);
|
|
|
|
|
root
|
|
|
|
|
};
|
2023-06-22 16:54:00 -04:00
|
|
|
|
2023-06-25 12:25:59 -04:00
|
|
|
println!("{:#?}", root);
|
|
|
|
|
println!("=======================================================");
|
2023-06-22 16:54:00 -04:00
|
|
|
|
2023-06-23 09:41:27 -04:00
|
|
|
let sections = &cfg.sections.unwrap();
|
|
|
|
|
let groups = extract_secitons(root, sections);
|
|
|
|
|
println!("{:#?}", groups);
|
2023-06-15 12:14:36 -04:00
|
|
|
|
2023-06-23 09:41:27 -04:00
|
|
|
let level = groups.values().map(|group| group.level).min().unwrap_or(2);
|
|
|
|
|
|
2023-06-24 08:44:07 -04:00
|
|
|
let data = sections
|
2023-06-23 09:41:27 -04:00
|
|
|
.iter()
|
|
|
|
|
.map(|section| match groups.get(section) {
|
|
|
|
|
Some(group) => group.clone(),
|
|
|
|
|
None => TaskGroup::empty(section.to_string(), level),
|
|
|
|
|
})
|
2023-06-24 08:44:07 -04:00
|
|
|
.collect();
|
2023-06-23 09:41:27 -04:00
|
|
|
|
2023-06-26 09:40:56 -04:00
|
|
|
let content = generate_file_content(&data, &today);
|
|
|
|
|
let file_path = get_filepath(&data_dir, &today);
|
|
|
|
|
write_file(&file_path, &content);
|
|
|
|
|
file_path
|
2023-06-22 16:54:00 -04:00
|
|
|
}
|
2023-06-23 10:47:43 -04:00
|
|
|
Err(_) => {
|
|
|
|
|
println!("No files in dir: {:}", cfg.notes_dir.unwrap());
|
|
|
|
|
let sections = &cfg.sections.unwrap();
|
2023-06-24 08:44:07 -04:00
|
|
|
let data = sections
|
2023-06-23 10:47:43 -04:00
|
|
|
.iter()
|
2023-06-24 08:44:07 -04:00
|
|
|
.map(|sec| TaskGroup::empty(sec.clone(), 2))
|
2024-02-28 15:12:19 -05:00
|
|
|
.collect();
|
|
|
|
|
let content = generate_file_content(&data, &today);
|
2023-06-26 09:40:56 -04:00
|
|
|
let file_path = get_filepath(&data_dir, &today);
|
|
|
|
|
write_file(&file_path, &content);
|
|
|
|
|
file_path
|
2023-06-24 08:44:07 -04:00
|
|
|
}
|
2023-06-26 09:40:56 -04:00
|
|
|
Ok(todo_file) => {
|
2023-06-24 08:44:07 -04:00
|
|
|
println!("Today's file was created");
|
2023-06-26 09:40:56 -04:00
|
|
|
todo_file.file.path()
|
2023-06-13 08:10:18 -04:00
|
|
|
}
|
2023-06-22 13:44:06 -04:00
|
|
|
};
|
|
|
|
|
|
2023-06-26 09:40:56 -04:00
|
|
|
Command::new(cfg.editor.expect("Could not resolve editor from config"))
|
|
|
|
|
.args([current_file])
|
|
|
|
|
.status()
|
|
|
|
|
.expect(format!("failed to launch editor {}", "vim").as_str());
|
2024-02-22 01:34:11 -05:00
|
|
|
|
|
|
|
|
Ok(())
|
2023-06-13 07:59:09 -04:00
|
|
|
}
|
2023-06-24 08:44:07 -04:00
|
|
|
|
2023-06-26 09:40:56 -04:00
|
|
|
fn get_filepath(data_dir: &PathBuf, date: &NaiveDate) -> PathBuf {
|
|
|
|
|
let file_name = format!("{}-{:02}-{:02}.md", date.year(), date.month(), date.day());
|
|
|
|
|
let mut file_path = data_dir.clone();
|
|
|
|
|
file_path.push(file_name);
|
|
|
|
|
|
|
|
|
|
file_path
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn generate_file_content(data: &Vec<TaskGroup>, date: &NaiveDate) -> String {
|
2023-06-24 08:44:07 -04:00
|
|
|
let mut content = format!(
|
|
|
|
|
"# Today's tasks {}-{:02}-{:02}\n",
|
|
|
|
|
date.year(),
|
|
|
|
|
date.month(),
|
|
|
|
|
date.day()
|
|
|
|
|
);
|
|
|
|
|
data.iter()
|
|
|
|
|
.for_each(|task_group| content.push_str(format!("\n{}", task_group.to_string()).as_str()));
|
|
|
|
|
|
2023-06-26 09:40:56 -04:00
|
|
|
content
|
|
|
|
|
}
|
2023-06-24 08:44:07 -04:00
|
|
|
|
2023-06-26 09:40:56 -04:00
|
|
|
fn write_file(path: &PathBuf, content: &String) {
|
2023-10-06 12:20:36 -04:00
|
|
|
let mut new_file = File::create(&path).expect("Could not open today's file: {today_file_path}");
|
2023-06-26 09:40:56 -04:00
|
|
|
write!(new_file, "{}", content).expect("Could not write to file: {today_file_path}");
|
|
|
|
|
}
|
2023-06-24 08:44:07 -04:00
|
|
|
|
2023-06-26 09:40:56 -04:00
|
|
|
fn load_file(file: &TodoFile) -> String {
|
|
|
|
|
let contents_utf8 = read(file.file.path())
|
|
|
|
|
.expect(format!("Could not read file {}", file.file.path().to_string_lossy()).as_str());
|
|
|
|
|
str::from_utf8(&contents_utf8)
|
|
|
|
|
.expect(
|
|
|
|
|
format!(
|
|
|
|
|
"failed to convert contents of file to string: {}",
|
|
|
|
|
file.file.path().to_string_lossy()
|
|
|
|
|
)
|
|
|
|
|
.as_str(),
|
|
|
|
|
)
|
|
|
|
|
.to_string()
|
2023-06-24 08:44:07 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-26 09:40:56 -04:00
|
|
|
fn parse_todo_file<'a>(contents: &String, arena: &'a Arena<AstNode<'a>>) -> &'a AstNode<'a> {
|
2023-06-15 12:14:36 -04:00
|
|
|
let options = &ComrakOptions {
|
|
|
|
|
extension: ComrakExtensionOptions {
|
|
|
|
|
tasklist: true,
|
|
|
|
|
..ComrakExtensionOptions::default()
|
|
|
|
|
},
|
|
|
|
|
parse: ComrakParseOptions {
|
|
|
|
|
relaxed_tasklist_matching: true,
|
|
|
|
|
..ComrakParseOptions::default()
|
|
|
|
|
},
|
|
|
|
|
..ComrakOptions::default()
|
|
|
|
|
};
|
2023-06-16 07:53:59 -04:00
|
|
|
parse_document(arena, contents, options)
|
2023-06-15 12:14:36 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-23 09:41:27 -04:00
|
|
|
fn extract_secitons<'a>(
|
|
|
|
|
root: &'a AstNode<'a>,
|
|
|
|
|
sections: &Vec<String>,
|
|
|
|
|
) -> HashMap<String, TaskGroup> {
|
|
|
|
|
let mut groups: HashMap<String, TaskGroup> = HashMap::new();
|
2023-06-22 16:54:00 -04:00
|
|
|
for node in root.reverse_children() {
|
2023-06-16 07:53:59 -04:00
|
|
|
let node_ref = &node.data.borrow();
|
|
|
|
|
if let NodeValue::Heading(heading) = node_ref.value {
|
2023-06-23 09:41:27 -04:00
|
|
|
if heading.level < 2 {
|
2023-06-16 07:53:59 -04:00
|
|
|
continue;
|
2023-06-15 20:54:33 -04:00
|
|
|
}
|
2023-06-22 13:44:06 -04:00
|
|
|
|
2023-06-16 07:53:59 -04:00
|
|
|
let first_child_ref = &node.first_child();
|
2024-02-22 02:16:58 -05:00
|
|
|
let first_child = if let Some(child) = first_child_ref {
|
2023-06-16 07:53:59 -04:00
|
|
|
child
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let data_ref = &first_child.data.borrow();
|
|
|
|
|
let title = if let NodeValue::Text(value) = &data_ref.value {
|
|
|
|
|
value
|
|
|
|
|
} else {
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
2023-06-23 09:41:27 -04:00
|
|
|
println!("Attempting to parse {}", title);
|
|
|
|
|
if sections.iter().any(|section| section.eq(title)) {
|
|
|
|
|
if let Ok(mut group) = TaskGroup::try_from(node) {
|
|
|
|
|
group.tasks = group
|
|
|
|
|
.tasks
|
|
|
|
|
.into_iter()
|
|
|
|
|
.filter(|task| !matches!(task.status, TaskStatus::Done(_)))
|
|
|
|
|
.collect();
|
|
|
|
|
groups.insert(title.to_string(), group);
|
2023-06-16 07:53:59 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-06-15 20:54:33 -04:00
|
|
|
}
|
2023-06-23 09:41:27 -04:00
|
|
|
groups
|
2023-06-15 20:54:33 -04:00
|
|
|
}
|
|
|
|
|
|
2023-06-12 23:46:40 -04:00
|
|
|
fn get_data_dir(dir_name: &str) -> PathBuf {
|
2023-06-16 07:53:59 -04:00
|
|
|
let mut dir = match env::var("HOME") {
|
|
|
|
|
Ok(home) => {
|
|
|
|
|
let mut x = PathBuf::new();
|
|
|
|
|
x.push(home);
|
|
|
|
|
x
|
|
|
|
|
}
|
|
|
|
|
_ => env::current_dir().expect("PWD environment variable not set"),
|
2023-06-12 23:46:40 -04:00
|
|
|
};
|
|
|
|
|
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())
|
|
|
|
|
}
|