From b0cca6a4088c1f3dd0bb99d0d6f74512f592089c Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 18 Aug 2022 16:10:23 +0300 Subject: [PATCH] cli/show: add possibility to choose a task by idx Closes #26 --- src/cli/show.rs | 62 ++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 9 deletions(-) diff --git a/src/cli/show.rs b/src/cli/show.rs index 7deaaad..9d4c06a 100644 --- a/src/cli/show.rs +++ b/src/cli/show.rs @@ -25,10 +25,10 @@ pub struct Args { #[clap(short, long)] clip: bool, - part: Option, + rest: Vec, } -#[derive(clap::ValueEnum, Clone)] +#[derive(Clone, clap::ValueEnum)] enum PrintPart { Project, DirPath, @@ -49,17 +49,61 @@ impl std::str::FromStr for PrintPart { } pub fn execute(repo: impl Repository, args: Args) { - let task = match repo.get_current_task_opt() { - Ok(None) => { - return eprintln!("You don't have an active task."); + let (idx, part) = match args.rest.len() { + 0 => (None, None), + 1 => { + let first = args.rest.first().unwrap(); + match first.parse::() { + Ok(idx) => (Some(idx), None), + Err(idx_err) => match first.parse::() { + Ok(part) => (None, Some(part)), + Err(part_err) => { + return eprint!( + r#"error: Invalid value "{}": {}; {}"#, + first, idx_err, part_err + ) + } + }, + } } - Ok(Some(CurrentTaskInfo { task, .. })) => task, - Err(err) => { - return eprintln!("Cannot read current task: {}", err); + 2 => { + let mut rest = args.rest.iter(); + let first = rest.next().unwrap(); + let second = rest.next().unwrap(); + + let idx = match first.parse::() { + Ok(idx) => Some(idx), + Err(err) => return eprint!(r#"error: Invalid value "{}": {}"#, first, err), + }; + let part = match second.parse::() { + Ok(part) => Some(part), + Err(err) => return eprint!(r#"error: Invalid value "{}": {}"#, second, err), + }; + + (idx, part) + } + _ => return eprintln!("error: To much arguments: {}", args.rest.join(",")), + }; + + let task = if let Some(idx) = idx { + match repo.get_task_opt(idx) { + Ok(Some(task)) => task, + Ok(None) => return eprintln!("The task not found"), + Err(err) => return eprintln!("Cannot get the task: {}", err), + } + } else { + match repo.get_current_task_opt() { + Ok(None) => { + return eprintln!("You don't have an active task."); + } + Ok(Some(CurrentTaskInfo { task, .. })) => task, + Err(err) => { + return eprintln!("Cannot read current task: {}", err); + } } }; - match args.part { + match part { None => { if args.clip { eprintln!("[WARNING]: You don't provide part. --clip option will ignore.");