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

View File

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