cli: extract status and finish subcommands

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-05 16:48:22 +03:00
parent c2894a0f2b
commit 4abf8a9341
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
11 changed files with 70 additions and 41 deletions

View file

@ -14,6 +14,9 @@
//! along with tas. If not, see <https://www.gnu.org/licenses/>. //! along with tas. If not, see <https://www.gnu.org/licenses/>.
//! //!
pub mod finish;
pub mod status;
#[derive(clap::Parser)] #[derive(clap::Parser)]
#[clap(author, version, about = "Experemental task manager")] #[clap(author, version, about = "Experemental task manager")]
pub struct Args { pub struct Args {

0
src/cli/add.rs Normal file
View file

0
src/cli/edit.rs Normal file
View file

41
src/cli/finish.rs Normal file
View file

@ -0,0 +1,41 @@
use crate::{CurrentTaskInfo, Task};
use std::io::Write;
use std::path::PathBuf;
pub struct Request {
pub current_task_info: Option<CurrentTaskInfo>,
pub tasks: Vec<Task>,
pub finished_tasks_file_path: PathBuf,
pub tasks_file_path: PathBuf,
pub current_task_info_file_path: PathBuf,
}
pub fn execute(mut req: Request) {
match req.current_task_info.take() {
None => {
panic!("You can use the finish subcommand only when you have an active task")
}
Some(info) => {
let mut finished_tasks: Vec<Task> = std::fs::File::open(&req.finished_tasks_file_path)
.map(|file| serde_json::from_reader(file).unwrap())
.unwrap_or_default();
let task = req.tasks.remove(info.task_idx - 1);
finished_tasks.push(task);
let mut file = std::fs::File::create(req.tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&req.tasks).unwrap())
.unwrap();
let mut file = std::fs::File::create(req.finished_tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&finished_tasks).unwrap())
.unwrap();
let mut file = std::fs::File::create(req.current_task_info_file_path).unwrap();
file.write_all(&serde_json::to_vec(&req.current_task_info).unwrap())
.unwrap();
println!("finished");
}
}
}

0
src/cli/list.rs Normal file
View file

0
src/cli/pause.rs Normal file
View file

0
src/cli/priority.rs Normal file
View file

0
src/cli/remove.rs Normal file
View file

0
src/cli/start.rs Normal file
View file

16
src/cli/status.rs Normal file
View file

@ -0,0 +1,16 @@
use crate::CurrentTaskInfo;
pub fn execute(current_task_info: Option<CurrentTaskInfo>) {
match current_task_info {
None => {
panic!("You don't have an active task.");
}
Some(info) => {
println!("Information about your current task:");
println!(" {}", info.task.name);
if let Some(link) = info.task.link {
println!(" link: {}", link);
}
}
}
}

View file

@ -232,57 +232,26 @@ fn main() {
.unwrap(); .unwrap();
println!("paused"); println!("paused");
} }
cli::SubCommand::Finish => match current_task_info.take() { cli::SubCommand::Finish => cli::finish::execute(cli::finish::Request {
None => { tasks,
panic!("You can use the finish subcommand only when you have an active task") current_task_info,
} current_task_info_file_path,
Some(info) => { tasks_file_path,
let mut finished_tasks: Vec<Task> = std::fs::File::open(&finished_tasks_file_path) finished_tasks_file_path,
.map(|file| serde_json::from_reader(file).unwrap()) }),
.unwrap_or_default(); cli::SubCommand::Status => cli::status::execute(current_task_info),
let task = tasks.remove(info.task_idx - 1);
finished_tasks.push(task);
let mut file = std::fs::File::create(&tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&tasks).unwrap())
.unwrap();
let mut file = std::fs::File::create(&finished_tasks_file_path).unwrap();
file.write_all(&serde_json::to_vec(&finished_tasks).unwrap())
.unwrap();
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!("finished");
}
},
cli::SubCommand::Status => match current_task_info {
None => {
panic!("You don't have an active task.");
}
Some(info) => {
println!("Information about your current task:");
println!(" {}", info.task.name);
if let Some(link) = info.task.link {
println!(" link: {}", link);
}
}
},
} }
} }
#[derive(Deserialize, Serialize, Clone)] #[derive(Deserialize, Serialize, Clone)]
struct Task { pub struct Task {
name: String, name: String,
link: Option<String>, link: Option<String>,
// created_at // created_at
} }
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
struct CurrentTaskInfo { pub struct CurrentTaskInfo {
task_idx: usize, task_idx: usize,
task: Task, task: Task,
// started_at // started_at