cli: extract remove subcommand
This commit is contained in:
parent
6c1def1a2d
commit
fe705858de
3 changed files with 66 additions and 41 deletions
|
@ -18,6 +18,7 @@ pub mod finish;
|
||||||
pub mod list;
|
pub mod list;
|
||||||
pub mod pause;
|
pub mod pause;
|
||||||
pub mod priority;
|
pub mod priority;
|
||||||
|
pub mod remove;
|
||||||
pub mod start;
|
pub mod start;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
|
|
||||||
|
@ -47,9 +48,7 @@ pub enum SubCommand {
|
||||||
|
|
||||||
idx: usize,
|
idx: usize,
|
||||||
},
|
},
|
||||||
Remove {
|
Remove(self::remove::Args),
|
||||||
idx: usize,
|
|
||||||
},
|
|
||||||
Priority(self::priority::Args),
|
Priority(self::priority::Args),
|
||||||
List,
|
List,
|
||||||
Start(self::start::Args),
|
Start(self::start::Args),
|
||||||
|
|
|
@ -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<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");
|
||||||
|
}
|
46
src/main.rs
46
src/main.rs
|
@ -28,7 +28,7 @@
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use std::io::{BufRead, Write};
|
use std::io::Write;
|
||||||
use xdg::BaseDirectories;
|
use xdg::BaseDirectories;
|
||||||
|
|
||||||
mod cli;
|
mod cli;
|
||||||
|
@ -91,43 +91,13 @@ fn main() {
|
||||||
|
|
||||||
println!("changed");
|
println!("changed");
|
||||||
}
|
}
|
||||||
cli::SubCommand::Remove { idx } => {
|
cli::SubCommand::Remove(args) => {
|
||||||
if current_task_info.is_some() {
|
cli::remove::execute(cli::remove::Request {
|
||||||
panic!("You can remove task only when you don't have an active task, yet");
|
args,
|
||||||
}
|
current_task_info,
|
||||||
|
tasks,
|
||||||
if idx == 0 || idx > tasks.len() {
|
tasks_file_path,
|
||||||
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::List => {
|
cli::SubCommand::List => {
|
||||||
cli::list::execute(cli::list::Request {
|
cli::list::execute(cli::list::Request {
|
||||||
|
|
Loading…
Reference in a new issue