cli: extract priority subcommand
This commit is contained in:
parent
f3090eebe9
commit
04c25e2589
3 changed files with 97 additions and 68 deletions
13
src/cli.rs
13
src/cli.rs
|
@ -16,6 +16,7 @@
|
||||||
|
|
||||||
pub mod finish;
|
pub mod finish;
|
||||||
pub mod pause;
|
pub mod pause;
|
||||||
|
pub mod priority;
|
||||||
pub mod start;
|
pub mod start;
|
||||||
pub mod status;
|
pub mod status;
|
||||||
|
|
||||||
|
@ -48,20 +49,10 @@ pub enum SubCommand {
|
||||||
Remove {
|
Remove {
|
||||||
idx: usize,
|
idx: usize,
|
||||||
},
|
},
|
||||||
Priority {
|
Priority(self::priority::Args),
|
||||||
idx: usize,
|
|
||||||
#[clap(subcommand)]
|
|
||||||
priority: Priority,
|
|
||||||
},
|
|
||||||
List,
|
List,
|
||||||
Start(self::start::Args),
|
Start(self::start::Args),
|
||||||
Pause,
|
Pause,
|
||||||
Finish,
|
Finish,
|
||||||
Status,
|
Status,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(clap::Subcommand)]
|
|
||||||
pub enum Priority {
|
|
||||||
Before { idx: usize },
|
|
||||||
After { idx: usize },
|
|
||||||
}
|
|
||||||
|
|
|
@ -0,0 +1,62 @@
|
||||||
|
use crate::{CurrentTaskInfo, Task};
|
||||||
|
|
||||||
|
use std::cmp::Ordering;
|
||||||
|
use std::io::Write;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
#[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 struct Request {
|
||||||
|
pub args: Args,
|
||||||
|
pub tasks: Vec<Task>,
|
||||||
|
pub current_task_info: Option<CurrentTaskInfo>,
|
||||||
|
pub tasks_file_path: PathBuf,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn execute(req: Request) {
|
||||||
|
if req.current_task_info.is_some() {
|
||||||
|
panic!("You can change priority only when you don't have an active task, yet");
|
||||||
|
}
|
||||||
|
|
||||||
|
let target_idx = req.args.idx;
|
||||||
|
if target_idx == 0 || target_idx > req.tasks.len() {
|
||||||
|
panic!("invalid index");
|
||||||
|
}
|
||||||
|
|
||||||
|
let idx = match req.args.priority {
|
||||||
|
Priority::Before { idx } | Priority::After { idx } => match target_idx.cmp(&idx) {
|
||||||
|
Ordering::Equal => return,
|
||||||
|
Ordering::Less => idx - 1,
|
||||||
|
Ordering::Greater => idx,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
if idx == 0 || idx > req.tasks.len() {
|
||||||
|
panic!("invalid index");
|
||||||
|
}
|
||||||
|
|
||||||
|
let target = req.tasks.remove(target_idx - 1);
|
||||||
|
|
||||||
|
match req.args.priority {
|
||||||
|
Priority::Before { .. } => {
|
||||||
|
req.tasks.insert(idx - 1, target);
|
||||||
|
}
|
||||||
|
Priority::After { .. } => {
|
||||||
|
req.tasks.insert(idx, target);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut file = std::fs::File::create(&req.tasks_file_path).unwrap();
|
||||||
|
file.write_all(&serde_json::to_vec(&req.tasks).unwrap())
|
||||||
|
.unwrap();
|
||||||
|
}
|
68
src/main.rs
68
src/main.rs
|
@ -148,64 +148,40 @@ fn main() {
|
||||||
println!("{}", task.name);
|
println!("{}", task.name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
cli::SubCommand::Priority {
|
cli::SubCommand::Priority(args) => {
|
||||||
idx: target_idx,
|
cli::priority::execute(cli::priority::Request {
|
||||||
priority,
|
args,
|
||||||
} => {
|
tasks,
|
||||||
if current_task_info.is_some() {
|
tasks_file_path,
|
||||||
panic!("You can change priority only when you don't have an active task, yet");
|
current_task_info,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
cli::SubCommand::Start(args) => {
|
||||||
if target_idx == 0 || target_idx > tasks.len() {
|
cli::start::execute(cli::start::Request {
|
||||||
panic!("invalid index");
|
|
||||||
}
|
|
||||||
|
|
||||||
let idx = match priority {
|
|
||||||
cli::Priority::Before { idx } | cli::Priority::After { idx } => {
|
|
||||||
match target_idx.cmp(&idx) {
|
|
||||||
Ordering::Equal => return,
|
|
||||||
Ordering::Less => idx - 1,
|
|
||||||
Ordering::Greater => idx,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
if idx == 0 || idx > tasks.len() {
|
|
||||||
panic!("invalid index");
|
|
||||||
}
|
|
||||||
|
|
||||||
let target = tasks.remove(target_idx - 1);
|
|
||||||
|
|
||||||
match priority {
|
|
||||||
cli::Priority::Before { .. } => {
|
|
||||||
tasks.insert(idx - 1, target);
|
|
||||||
}
|
|
||||||
cli::Priority::After { .. } => {
|
|
||||||
tasks.insert(idx, target);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
|
||||||
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
|
||||||
.unwrap();
|
|
||||||
}
|
|
||||||
cli::SubCommand::Start(args) => cli::start::execute(cli::start::Request {
|
|
||||||
args,
|
args,
|
||||||
tasks,
|
tasks,
|
||||||
current_task_info,
|
current_task_info,
|
||||||
current_task_info_file_path,
|
current_task_info_file_path,
|
||||||
}),
|
});
|
||||||
cli::SubCommand::Pause => cli::pause::execute(cli::pause::Request {
|
}
|
||||||
|
cli::SubCommand::Pause => {
|
||||||
|
cli::pause::execute(cli::pause::Request {
|
||||||
current_task_info,
|
current_task_info,
|
||||||
current_task_info_file_path,
|
current_task_info_file_path,
|
||||||
}),
|
});
|
||||||
cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request {
|
}
|
||||||
|
cli::SubCommand::Finish => {
|
||||||
|
cli::finish::execute(cli::finish::Request {
|
||||||
tasks,
|
tasks,
|
||||||
current_task_info,
|
current_task_info,
|
||||||
current_task_info_file_path,
|
current_task_info_file_path,
|
||||||
tasks_file_path,
|
tasks_file_path,
|
||||||
finished_tasks_file_path,
|
finished_tasks_file_path,
|
||||||
}),
|
});
|
||||||
cli::SubCommand::Status => cli::status::execute(current_task_info),
|
}
|
||||||
|
cli::SubCommand::Status => {
|
||||||
|
cli::status::execute(current_task_info);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue