From 972aae94602c683791c13e6a3c2cbc8adb259bc5 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 12:35:14 +0300 Subject: [PATCH] add start task subcommand --- Cargo.lock | 10 ++++++++ Cargo.toml | 1 + src/cli.rs | 7 +++++- src/main.rs | 71 +++++++++++++++++++++++++++++++++++++++++------------ 4 files changed, 72 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 725bc05..51133ab 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/Cargo.toml b/Cargo.toml index 54cc653..80854e2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/cli.rs b/src/cli.rs index c163542..18a5962 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -10,7 +10,12 @@ pub struct Args { #[derive(Parser)] pub enum SubCommand { List, - Start, + Start { + #[clap(short, long)] + open: bool, + + idx: Option, + }, Pause, Finish, Status, diff --git a/src/main.rs b/src/main.rs index 6fcb52b..26db63c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = + std::fs::File::open(¤t_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(¤t_task_info_file_path).unwrap(); + file.write_all(&serde_json::to_vec(¤t_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, - description: Option, // created_at } + +#[derive(Deserialize, Serialize)] +struct CurrentTaskInfo { + task_idx: usize, + task: Task, + // started_at +}