tas/src/cli/remove.rs

71 lines
2.3 KiB
Rust

//! Copyright (C) 2022, Dmitriy Pleshevskiy <dmitriy@ideascup.me>
//!
//! tas is free software: you can redistribute it and/or modify
//! it under the terms of the GNU General Public License as published by
//! the Free Software Foundation, either version 3 of the License, or
//! (at your option) any later version.
//!
//! tas is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with tas. If not, see <https://www.gnu.org/licenses/>.
//!
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);
}
}
}