From bd0ea82f8e1087f0f96f7e1cfd69cfa386110bbd Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 00:31:01 +0300 Subject: [PATCH] ask before deleting task --- src/main.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 1e1414d..8eeb4fd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use clap::Parser; use serde::{Deserialize, Serialize}; -use std::io::Write; +use std::io::{BufRead, Write}; use xdg::BaseDirectories; mod cli; @@ -47,6 +47,23 @@ fn main() { println!("invalid index"); } + println!("You are deleting task:"); + println!(" {}", tasks[idx - 1].name); + println!(); + 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();