91 lines
2.6 KiB
Rust
91 lines
2.6 KiB
Rust
//! Copyright (C) 2022, Dmitriy Pleshevskiy <dmitriy@ideascup.me>
|
|
//!
|
|
//! tas is free software: you can redistribute it and/or modify
|
|
//! it under the terms of the GNU General Public License as published by
|
|
//! the Free Software Foundation, either version 3 of the License, or
|
|
//! (at your option) any later version.
|
|
//!
|
|
//! tas is distributed in the hope that it will be useful,
|
|
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
//! GNU General Public License for more details.
|
|
//!
|
|
//! You should have received a copy of the GNU General Public License
|
|
//! along with tas. If not, see <https://www.gnu.org/licenses/>.
|
|
//!
|
|
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,
|
|
project: target.project,
|
|
link: target.link,
|
|
dir_path: target.dir_path,
|
|
});
|
|
match res {
|
|
Ok(task) => {
|
|
println!("The task was reordered successfully");
|
|
print_task_detail(&task);
|
|
}
|
|
Err(err) => {
|
|
eprintln!("Cannot reorder the task: {}", err);
|
|
}
|
|
}
|
|
}
|