cli: extract priority subcommand

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-05 18:19:12 +03:00
parent f3090eebe9
commit 04c25e2589
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
3 changed files with 97 additions and 68 deletions

View file

@ -16,6 +16,7 @@
pub mod finish;
pub mod pause;
pub mod priority;
pub mod start;
pub mod status;
@ -48,20 +49,10 @@ pub enum SubCommand {
Remove {
idx: usize,
},
Priority {
idx: usize,
#[clap(subcommand)]
priority: Priority,
},
Priority(self::priority::Args),
List,
Start(self::start::Args),
Pause,
Finish,
Status,
}
#[derive(clap::Subcommand)]
pub enum Priority {
Before { idx: usize },
After { idx: usize },
}

View file

@ -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();
}

View file

@ -148,64 +148,40 @@ fn main() {
println!("{}", task.name);
}
}
cli::SubCommand::Priority {
idx: target_idx,
priority,
} => {
if current_task_info.is_some() {
panic!("You can change priority only when you don't have an active task, yet");
cli::SubCommand::Priority(args) => {
cli::priority::execute(cli::priority::Request {
args,
tasks,
tasks_file_path,
current_task_info,
});
}
if target_idx == 0 || target_idx > tasks.len() {
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 {
cli::SubCommand::Start(args) => {
cli::start::execute(cli::start::Request {
args,
tasks,
current_task_info,
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_file_path,
}),
cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request {
});
}
cli::SubCommand::Finish => {
cli::finish::execute(cli::finish::Request {
tasks,
current_task_info,
current_task_info_file_path,
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);
}
}
}