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

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,7 +49,50 @@ 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() {
0 => (None, None),
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
)
}
},
}
}
2 => {
let mut rest = args.rest.iter();
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) => { Ok(None) => {
return eprintln!("You don't have an active task."); return eprintln!("You don't have an active task.");
} }
@ -57,9 +100,10 @@ pub fn execute(repo: impl Repository, args: Args) {
Err(err) => { Err(err) => {
return eprintln!("Cannot read current task: {}", 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.");