2022-08-05 14:11:24 +03:00
|
|
|
//! Copyright (C) 2022, Dmitriy Pleshevskiy <dmitriy@ideascup.me>
|
|
|
|
//!
|
|
|
|
//! tas is free software: you can redistribute it and/or modify
|
|
|
|
//! it under the terms of the GNU General Public License as published by
|
|
|
|
//! the Free Software Foundation, either version 3 of the License, or
|
|
|
|
//! (at your option) any later version.
|
|
|
|
//!
|
|
|
|
//! tas is distributed in the hope that it will be useful,
|
|
|
|
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
//! GNU General Public License for more details.
|
|
|
|
//!
|
|
|
|
//! You should have received a copy of the GNU General Public License
|
|
|
|
//! along with tas. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
//!
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
// Rustc lints
|
|
|
|
#![deny(
|
|
|
|
unused_imports,
|
|
|
|
unused_qualifications,
|
|
|
|
rust_2018_idioms,
|
|
|
|
unsafe_code,
|
|
|
|
non_ascii_idents
|
|
|
|
)]
|
|
|
|
// Clippy lints
|
|
|
|
#![deny(clippy::all)]
|
|
|
|
//---------------------------------------------------------------------
|
|
|
|
|
2022-08-05 00:08:27 +03:00
|
|
|
use clap::Parser;
|
|
|
|
use serde::{Deserialize, Serialize};
|
2022-08-05 18:32:16 +03:00
|
|
|
use std::io::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();
|
|
|
|
|
2022-08-05 13:06:48 +03:00
|
|
|
let finished_tasks_file_path = xdg_dirs.place_data_file("finished_data.json").unwrap();
|
|
|
|
|
2022-08-05 00:08:27 +03:00
|
|
|
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();
|
2022-08-05 18:25:01 +03:00
|
|
|
let current_task_info: Option<CurrentTaskInfo> =
|
2022-08-05 12:35:14 +03:00
|
|
|
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() {
|
2022-08-05 13:06:48 +03:00
|
|
|
panic!("You can edit task only when you don't have an active task, yet");
|
2022-08-05 12:39:19 +03:00
|
|
|
}
|
|
|
|
|
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");
|
|
|
|
}
|
2022-08-05 18:32:16 +03:00
|
|
|
cli::SubCommand::Remove(args) => {
|
|
|
|
cli::remove::execute(cli::remove::Request {
|
|
|
|
args,
|
|
|
|
current_task_info,
|
|
|
|
tasks,
|
|
|
|
tasks_file_path,
|
|
|
|
});
|
2022-08-05 00:08:27 +03:00
|
|
|
}
|
|
|
|
cli::SubCommand::List => {
|
2022-08-05 18:25:01 +03:00
|
|
|
cli::list::execute(cli::list::Request {
|
|
|
|
tasks,
|
|
|
|
current_task_info,
|
|
|
|
});
|
2022-08-05 00:08:27 +03:00
|
|
|
}
|
2022-08-05 18:19:12 +03:00
|
|
|
cli::SubCommand::Priority(args) => {
|
|
|
|
cli::priority::execute(cli::priority::Request {
|
|
|
|
args,
|
|
|
|
tasks,
|
|
|
|
tasks_file_path,
|
|
|
|
current_task_info,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cli::SubCommand::Start(args) => {
|
|
|
|
cli::start::execute(cli::start::Request {
|
|
|
|
args,
|
|
|
|
tasks,
|
|
|
|
current_task_info,
|
|
|
|
current_task_info_file_path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cli::SubCommand::Pause => {
|
|
|
|
cli::pause::execute(cli::pause::Request {
|
|
|
|
current_task_info,
|
|
|
|
current_task_info_file_path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cli::SubCommand::Finish => {
|
|
|
|
cli::finish::execute(cli::finish::Request {
|
|
|
|
tasks,
|
|
|
|
current_task_info,
|
|
|
|
current_task_info_file_path,
|
|
|
|
tasks_file_path,
|
|
|
|
finished_tasks_file_path,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
cli::SubCommand::Status => {
|
|
|
|
cli::status::execute(current_task_info);
|
2022-08-05 10:42:35 +03:00
|
|
|
}
|
2022-08-05 00:08:27 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-05 12:35:14 +03:00
|
|
|
#[derive(Deserialize, Serialize, Clone)]
|
2022-08-05 16:48:22 +03:00
|
|
|
pub struct Task {
|
2022-08-05 00:08:27 +03:00
|
|
|
name: String,
|
|
|
|
link: Option<String>,
|
|
|
|
// created_at
|
|
|
|
}
|
2022-08-05 12:35:14 +03:00
|
|
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
2022-08-05 16:48:22 +03:00
|
|
|
pub struct CurrentTaskInfo {
|
2022-08-05 12:35:14 +03:00
|
|
|
task_idx: usize,
|
|
|
|
task: Task,
|
|
|
|
// started_at
|
|
|
|
}
|