From 4abf8a934127a1209d5be414bf28470cdfefb82d Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 16:48:22 +0300 Subject: [PATCH] cli: extract status and finish subcommands --- src/cli.rs | 3 +++ src/cli/add.rs | 0 src/cli/edit.rs | 0 src/cli/finish.rs | 41 ++++++++++++++++++++++++++++++++++++ src/cli/list.rs | 0 src/cli/pause.rs | 0 src/cli/priority.rs | 0 src/cli/remove.rs | 0 src/cli/start.rs | 0 src/cli/status.rs | 16 ++++++++++++++ src/main.rs | 51 +++++++++------------------------------------ 11 files changed, 70 insertions(+), 41 deletions(-) create mode 100644 src/cli/add.rs create mode 100644 src/cli/edit.rs create mode 100644 src/cli/finish.rs create mode 100644 src/cli/list.rs create mode 100644 src/cli/pause.rs create mode 100644 src/cli/priority.rs create mode 100644 src/cli/remove.rs create mode 100644 src/cli/start.rs create mode 100644 src/cli/status.rs diff --git a/src/cli.rs b/src/cli.rs index 8ce05e8..1a53729 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -14,6 +14,9 @@ //! along with tas. If not, see . //! +pub mod finish; +pub mod status; + #[derive(clap::Parser)] #[clap(author, version, about = "Experemental task manager")] pub struct Args { diff --git a/src/cli/add.rs b/src/cli/add.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/edit.rs b/src/cli/edit.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/finish.rs b/src/cli/finish.rs new file mode 100644 index 0000000..30795a2 --- /dev/null +++ b/src/cli/finish.rs @@ -0,0 +1,41 @@ +use crate::{CurrentTaskInfo, Task}; +use std::io::Write; +use std::path::PathBuf; + +pub struct Request { + pub current_task_info: Option, + pub tasks: Vec, + pub finished_tasks_file_path: PathBuf, + pub tasks_file_path: PathBuf, + pub current_task_info_file_path: PathBuf, +} + +pub fn execute(mut req: Request) { + match req.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(&req.finished_tasks_file_path) + .map(|file| serde_json::from_reader(file).unwrap()) + .unwrap_or_default(); + + let task = req.tasks.remove(info.task_idx - 1); + finished_tasks.push(task); + + let mut file = std::fs::File::create(req.tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&req.tasks).unwrap()) + .unwrap(); + + let mut file = std::fs::File::create(req.finished_tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&finished_tasks).unwrap()) + .unwrap(); + + let mut file = std::fs::File::create(req.current_task_info_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&req.current_task_info).unwrap()) + .unwrap(); + + println!("finished"); + } + } +} diff --git a/src/cli/list.rs b/src/cli/list.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/pause.rs b/src/cli/pause.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/priority.rs b/src/cli/priority.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/remove.rs b/src/cli/remove.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/start.rs b/src/cli/start.rs new file mode 100644 index 0000000..e69de29 diff --git a/src/cli/status.rs b/src/cli/status.rs new file mode 100644 index 0000000..913503b --- /dev/null +++ b/src/cli/status.rs @@ -0,0 +1,16 @@ +use crate::CurrentTaskInfo; + +pub fn execute(current_task_info: Option) { + 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); + } + } + } +} diff --git a/src/main.rs b/src/main.rs index c9a3c02..8e16d5a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -232,57 +232,26 @@ fn main() { .unwrap(); println!("paused"); } - 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); - } - } - }, + cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request { + tasks, + current_task_info, + current_task_info_file_path, + tasks_file_path, + finished_tasks_file_path, + }), + cli::SubCommand::Status => cli::status::execute(current_task_info), } } #[derive(Deserialize, Serialize, Clone)] -struct Task { +pub struct Task { name: String, link: Option, // created_at } #[derive(Deserialize, Serialize)] -struct CurrentTaskInfo { +pub struct CurrentTaskInfo { task_idx: usize, task: Task, // started_at