76 lines
1.9 KiB
Rust
76 lines
1.9 KiB
Rust
use crate::cli::print_task_detail;
|
|
use crate::repo::{self, Repository};
|
|
|
|
use std::cmp::Ordering;
|
|
|
|
#[derive(clap::Args)]
|
|
pub struct Args {
|
|
idx: usize,
|
|
#[clap(subcommand)]
|
|
priority: Priority,
|
|
}
|
|
|
|
#[derive(clap::Subcommand)]
|
|
pub enum Priority {
|
|
Before { idx: usize },
|
|
After { idx: usize },
|
|
}
|
|
|
|
pub fn execute(repo: impl Repository, args: Args) {
|
|
match repo.get_current_task_opt() {
|
|
Ok(Some(_)) => {
|
|
return eprintln!(
|
|
"You can change priority only when you don't have an active task, yet"
|
|
)
|
|
}
|
|
Err(err) => {
|
|
return eprintln!("Cannot read current task: {}", err);
|
|
}
|
|
_ => {}
|
|
}
|
|
|
|
let target_idx = args.idx;
|
|
|
|
if let Err(err) = repo.get_task_opt(target_idx) {
|
|
return eprintln!("Task not found: {}", err);
|
|
}
|
|
|
|
let idx = match args.priority {
|
|
Priority::Before { idx } | Priority::After { idx } => match target_idx.cmp(&idx) {
|
|
Ordering::Equal => return,
|
|
Ordering::Less => idx - 1,
|
|
Ordering::Greater => idx,
|
|
},
|
|
};
|
|
if let Err(err) = repo.get_task_opt(idx) {
|
|
return eprintln!("Task not found: {}", err);
|
|
}
|
|
|
|
let target = match repo.remove_task(target_idx) {
|
|
Ok(removed) => removed,
|
|
Err(err) => return eprintln!("Cannot remove the task: {}", err),
|
|
};
|
|
|
|
let new_idx = match args.priority {
|
|
Priority::Before { .. } => idx - 1,
|
|
Priority::After { .. } => idx,
|
|
};
|
|
|
|
let res = repo.insert_task(repo::InsertTaskData {
|
|
index: Some(new_idx),
|
|
name: target.name,
|
|
group: target.group,
|
|
link: target.link,
|
|
path: target.path,
|
|
});
|
|
match res {
|
|
Ok(task) => {
|
|
println!("The task was reordered successfully");
|
|
print_task_detail(&task);
|
|
}
|
|
Err(err) => {
|
|
eprintln!("Cannot reorder the task: {}", err);
|
|
}
|
|
}
|
|
}
|