2022-08-05 00:08:27 +03:00
|
|
|
use clap::Parser;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2022-08-05 10:42:35 +03:00
|
|
|
use std::{
|
|
|
|
cmp::Ordering,
|
|
|
|
io::{BufRead, Write},
|
|
|
|
};
|
2022-08-05 00:08:27 +03:00
|
|
|
use xdg::BaseDirectories;
|
|
|
|
|
|
|
|
mod cli;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let args = cli::Args::parse();
|
|
|
|
|
|
|
|
let xdg_dirs = BaseDirectories::with_prefix(env!("CARGO_PKG_NAME")).unwrap();
|
|
|
|
|
|
|
|
let tasks_file_path = xdg_dirs.place_data_file("data.json").unwrap();
|
|
|
|
let mut tasks: Vec<Task> = std::fs::File::open(&tasks_file_path)
|
|
|
|
.map(|file| serde_json::from_reader(file).unwrap())
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
2022-08-05 12:35:14 +03:00
|
|
|
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();
|
|
|
|
|
2022-08-05 00:08:27 +03:00
|
|
|
match args.command {
|
2022-08-05 00:54:47 +03:00
|
|
|
cli::SubCommand::Add { link, name } => {
|
2022-08-05 12:35:14 +03:00
|
|
|
tasks.push(Task { name, link });
|
2022-08-05 00:08:27 +03:00
|
|
|
|
|
|
|
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
|
|
|
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!("added");
|
|
|
|
}
|
2022-08-05 00:54:47 +03:00
|
|
|
cli::SubCommand::Edit {
|
|
|
|
idx,
|
|
|
|
name,
|
|
|
|
link,
|
|
|
|
no_link,
|
|
|
|
} => {
|
2022-08-05 12:39:19 +03:00
|
|
|
if current_task_info.is_some() {
|
|
|
|
panic!("You can change priority only when you don't have an active task, yet");
|
|
|
|
}
|
|
|
|
|
2022-08-05 00:08:27 +03:00
|
|
|
if idx == 0 || idx > tasks.len() {
|
2022-08-05 12:35:14 +03:00
|
|
|
panic!("invalid index");
|
2022-08-05 00:08:27 +03:00
|
|
|
}
|
|
|
|
|
2022-08-05 00:54:47 +03:00
|
|
|
let mut task = &mut tasks[idx - 1];
|
|
|
|
if let Some(name) = name {
|
|
|
|
task.name = name;
|
|
|
|
}
|
|
|
|
if let Some(link) = link {
|
|
|
|
task.link = Some(link);
|
|
|
|
} else if no_link {
|
|
|
|
task.link = None;
|
|
|
|
}
|
2022-08-05 00:08:27 +03:00
|
|
|
|
|
|
|
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
|
|
|
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!("changed");
|
|
|
|
}
|
|
|
|
cli::SubCommand::Remove { idx } => {
|
2022-08-05 12:39:19 +03:00
|
|
|
if current_task_info.is_some() {
|
|
|
|
panic!("You can change priority only when you don't have an active task, yet");
|
|
|
|
}
|
|
|
|
|
2022-08-05 00:08:27 +03:00
|
|
|
if idx == 0 || idx > tasks.len() {
|
2022-08-05 12:35:14 +03:00
|
|
|
panic!("invalid index");
|
2022-08-05 00:08:27 +03:00
|
|
|
}
|
2022-08-05 00:31:01 +03:00
|
|
|
|
|
|
|
println!("You are deleting task:");
|
|
|
|
println!(" {}", tasks[idx - 1].name);
|
|
|
|
println!();
|
|
|
|
println!("In most cases you need to `finish` command");
|
|
|
|
loop {
|
|
|
|
print!("Do you still want to delete the task? (y/N): ");
|
|
|
|
std::io::stdout().flush().unwrap();
|
|
|
|
let mut stdin = std::io::stdin().lock();
|
|
|
|
let mut buf = String::new();
|
|
|
|
stdin.read_line(&mut buf).unwrap();
|
|
|
|
match buf.chars().next().unwrap_or_default() {
|
|
|
|
'\r' | '\n' | 'n' | 'N' => return,
|
|
|
|
'y' | 'Y' => break,
|
|
|
|
_ => println!("Unrecognised answer. Please try again."),
|
|
|
|
}
|
|
|
|
}
|
2022-08-05 00:08:27 +03:00
|
|
|
|
|
|
|
tasks.remove(idx - 1);
|
|
|
|
|
|
|
|
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
|
|
|
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
println!("removed");
|
|
|
|
}
|
|
|
|
cli::SubCommand::List => {
|
|
|
|
for (i, task) in tasks.iter().enumerate() {
|
2022-08-05 12:35:14 +03:00
|
|
|
let idx = i + 1;
|
|
|
|
|
|
|
|
match current_task_info {
|
|
|
|
Some(CurrentTaskInfo { task_idx, .. }) if task_idx == idx => print!("> "),
|
|
|
|
_ => print!(" "),
|
|
|
|
}
|
|
|
|
|
|
|
|
print!("{}. ", idx);
|
2022-08-05 00:54:47 +03:00
|
|
|
if task.link.is_some() {
|
|
|
|
print!("(link) ");
|
|
|
|
}
|
|
|
|
println!("{}", task.name);
|
2022-08-05 00:08:27 +03:00
|
|
|
}
|
|
|
|
}
|
2022-08-05 10:42:35 +03:00
|
|
|
cli::SubCommand::Priority {
|
|
|
|
idx: target_idx,
|
|
|
|
priority,
|
|
|
|
} => {
|
2022-08-05 12:39:19 +03:00
|
|
|
if current_task_info.is_some() {
|
|
|
|
panic!("You can change priority only when you don't have an active task, yet");
|
|
|
|
}
|
|
|
|
|
2022-08-05 10:42:35 +03:00
|
|
|
if target_idx == 0 || target_idx > tasks.len() {
|
2022-08-05 12:35:14 +03:00
|
|
|
panic!("invalid index");
|
2022-08-05 10:42:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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() {
|
2022-08-05 12:35:14 +03:00
|
|
|
panic!("invalid index");
|
2022-08-05 10:42:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
let target = tasks.remove(target_idx - 1);
|
|
|
|
|
|
|
|
match priority {
|
|
|
|
cli::Priority::Before { .. } => {
|
|
|
|
tasks.insert(idx - 1, target);
|
|
|
|
}
|
|
|
|
cli::Priority::After { .. } => {
|
|
|
|
tasks.insert(idx, target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
|
|
|
|
file.write_all(&serde_json::to_vec(&tasks).unwrap())
|
|
|
|
.unwrap();
|
|
|
|
}
|
2022-08-05 12:35:14 +03:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2022-08-05 00:08:27 +03:00
|
|
|
println!("starting...");
|
|
|
|
println!("-- on start hook found");
|
|
|
|
println!("-- running...");
|
|
|
|
println!("> curl ...");
|
|
|
|
println!("-- status was changed to \"In progress\" successfuly");
|
2022-08-05 12:35:14 +03:00
|
|
|
*/
|
2022-08-05 00:08:27 +03:00
|
|
|
}
|
|
|
|
cli::SubCommand::Pause => {
|
|
|
|
println!("pausing...");
|
|
|
|
println!("paused");
|
|
|
|
}
|
|
|
|
cli::SubCommand::Finish => {
|
|
|
|
println!("finishing...");
|
|
|
|
println!("-- on finish hook found");
|
|
|
|
println!("-- running...");
|
|
|
|
println!("> curl ...");
|
|
|
|
println!("-- status was changed to \"Deploy\" successfuly");
|
|
|
|
println!("finished");
|
|
|
|
}
|
|
|
|
cli::SubCommand::Status => {
|
|
|
|
println!("Information about your current task:");
|
|
|
|
println!("[work]");
|
|
|
|
println!("https://redmine.example.com/issues/4051");
|
|
|
|
println!(
|
|
|
|
"#4051 Plannig/Assignment: Assign week and day general delvirebles after regular"
|
|
|
|
);
|
|
|
|
println!("-------------------------------------------------------------------------");
|
|
|
|
println!("lorem impsum dolor dolor impsum lorem lorem impsum");
|
|
|
|
println!("lorem dolor impsum lorem lorem impsum");
|
|
|
|
println!("lorem dolor impsum lorem lorem impsum lorem impsum dolor dolor impsum lorem");
|
|
|
|
println!("lorem impsum");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:35:14 +03:00
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
2022-08-05 00:08:27 +03:00
|
|
|
struct Task {
|
|
|
|
name: String,
|
|
|
|
link: Option<String>,
|
|
|
|
// created_at
|
|
|
|
}
|
2022-08-05 12:35:14 +03:00
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
struct CurrentTaskInfo {
|
|
|
|
task_idx: usize,
|
|
|
|
task: Task,
|
|
|
|
// started_at
|
|
|
|
}
|