add start task subcommand

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-05 12:35:14 +03:00
parent 48aab9e860
commit 972aae9460
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
4 changed files with 72 additions and 17 deletions

10
Cargo.lock generated
View file

@ -121,6 +121,15 @@ version = "0.2.127"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b"
[[package]]
name = "log"
version = "0.4.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "abb12e687cfb44aa40f41fc3978ef76448f9b6038cad6aef4259d3c095a2382e"
dependencies = [
"cfg-if",
]
[[package]]
name = "once_cell"
version = "1.13.0"
@ -274,6 +283,7 @@ name = "tik"
version = "0.1.0"
dependencies = [
"clap",
"log",
"serde",
"serde_json",
"xdg",

View file

@ -7,6 +7,7 @@ edition = "2021"
[dependencies]
clap = { version = "3.2.16", default-features = false, features = ["derive", "std"] }
log = "0.4.17"
serde = { version = "1.0.142", features = ["derive"] }
serde_json = "1.0.83"
xdg = "2.4.1"

View file

@ -10,7 +10,12 @@ pub struct Args {
#[derive(Parser)]
pub enum SubCommand {
List,
Start,
Start {
#[clap(short, long)]
open: bool,
idx: Option<usize>,
},
Pause,
Finish,
Status,

View file

@ -18,13 +18,15 @@ fn main() {
.map(|file| serde_json::from_reader(file).unwrap())
.unwrap_or_default();
let current_task_info_file_path = xdg_dirs.place_data_file("current.json").unwrap();
let mut current_task_info: Option<CurrentTaskInfo> =
std::fs::File::open(&current_task_info_file_path)
.map(|file| serde_json::from_reader(file).unwrap())
.unwrap_or_default();
match args.command {
cli::SubCommand::Add { link, name } => {
tasks.push(Task {
name,
link,
description: None,
});
tasks.push(Task { name, link });
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&tasks).unwrap())
@ -39,7 +41,7 @@ fn main() {
no_link,
} => {
if idx == 0 || idx > tasks.len() {
println!("invalid index");
panic!("invalid index");
}
let mut task = &mut tasks[idx - 1];
@ -60,7 +62,7 @@ fn main() {
}
cli::SubCommand::Remove { idx } => {
if idx == 0 || idx > tasks.len() {
println!("invalid index");
panic!("invalid index");
}
println!("You are deleting task:");
@ -90,7 +92,14 @@ fn main() {
}
cli::SubCommand::List => {
for (i, task) in tasks.iter().enumerate() {
print!("{}. ", i + 1);
let idx = i + 1;
match current_task_info {
Some(CurrentTaskInfo { task_idx, .. }) if task_idx == idx => print!("> "),
_ => print!(" "),
}
print!("{}. ", idx);
if task.link.is_some() {
print!("(link) ");
}
@ -102,7 +111,7 @@ fn main() {
priority,
} => {
if target_idx == 0 || target_idx > tasks.len() {
println!("invalid index");
panic!("invalid index");
}
let idx = match priority {
@ -115,7 +124,7 @@ fn main() {
}
};
if idx == 0 || idx > tasks.len() {
println!("invalid index");
panic!("invalid index");
}
let target = tasks.remove(target_idx - 1);
@ -123,11 +132,9 @@ fn main() {
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);
}
}
@ -135,13 +142,39 @@ fn main() {
file.write_all(&serde_json::to_vec(&tasks).unwrap())
.unwrap();
}
cli::SubCommand::Start => {
cli::SubCommand::Start { idx, open } => {
let idx = idx.unwrap_or(1);
if idx == 0 || idx > tasks.len() {
panic!("invalid index");
}
let task = &tasks[idx - 1];
current_task_info.replace(CurrentTaskInfo {
task_idx: idx,
task: task.clone(),
});
let mut file = std::fs::File::create(&current_task_info_file_path).unwrap();
file.write_all(&serde_json::to_vec(&current_task_info).unwrap())
.unwrap();
println!("started");
if let (Some(link), true) = (task.link.as_ref(), open) {
log::debug!("opening link...");
std::process::Command::new("xdg-open")
.arg(link)
.spawn()
.expect("failed to start");
log::debug!("opened");
}
/*
println!("starting...");
println!("-- on start hook found");
println!("-- running...");
println!("> curl ...");
println!("-- status was changed to \"In progress\" successfuly");
println!("started");
*/
}
cli::SubCommand::Pause => {
println!("pausing...");
@ -171,10 +204,16 @@ fn main() {
}
}
#[derive(Deserialize, Serialize)]
#[derive(Deserialize, Serialize, Clone)]
struct Task {
name: String,
link: Option<String>,
description: Option<String>,
// created_at
}
#[derive(Deserialize, Serialize)]
struct CurrentTaskInfo {
task_idx: usize,
task: Task,
// started_at
}