add priority subcommand

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-05 10:42:35 +03:00
parent 80c5265e3b
commit 48aab9e860
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
2 changed files with 53 additions and 1 deletions

View file

@ -34,4 +34,15 @@ pub enum SubCommand {
Remove {
idx: usize,
},
Priority {
idx: usize,
#[clap(subcommand)]
priority: Priority,
},
}
#[derive(Parser)]
pub enum Priority {
Before { idx: usize },
After { idx: usize },
}

View file

@ -1,6 +1,9 @@
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::io::{BufRead, Write};
use std::{
cmp::Ordering,
io::{BufRead, Write},
};
use xdg::BaseDirectories;
mod cli;
@ -94,6 +97,44 @@ fn main() {
println!("{}", task.name);
}
}
cli::SubCommand::Priority {
idx: target_idx,
priority,
} => {
if target_idx == 0 || target_idx > tasks.len() {
println!("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() {
println!("invalid index");
}
let target = tasks.remove(target_idx - 1);
match priority {
cli::Priority::Before { .. } => {
tasks.insert(idx - 1, target);
println!("{} < {}", target_idx, idx);
}
cli::Priority::After { .. } => {
tasks.insert(idx, target);
println!("{} > {}", target_idx, idx);
}
}
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&tasks).unwrap())
.unwrap();
}
cli::SubCommand::Start => {
println!("starting...");
println!("-- on start hook found");