57 lines
1.5 KiB
Rust
57 lines
1.5 KiB
Rust
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<CurrentTaskInfo>,
|
|
pub tasks: Vec<Task>,
|
|
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");
|
|
}
|