From e16011950d07a98f04f4b7cdb613bbab2ba6947d Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 13:06:48 +0300 Subject: [PATCH] add pause, finish and status subcommands --- src/main.rs | 82 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 26 deletions(-) diff --git a/src/main.rs b/src/main.rs index 557992f..3a40a94 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,8 @@ fn main() { let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap(); + let finished_tasks_file_path = xdg_dirs.place_data_file("finished_data.json").unwrap(); + let tasks_file_path = xdg_dirs.place_data_file("data.json").unwrap(); let mut tasks: Vec = std::fs::File::open(&tasks_file_path) .map(|file| serde_json::from_reader(file).unwrap()) @@ -41,7 +43,7 @@ fn main() { no_link, } => { if current_task_info.is_some() { - panic!("You can change priority only when you don't have an active task, yet"); + panic!("You can edit task only when you don't have an active task, yet"); } if idx == 0 || idx > tasks.len() { @@ -66,16 +68,20 @@ fn main() { } cli::SubCommand::Remove { idx } => { if current_task_info.is_some() { - panic!("You can change priority only when you don't have an active task, yet"); + panic!("You can remove task only when you don't have an active task, yet"); } if idx == 0 || idx > tasks.len() { panic!("invalid index"); } + let task = &tasks[idx - 1]; println!("You are deleting task:"); - println!(" {}", tasks[idx - 1].name); - println!(); + println!(" {}", task.name); + if let Some(ref link) = task.link { + println!(" link: {}", link); + } + println!("In most cases you need to `finish` command"); loop { print!("Do you still want to delete the task? (y/N): "); @@ -189,30 +195,54 @@ fn main() { */ } cli::SubCommand::Pause => { - println!("pausing..."); + if current_task_info.take().is_none() { + panic!("You can use the pause subcommand only when you have an active task"); + } + + let mut file = std::fs::File::create(¤t_task_info_file_path).unwrap(); + file.write_all(&serde_json::to_vec(¤t_task_info).unwrap()) + .unwrap(); println!("paused"); } - cli::SubCommand::Finish => { - println!("finishing..."); - println!("-- on finish hook found"); - println!("-- running..."); - println!("> curl ..."); - println!("-- status was changed to \"Deploy\" successfuly"); - println!("finished"); - } - cli::SubCommand::Status => { - println!("Information about your current task:"); - println!("[work]"); - println!("https://redmine.example.com/issues/4051"); - println!( - "#4051 Plannig/Assignment: Assign week and day general delvirebles after regular" - ); - println!("-------------------------------------------------------------------------"); - println!("lorem impsum dolor dolor impsum lorem lorem impsum"); - println!("lorem dolor impsum lorem lorem impsum"); - println!("lorem dolor impsum lorem lorem impsum lorem impsum dolor dolor impsum lorem"); - println!("lorem impsum"); - } + cli::SubCommand::Finish => match current_task_info.take() { + None => { + panic!("You can use the finish subcommand only when you have an active task") + } + Some(info) => { + let mut finished_tasks: Vec = std::fs::File::open(&finished_tasks_file_path) + .map(|file| serde_json::from_reader(file).unwrap()) + .unwrap_or_default(); + + let task = tasks.remove(info.task_idx - 1); + finished_tasks.push(task); + + let mut file = std::fs::File::create(&tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&tasks).unwrap()) + .unwrap(); + + let mut file = std::fs::File::create(&finished_tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&finished_tasks).unwrap()) + .unwrap(); + + let mut file = std::fs::File::create(¤t_task_info_file_path).unwrap(); + file.write_all(&serde_json::to_vec(¤t_task_info).unwrap()) + .unwrap(); + + println!("finished"); + } + }, + cli::SubCommand::Status => match current_task_info { + None => { + panic!("You don't have an active task."); + } + Some(info) => { + println!("Information about your current task:"); + println!(" {}", info.task.name); + if let Some(link) = info.task.link { + println!(" link: {}", link); + } + } + }, } }