add start task subcommand
This commit is contained in:
parent
48aab9e860
commit
972aae9460
4 changed files with 72 additions and 17 deletions
10
Cargo.lock
generated
10
Cargo.lock
generated
|
@ -121,6 +121,15 @@ version = "0.2.127"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "505e71a4706fa491e9b1b55f51b95d4037d0821ee40131190475f692b35b009b"
|
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]]
|
[[package]]
|
||||||
name = "once_cell"
|
name = "once_cell"
|
||||||
version = "1.13.0"
|
version = "1.13.0"
|
||||||
|
@ -274,6 +283,7 @@ name = "tik"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
|
"log",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"xdg",
|
"xdg",
|
||||||
|
|
|
@ -7,6 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "3.2.16", default-features = false, features = ["derive", "std"] }
|
clap = { version = "3.2.16", default-features = false, features = ["derive", "std"] }
|
||||||
|
log = "0.4.17"
|
||||||
serde = { version = "1.0.142", features = ["derive"] }
|
serde = { version = "1.0.142", features = ["derive"] }
|
||||||
serde_json = "1.0.83"
|
serde_json = "1.0.83"
|
||||||
xdg = "2.4.1"
|
xdg = "2.4.1"
|
||||||
|
|
|
@ -10,7 +10,12 @@ pub struct Args {
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
pub enum SubCommand {
|
pub enum SubCommand {
|
||||||
List,
|
List,
|
||||||
Start,
|
Start {
|
||||||
|
#[clap(short, long)]
|
||||||
|
open: bool,
|
||||||
|
|
||||||
|
idx: Option<usize>,
|
||||||
|
},
|
||||||
Pause,
|
Pause,
|
||||||
Finish,
|
Finish,
|
||||||
Status,
|
Status,
|
||||||
|
|
71
src/main.rs
71
src/main.rs
|
@ -18,13 +18,15 @@ fn main() {
|
||||||
.map(|file| serde_json::from_reader(file).unwrap())
|
.map(|file| serde_json::from_reader(file).unwrap())
|
||||||
.unwrap_or_default();
|
.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(¤t_task_info_file_path)
|
||||||
|
.map(|file| serde_json::from_reader(file).unwrap())
|
||||||
|
.unwrap_or_default();
|
||||||
|
|
||||||
match args.command {
|
match args.command {
|
||||||
cli::SubCommand::Add { link, name } => {
|
cli::SubCommand::Add { link, name } => {
|
||||||
tasks.push(Task {
|
tasks.push(Task { name, link });
|
||||||
name,
|
|
||||||
link,
|
|
||||||
description: None,
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
||||||
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
||||||
|
@ -39,7 +41,7 @@ fn main() {
|
||||||
no_link,
|
no_link,
|
||||||
} => {
|
} => {
|
||||||
if idx == 0 || idx > tasks.len() {
|
if idx == 0 || idx > tasks.len() {
|
||||||
println!("invalid index");
|
panic!("invalid index");
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut task = &mut tasks[idx - 1];
|
let mut task = &mut tasks[idx - 1];
|
||||||
|
@ -60,7 +62,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
cli::SubCommand::Remove { idx } => {
|
cli::SubCommand::Remove { idx } => {
|
||||||
if idx == 0 || idx > tasks.len() {
|
if idx == 0 || idx > tasks.len() {
|
||||||
println!("invalid index");
|
panic!("invalid index");
|
||||||
}
|
}
|
||||||
|
|
||||||
println!("You are deleting task:");
|
println!("You are deleting task:");
|
||||||
|
@ -90,7 +92,14 @@ fn main() {
|
||||||
}
|
}
|
||||||
cli::SubCommand::List => {
|
cli::SubCommand::List => {
|
||||||
for (i, task) in tasks.iter().enumerate() {
|
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() {
|
if task.link.is_some() {
|
||||||
print!("(link) ");
|
print!("(link) ");
|
||||||
}
|
}
|
||||||
|
@ -102,7 +111,7 @@ fn main() {
|
||||||
priority,
|
priority,
|
||||||
} => {
|
} => {
|
||||||
if target_idx == 0 || target_idx > tasks.len() {
|
if target_idx == 0 || target_idx > tasks.len() {
|
||||||
println!("invalid index");
|
panic!("invalid index");
|
||||||
}
|
}
|
||||||
|
|
||||||
let idx = match priority {
|
let idx = match priority {
|
||||||
|
@ -115,7 +124,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
if idx == 0 || idx > tasks.len() {
|
if idx == 0 || idx > tasks.len() {
|
||||||
println!("invalid index");
|
panic!("invalid index");
|
||||||
}
|
}
|
||||||
|
|
||||||
let target = tasks.remove(target_idx - 1);
|
let target = tasks.remove(target_idx - 1);
|
||||||
|
@ -123,11 +132,9 @@ fn main() {
|
||||||
match priority {
|
match priority {
|
||||||
cli::Priority::Before { .. } => {
|
cli::Priority::Before { .. } => {
|
||||||
tasks.insert(idx - 1, target);
|
tasks.insert(idx - 1, target);
|
||||||
println!("{} < {}", target_idx, idx);
|
|
||||||
}
|
}
|
||||||
cli::Priority::After { .. } => {
|
cli::Priority::After { .. } => {
|
||||||
tasks.insert(idx, target);
|
tasks.insert(idx, target);
|
||||||
println!("{} > {}", target_idx, idx);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -135,13 +142,39 @@ fn main() {
|
||||||
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
||||||
.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!("starting...");
|
||||||
println!("-- on start hook found");
|
println!("-- on start hook found");
|
||||||
println!("-- running...");
|
println!("-- running...");
|
||||||
println!("> curl ...");
|
println!("> curl ...");
|
||||||
println!("-- status was changed to \"In progress\" successfuly");
|
println!("-- status was changed to \"In progress\" successfuly");
|
||||||
println!("started");
|
*/
|
||||||
}
|
}
|
||||||
cli::SubCommand::Pause => {
|
cli::SubCommand::Pause => {
|
||||||
println!("pausing...");
|
println!("pausing...");
|
||||||
|
@ -171,10 +204,16 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Deserialize, Serialize)]
|
#[derive(Deserialize, Serialize, Clone)]
|
||||||
struct Task {
|
struct Task {
|
||||||
name: String,
|
name: String,
|
||||||
link: Option<String>,
|
link: Option<String>,
|
||||||
description: Option<String>,
|
|
||||||
// created_at
|
// created_at
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Deserialize, Serialize)]
|
||||||
|
struct CurrentTaskInfo {
|
||||||
|
task_idx: usize,
|
||||||
|
task: Task,
|
||||||
|
// started_at
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue