diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 78 |
1 files changed, 55 insertions, 23 deletions
diff --git a/src/main.rs b/src/main.rs index 06431e8..ea29cb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::io::Write; +use std::fs::OpenOptions; use std::env; use chrono::{Local, Duration, Datelike}; @@ -11,6 +13,22 @@ mod md_calendar; use md_calendar::*; mod dates; use dates::*; +mod prune; +use prune::*; + +// https://doc.rust-lang.org/std/fs/struct.File.html +// https://doc.rust-lang.org/book/ch12-01-accepting-command-line-arguments.html +fn main() { + let args: Vec<String> = env::args().collect(); + let str_args: Vec<&str> = args.iter().map(String::as_str).collect(); + match str_args[..] { + [_, "html"] => command_html(), + [_, "describe", week] => command_describe(&week), + [_, "generate", week] => command_generate(&week), + [_, "validate"] => command_validate(), + _ => panic!("Invalid command. Valid commands are: html, describe [week], generate [week], and validate.") + } +} fn command_html() { /* Pull tasks from: @@ -32,25 +50,36 @@ fn command_html() { } fn command_describe(week: &str) { + println!("{}", internal_describe(week)); +} + +fn command_generate(week: &str) { + /* This command basically does two things: + * 1) wtd describe [week] > [week].md + * 2) Prune all tasks from this week from wtd.md + */ + let new_week_file_contents = internal_describe(week); let first_date = parse_week_str(week).expect("Invalid week string provided. Should be, e.g., jan_24_2022"); if first_date.weekday().number_from_monday() != 1 { // 1-based, for some reason. panic!("The provided week should start on a Monday."); } - let last_date = first_date + Duration::days(6); // inclusive - - let paths = relevant_files(&first_date, &last_date); - let mut tasks: Vec<Task> = vec![]; - for path in paths { - tasks.append(&mut tasks_from_path(&path)); - } - - let md = tasks_to_md(&tasks, first_date, last_date); - println!("{}", md); + let new_wtd_contents = prune_week_from_file(first_date, "wtd.md"); + let week_file_name = first_date.format("%b_%d_%Y.md").to_string().to_lowercase(); + overwrite_file(&week_file_name, &new_week_file_contents); + overwrite_file("wtd.md", &new_wtd_contents); } -fn command_generate(week: &str) { - // TODO +fn overwrite_file(name: &str, contents: &str) { + let mut file = match OpenOptions::new().create(true).write(true).truncate(true).open(name) { + Err(why) => panic!("Error opening {}: {}", name, why), + Ok(file) => file, + }; + + match file.write_all(contents.as_bytes()) { + Err(why) => panic!("Couldn't write to {}: {}", name, why), + Ok(_) => {}, + }; } fn command_validate() { @@ -81,16 +110,19 @@ fn command_validate() { } } -// https://doc.rust-lang.org/std/fs/struct.File.html -// https://doc.rust-lang.org/book/ch12-01-accepting-command-line-arguments.html -fn main() { - let args: Vec<String> = env::args().collect(); - let str_args: Vec<&str> = args.iter().map(String::as_str).collect(); - match str_args[..] { - [_, "html"] => command_html(), - [_, "describe", week] => command_describe(&week), - [_, "generate", week] => command_generate(&week), - [_, "validate"] => command_validate(), - _ => panic!("Invalid command. Valid commands are: html, describe [week], generate [week], and validate.") +fn internal_describe(week: &str) -> String { + let first_date + = parse_week_str(week).expect("Invalid week string provided. Should be, e.g., jan_24_2022"); + if first_date.weekday().number_from_monday() != 1 { // 1-based, for some reason. + panic!("The provided week should start on a Monday."); + } + let last_date = first_date + Duration::days(6); // inclusive + + let paths = relevant_files(&first_date, &last_date); + let mut tasks: Vec<Task> = vec![]; + for path in paths { + tasks.append(&mut tasks_from_path(&path)); } + + return tasks_to_md(&tasks, first_date, last_date); } |