tas/src/cli/remove.rs

56 lines
1.6 KiB
Rust

use crate::{
cli::print_task_detail,
repo::{self, Repository},
};
use std::io::{BufRead, Write};
#[derive(clap::Args)]
pub struct Args {
idx: usize,
}
pub fn execute(repo: impl Repository, args: Args) {
match repo.get_current_task_opt() {
Ok(Some(_)) => {
return eprintln!("You can remove task only when you don't have an active task, yet");
}
Err(err) => {
return eprintln!("Cannot read current task: {}", err);
}
_ => {}
}
let task = match repo.get_task_opt(args.idx) {
Ok(Some(task)) => task,
Ok(None) | Err(repo::Error::NotFound) => return eprintln!("Task not found"),
Err(err) => return eprintln!("Cannot get task: {}", err),
};
println!("You are deleting task:");
print_task_detail(&task);
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."),
}
}
match repo.remove_task(args.idx) {
Ok(_) => {
println!("The task was removed successfully");
print_task_detail(&task);
}
Err(err) => {
eprintln!("Cannot remove the task: {}", err);
}
}
}