extract-cli #9

Merged
pleshevskiy merged 7 commits from extract-cli into main 2022-08-05 22:07:16 +03:00
3 changed files with 97 additions and 68 deletions
Showing only changes of commit 04c25e2589 - Show all commits

View file

@ -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 },
}

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); 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,
} });
}
if target_idx == 0 || target_idx > tasks.len() { cli::SubCommand::Start(args) => {
panic!("invalid index"); cli::start::execute(cli::start::Request {
} args,
tasks,
let idx = match priority { current_task_info,
cli::Priority::Before { idx } | cli::Priority::After { idx } => { current_task_info_file_path,
match target_idx.cmp(&idx) { });
Ordering::Equal => return, }
Ordering::Less => idx - 1, cli::SubCommand::Pause => {
Ordering::Greater => idx, cli::pause::execute(cli::pause::Request {
} current_task_info,
} current_task_info_file_path,
}; });
if idx == 0 || idx > tasks.len() { }
panic!("invalid index"); cli::SubCommand::Finish => {
} cli::finish::execute(cli::finish::Request {
tasks,
let target = tasks.remove(target_idx - 1); current_task_info,
current_task_info_file_path,
match priority { tasks_file_path,
cli::Priority::Before { .. } => { finished_tasks_file_path,
tasks.insert(idx - 1, target); });
} }
cli::Priority::After { .. } => { cli::SubCommand::Status => {
tasks.insert(idx, target); cli::status::execute(current_task_info);
}
}
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,
tasks,
current_task_info,
current_task_info_file_path,
}),
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 {
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),
} }
} }