Compare commits

..

No commits in common. "f6e9bac0e3fc312e40fd555a68486faf3b8699bd" and "6958375f6c476ed011af76e70599408aaf1bd23e" have entirely different histories.

2 changed files with 53 additions and 56 deletions

View File

@ -2,8 +2,8 @@ mod config;
mod todo;
use crate::config::Config;
use crate::todo::File as TodoFile;
use crate::todo::{Status as TaskStatus, TaskGroup};
use crate::todo::File as TodoFile;
use chrono::naive::NaiveDate;
use chrono::{Datelike, Local};
use comrak::nodes::{AstNode, NodeValue};
@ -65,14 +65,11 @@ fn main() {
let now = Local::now();
let today = NaiveDate::from_ymd_opt(now.year(), now.month(), now.day()).unwrap();
let current_file = match latest_file {
Ok(todo_file) if todo_file.date < today => {
Ok(file) if file.date < today => {
println!("Today's file does not exist, creating");
let arena = Arena::new();
let root = {
let contents = load_file(&todo_file);
let root = parse_todo_file(&contents, &arena);
root
};
let root = parse_todo_file(&file, &arena);
println!("{:#?}", root);
println!("=======================================================");
@ -91,47 +88,38 @@ fn main() {
})
.collect();
// let new_file = write_file(&data_dir, &today, &data);
let new_file = write_file(&data_dir, &today, &data);
let content = generate_file_content(&data, &today);
let file_path = get_filepath(&data_dir, &today);
write_file(&file_path, &content);
file_path
Some(new_file)
}
Err(_) => {
println!("No files in dir: {:}", cfg.notes_dir.unwrap());
let sections = &cfg.sections.unwrap();
let data = sections
.iter()
.map(|sec| TaskGroup::empty(sec.clone(), 2))
.collect();
let content = generate_file_content(&data, &today);
let file_path = get_filepath(&data_dir, &today);
write_file(&file_path, &content);
file_path
let new_file = write_file(&data_dir, &today, &data);
Some(new_file)
}
Ok(todo_file) => {
Ok(file) => {
println!("Today's file was created");
todo_file.file.path()
Some(file.file.path())
}
};
if let Some(file) = current_file {
Command::new(cfg.editor.expect("Could not resolve editor from config"))
.args([current_file])
.args([file])
.status()
.expect(format!("failed to launch editor {}", "vim").as_str());
};
}
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 {
fn write_file(data_dir: &PathBuf, date: &NaiveDate, data: &Vec<TaskGroup>) -> PathBuf {
let mut content = format!(
"# Today's tasks {}-{:02}-{:02}\n",
date.year(),
@ -141,30 +129,22 @@ fn generate_file_content(data: &Vec<TaskGroup>, date: &NaiveDate) -> String {
data.iter()
.for_each(|task_group| content.push_str(format!("\n{}", task_group.to_string()).as_str()));
content
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);
let mut file = File::create(&file_path).expect("Could not open today's file: {today_file_path}");
write!(file, "{}", content).expect("Could not write to file: {today_file_path}");
file_path
}
fn write_file(path: &PathBuf, content: &String) {
let mut new_file =
File::create(&path).expect("Could not open today's file: {today_file_path}");
write!(new_file, "{}", content).expect("Could not write to file: {today_file_path}");
}
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()
}
fn parse_todo_file<'a>(contents: &String, arena: &'a Arena<AstNode<'a>>) -> &'a AstNode<'a> {
fn parse_todo_file<'a>(file: &TodoFile, arena: &'a Arena<AstNode<'a>>) -> &'a AstNode<'a> {
let options = &ComrakOptions {
extension: ComrakExtensionOptions {
tasklist: true,
@ -176,6 +156,17 @@ fn parse_todo_file<'a>(contents: &String, arena: &'a Arena<AstNode<'a>>) -> &'a
},
..ComrakOptions::default()
};
let contents_utf8 = read(file.file.path())
.expect(format!("Could not read file {}", file.file.path().to_string_lossy()).as_str());
let contents = str::from_utf8(&contents_utf8).expect(
format!(
"failed to convert contents of file to string: {}",
file.file.path().to_string_lossy()
)
.as_str(),
);
parse_document(arena, contents, options)
}

View File

@ -76,17 +76,23 @@ impl ToString for Task {
};
let subtasks = if let Some(subtasks) = &self.subtasks {
let text = subtasks
let mut text = subtasks
.iter()
.map(|task| task.to_string())
.collect::<Vec<_>>()
.join("\n");
format!("\n{}", text).trim_end().replace("\n", "\n ")
text.insert(0, '\n');
text.trim_end().to_string()
} else {
"".into()
};
format!("- [{}] {}{}\n", ch, self.text.trim(), subtasks)
format!(
"- [{}] {}{}\n",
ch,
self.text.trim(),
subtasks.replace("\n", "\n ")
)
}
}