refactor file creation
This commit is contained in:
parent
02ed04fad5
commit
7a6ed10e23
108
src/main.rs
108
src/main.rs
|
|
@ -43,11 +43,14 @@ fn main() {
|
||||||
let cfg = Config::load(cfg_files.last().unwrap().to_str().unwrap()).unwrap();
|
let cfg = Config::load(cfg_files.last().unwrap().to_str().unwrap()).unwrap();
|
||||||
|
|
||||||
println!("{:#?}", cfg);
|
println!("{:#?}", cfg);
|
||||||
let data_dir = get_data_dir(&cfg.notes_dir.clone().expect("Could not get notes dir from config"));
|
let data_dir = get_data_dir(
|
||||||
|
&cfg.notes_dir
|
||||||
|
.clone()
|
||||||
|
.expect("Could not get notes dir from config"),
|
||||||
|
);
|
||||||
println!("dir = {}", data_dir.to_str().unwrap());
|
println!("dir = {}", data_dir.to_str().unwrap());
|
||||||
|
|
||||||
let latest_file =
|
let latest_file = get_latest_file(&data_dir);
|
||||||
get_latest_file(&data_dir);
|
|
||||||
println!("Latest file: {:?}", latest_file);
|
println!("Latest file: {:?}", latest_file);
|
||||||
|
|
||||||
let now = Local::now();
|
let now = Local::now();
|
||||||
|
|
@ -55,19 +58,11 @@ fn main() {
|
||||||
let current_file = match latest_file {
|
let current_file = match latest_file {
|
||||||
Ok(file) if 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 today_file_name = format!(
|
|
||||||
"{}-{:02}-{:02}.md",
|
|
||||||
today.year(),
|
|
||||||
today.month(),
|
|
||||||
today.day()
|
|
||||||
);
|
|
||||||
let mut today_file_path = data_dir.clone();
|
|
||||||
today_file_path.push(today_file_name);
|
|
||||||
|
|
||||||
let arena = Arena::new();
|
let arena = Arena::new();
|
||||||
let root = parse_todo_file(&file, &arena);
|
let root = parse_todo_file(&file, &arena);
|
||||||
//println!("{:#?}", root);
|
|
||||||
|
|
||||||
|
//println!("{:#?}", root);
|
||||||
//println!("=======================================================");
|
//println!("=======================================================");
|
||||||
|
|
||||||
let sections = &cfg.sections.unwrap();
|
let sections = &cfg.sections.unwrap();
|
||||||
|
|
@ -76,71 +71,34 @@ fn main() {
|
||||||
|
|
||||||
let level = groups.values().map(|group| group.level).min().unwrap_or(2);
|
let level = groups.values().map(|group| group.level).min().unwrap_or(2);
|
||||||
|
|
||||||
sections
|
let data = sections
|
||||||
.iter()
|
.iter()
|
||||||
.map(|section| match groups.get(section) {
|
.map(|section| match groups.get(section) {
|
||||||
Some(group) => group.clone(),
|
Some(group) => group.clone(),
|
||||||
None => TaskGroup::empty(section.to_string(), level),
|
None => TaskGroup::empty(section.to_string(), level),
|
||||||
})
|
})
|
||||||
.for_each(|task_group| println!("{}", task_group.to_string()));
|
.collect();
|
||||||
let mut content = format!(
|
|
||||||
"# Today's tasks {}-{:02}-{:02}\n",
|
|
||||||
today.year(),
|
|
||||||
today.month(),
|
|
||||||
today.day()
|
|
||||||
);
|
|
||||||
|
|
||||||
sections
|
let new_file = write_file(&data_dir, &today, &data);
|
||||||
.iter()
|
|
||||||
.map(|section| match groups.get(section) {
|
|
||||||
Some(group) => group.clone(),
|
|
||||||
None => TaskGroup::empty(section.to_string(), level),
|
|
||||||
})
|
|
||||||
.for_each(|task_group| {
|
|
||||||
content.push_str(format!("\n{}", task_group.to_string()).as_str())
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut file = File::create(today_file_path.clone())
|
Some(new_file)
|
||||||
.expect("Could not open today's file: {today_file_path}");
|
|
||||||
write!(file, "{}", content).expect("Could not write to file: {today_file_path}");
|
|
||||||
|
|
||||||
Some(today_file_path)
|
|
||||||
}
|
|
||||||
Ok(file) => {
|
|
||||||
println!("Today's file was created");
|
|
||||||
Some(file.file.path())
|
|
||||||
}
|
}
|
||||||
Err(_) => {
|
Err(_) => {
|
||||||
println!("No files in dir: {:}", cfg.notes_dir.unwrap());
|
println!("No files in dir: {:}", cfg.notes_dir.unwrap());
|
||||||
|
|
||||||
let today_file_name = format!(
|
|
||||||
"{}-{:02}-{:02}.md",
|
|
||||||
today.year(),
|
|
||||||
today.month(),
|
|
||||||
today.day()
|
|
||||||
);
|
|
||||||
let mut today_file_path = data_dir.clone();
|
|
||||||
today_file_path.push(today_file_name);
|
|
||||||
|
|
||||||
let sections = &cfg.sections.unwrap();
|
let sections = &cfg.sections.unwrap();
|
||||||
let mut content = format!(
|
let data = sections
|
||||||
"# Today's tasks {}-{:02}-{:02}\n",
|
|
||||||
today.year(),
|
|
||||||
today.month(),
|
|
||||||
today.day()
|
|
||||||
);
|
|
||||||
sections
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|section| TaskGroup::empty(section.to_string(), 2))
|
.map(|sec| TaskGroup::empty(sec.clone(), 2))
|
||||||
.for_each(|task_group| {
|
.collect();
|
||||||
content.push_str(format!("\n{}", task_group.to_string()).as_str())
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut file = File::create(today_file_path.clone())
|
let new_file = write_file(&data_dir, &today, &data);
|
||||||
.expect("Could not open today's file: {today_file_path}");
|
|
||||||
write!(file, "{}", content).expect("Could not write to file: {today_file_path}");
|
|
||||||
|
|
||||||
Some(today_file_path)
|
Some(new_file)
|
||||||
|
}
|
||||||
|
Ok(file) => {
|
||||||
|
println!("Today's file was created");
|
||||||
|
Some(file.file.path())
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -151,6 +109,32 @@ fn main() {
|
||||||
.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 {
|
||||||
|
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()));
|
||||||
|
|
||||||
|
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 parse_todo_file<'a>(file: &TodoFile, 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 {
|
let options = &ComrakOptions {
|
||||||
extension: ComrakExtensionOptions {
|
extension: ComrakExtensionOptions {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue