diff --git a/src/cli.rs b/src/cli.rs index 24f44ed..7a23b26 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,6 +18,7 @@ pub mod finish; pub mod list; pub mod pause; pub mod priority; +pub mod remove; pub mod start; pub mod status; @@ -47,9 +48,7 @@ pub enum SubCommand { idx: usize, }, - Remove { - idx: usize, - }, + Remove(self::remove::Args), Priority(self::priority::Args), List, Start(self::start::Args), diff --git a/src/cli/remove.rs b/src/cli/remove.rs index e69de29..86f8c6b 100644 --- a/src/cli/remove.rs +++ b/src/cli/remove.rs @@ -0,0 +1,56 @@ +use std::io::{BufRead, Write}; +use std::path::PathBuf; + +use crate::{CurrentTaskInfo, Task}; + +#[derive(clap::Args)] +pub struct Args { + idx: usize, +} + +pub struct Request { + pub args: Args, + pub current_task_info: Option, + pub tasks: Vec, + pub tasks_file_path: PathBuf, +} + +pub fn execute(mut req: Request) { + if req.current_task_info.is_some() { + panic!("You can remove task only when you don't have an active task, yet"); + } + + let idx = req.args.idx; + if idx == 0 || idx > req.tasks.len() { + panic!("invalid index"); + } + + let task = &req.tasks[idx - 1]; + println!("You are deleting task:"); + 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): "); + std::io::stdout().flush().unwrap(); + let mut stdin = std::io::stdin().lock(); + let mut buf = String::new(); + stdin.read_line(&mut buf).unwrap(); + match buf.chars().next().unwrap_or_default() { + '\r' | '\n' | 'n' | 'N' => return, + 'y' | 'Y' => break, + _ => println!("Unrecognised answer. Please try again."), + } + } + + req.tasks.remove(idx - 1); + + let mut file = std::fs::File::create(&req.tasks_file_path).unwrap(); + file.write_all(&serde_json::to_vec(&req.tasks).unwrap()) + .unwrap(); + + println!("removed"); +} diff --git a/src/main.rs b/src/main.rs index cec3bd0..1d140be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ use clap::Parser; use serde::{Deserialize, Serialize}; -use std::io::{BufRead, Write}; +use std::io::Write; use xdg::BaseDirectories; mod cli; @@ -91,43 +91,13 @@ fn main() { println!("changed"); } - cli::SubCommand::Remove { idx } => { - if current_task_info.is_some() { - 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!(" {}", 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): "); - std::io::stdout().flush().unwrap(); - let mut stdin = std::io::stdin().lock(); - let mut buf = String::new(); - stdin.read_line(&mut buf).unwrap(); - match buf.chars().next().unwrap_or_default() { - '\r' | '\n' | 'n' | 'N' => return, - 'y' | 'Y' => break, - _ => println!("Unrecognised answer. Please try again."), - } - } - - tasks.remove(idx - 1); - - let mut file = std::fs::File::create(&tasks_file_path).unwrap(); - file.write_all(&serde_json::to_vec(&tasks).unwrap()) - .unwrap(); - - println!("removed"); + cli::SubCommand::Remove(args) => { + cli::remove::execute(cli::remove::Request { + args, + current_task_info, + tasks, + tasks_file_path, + }); } cli::SubCommand::List => { cli::list::execute(cli::list::Request {