cli/show: add possibility to choose a task by idx

Closes #26
This commit is contained in:
Dmitriy Pleshevskiy 2022-08-18 16:10:23 +03:00
parent 15b156414e
commit b0cca6a408
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
1 changed files with 53 additions and 9 deletions

View File

@ -25,10 +25,10 @@ pub struct Args {
#[clap(short, long)] #[clap(short, long)]
clip: bool, clip: bool,
part: Option<PrintPart>, rest: Vec<String>,
} }
#[derive(clap::ValueEnum, Clone)] #[derive(Clone, clap::ValueEnum)]
enum PrintPart { enum PrintPart {
Project, Project,
DirPath, DirPath,
@ -49,17 +49,61 @@ impl std::str::FromStr for PrintPart {
} }
pub fn execute(repo: impl Repository, args: Args) { pub fn execute(repo: impl Repository, args: Args) {
let task = match repo.get_current_task_opt() { let (idx, part) = match args.rest.len() {
Ok(None) => { 0 => (None, None),
return eprintln!("You don't have an active task."); 1 => {
let first = args.rest.first().unwrap();
match first.parse::<usize>() {
Ok(idx) => (Some(idx), None),
Err(idx_err) => match first.parse::<PrintPart>() {
Ok(part) => (None, Some(part)),
Err(part_err) => {
return eprint!(
r#"error: Invalid value "{}": {}; {}"#,
first, idx_err, part_err
)
}
},
}
} }
Ok(Some(CurrentTaskInfo { task, .. })) => task, 2 => {
Err(err) => { let mut rest = args.rest.iter();
return eprintln!("Cannot read current task: {}", err); let first = rest.next().unwrap();
let second = rest.next().unwrap();
let idx = match first.parse::<usize>() {
Ok(idx) => Some(idx),
Err(err) => return eprint!(r#"error: Invalid value "{}": {}"#, first, err),
};
let part = match second.parse::<PrintPart>() {
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 => { None => {
if args.clip { if args.clip {
eprintln!("[WARNING]: You don't provide part. --clip option will ignore."); eprintln!("[WARNING]: You don't provide part. --clip option will ignore.");