refactor file io and task to_string

This commit is contained in:
Andrei Stoica 2023-06-26 09:40:56 -04:00
parent e90ec3e647
commit 85d81c02cc
2 changed files with 56 additions and 53 deletions

View File

@ -2,8 +2,8 @@ mod config;
mod todo; mod todo;
use crate::config::Config; use crate::config::Config;
use crate::todo::{Status as TaskStatus, TaskGroup};
use crate::todo::File as TodoFile; use crate::todo::File as TodoFile;
use crate::todo::{Status as TaskStatus, TaskGroup};
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};
@ -55,11 +55,14 @@ 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(file) if file.date < today => { Ok(todo_file) if todo_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 = parse_todo_file(&file, &arena); let root = {
let contents = load_file(&todo_file);
let root = parse_todo_file(&contents, &arena);
root
};
println!("{:#?}", root); println!("{:#?}", root);
println!("======================================================="); println!("=======================================================");
@ -78,38 +81,47 @@ fn main() {
}) })
.collect(); .collect();
let new_file = write_file(&data_dir, &today, &data); // let new_file = write_file(&data_dir, &today, &data);
Some(new_file) let content = generate_file_content(&data, &today);
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 new_file = write_file(&data_dir, &today, &data); let content = generate_file_content(&data, &today);
let file_path = get_filepath(&data_dir, &today);
Some(new_file) write_file(&file_path, &content);
file_path
} }
Ok(file) => { Ok(todo_file) => {
println!("Today's file was created"); println!("Today's file was created");
Some(file.file.path()) todo_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 write_file(data_dir: &PathBuf, date: &NaiveDate, data: &Vec<TaskGroup>) -> PathBuf { 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 {
let mut content = format!( let mut content = format!(
"# Today's tasks {}-{:02}-{:02}\n", "# Today's tasks {}-{:02}-{:02}\n",
date.year(), date.year(),
@ -119,22 +131,30 @@ fn write_file(data_dir: &PathBuf, date: &NaiveDate, data: &Vec<TaskGroup>) -> Pa
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()));
let file_name = format!( content
"{}-{: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 parse_todo_file<'a>(file: &TodoFile, arena: &'a Arena<AstNode<'a>>) -> &'a AstNode<'a> { 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> {
let options = &ComrakOptions { let options = &ComrakOptions {
extension: ComrakExtensionOptions { extension: ComrakExtensionOptions {
tasklist: true, tasklist: true,
@ -146,17 +166,6 @@ fn parse_todo_file<'a>(file: &TodoFile, arena: &'a Arena<AstNode<'a>>) -> &'a As
}, },
..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,23 +76,17 @@ impl ToString for Task {
}; };
let subtasks = if let Some(subtasks) = &self.subtasks { let subtasks = if let Some(subtasks) = &self.subtasks {
let mut text = subtasks let text = subtasks
.iter() .iter()
.map(|task| task.to_string()) .map(|task| task.to_string())
.collect::<Vec<_>>() .collect::<Vec<_>>()
.join("\n"); .join("\n");
text.insert(0, '\n'); format!("\n{}", text).trim_end().replace("\n", "\n ")
text.trim_end().to_string()
} else { } else {
"".into() "".into()
}; };
format!( format!("- [{}] {}{}\n", ch, self.text.trim(), subtasks)
"- [{}] {}{}\n",
ch,
self.text.trim(),
subtasks.replace("\n", "\n ")
)
} }
} }