extract-cli #9
11 changed files with 422 additions and 252 deletions
52
src/cli.rs
52
src/cli.rs
|
@ -14,6 +14,16 @@
|
|||
//! along with tas. If not, see <https://www.gnu.org/licenses/>.
|
||||
//!
|
||||
|
||||
pub mod add;
|
||||
pub mod edit;
|
||||
pub mod finish;
|
||||
pub mod list;
|
||||
pub mod pause;
|
||||
pub mod priority;
|
||||
pub mod remove;
|
||||
pub mod start;
|
||||
pub mod status;
|
||||
|
||||
#[derive(clap::Parser)]
|
||||
#[clap(author, version, about = "Experemental task manager")]
|
||||
pub struct Args {
|
||||
|
@ -23,45 +33,13 @@ pub struct Args {
|
|||
|
||||
#[derive(clap::Subcommand)]
|
||||
pub enum SubCommand {
|
||||
Add {
|
||||
#[clap(short, long)]
|
||||
link: Option<String>,
|
||||
|
||||
name: String,
|
||||
},
|
||||
Edit {
|
||||
#[clap(short, long)]
|
||||
link: Option<String>,
|
||||
#[clap(long)]
|
||||
no_link: bool,
|
||||
|
||||
#[clap(short, long)]
|
||||
name: Option<String>,
|
||||
|
||||
idx: usize,
|
||||
},
|
||||
Remove {
|
||||
idx: usize,
|
||||
},
|
||||
Priority {
|
||||
idx: usize,
|
||||
#[clap(subcommand)]
|
||||
priority: Priority,
|
||||
},
|
||||
Add(self::add::Args),
|
||||
Edit(self::edit::Args),
|
||||
Remove(self::remove::Args),
|
||||
Priority(self::priority::Args),
|
||||
List,
|
||||
Start {
|
||||
#[clap(short, long)]
|
||||
open: bool,
|
||||
|
||||
idx: Option<usize>,
|
||||
},
|
||||
Start(self::start::Args),
|
||||
Pause,
|
||||
Finish,
|
||||
Status,
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand)]
|
||||
pub enum Priority {
|
||||
Before { idx: usize },
|
||||
After { idx: usize },
|
||||
}
|
||||
|
|
27
src/cli/add.rs
Normal file
27
src/cli/add.rs
Normal file
|
@ -0,0 +1,27 @@
|
|||
use std::path::PathBuf;
|
||||
|
||||
use crate::Task;
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct Args {
|
||||
#[clap(short, long)]
|
||||
link: Option<String>,
|
||||
|
||||
name: String,
|
||||
}
|
||||
|
||||
pub struct Request {
|
||||
pub args: Args,
|
||||
pub tasks: Vec<Task>,
|
||||
pub tasks_file_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn execute(mut req: Request) {
|
||||
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())
|
||||
.unwrap();
|
||||
|
||||
println!("added");
|
||||
}
|
51
src/cli/edit.rs
Normal file
51
src/cli/edit.rs
Normal file
|
@ -0,0 +1,51 @@
|
|||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{CurrentTaskInfo, Task};
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct Args {
|
||||
#[clap(short, long)]
|
||||
link: Option<String>,
|
||||
#[clap(long)]
|
||||
no_link: bool,
|
||||
|
||||
#[clap(short, long)]
|
||||
name: Option<String>,
|
||||
|
||||
idx: usize,
|
||||
}
|
||||
|
||||
pub struct Request {
|
||||
pub args: Args,
|
||||
pub current_task_info: Option<CurrentTaskInfo>,
|
||||
pub tasks: Vec<Task>,
|
||||
pub tasks_file_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn execute(mut req: Request) {
|
||||
if req.current_task_info.is_some() {
|
||||
panic!("You can edit task only when you don't have an active task, yet");
|
||||
}
|
||||
|
||||
let idx = req.args.idx;
|
||||
if idx == 0 || idx > req.tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
let mut task = &mut req.tasks[idx - 1];
|
||||
if let Some(name) = req.args.name {
|
||||
task.name = name;
|
||||
}
|
||||
if let Some(link) = req.args.link {
|
||||
task.link = Some(link);
|
||||
} else if req.args.no_link {
|
||||
task.link = None;
|
||||
}
|
||||
|
||||
let mut file = std::fs::File::create(&req.tasks_file_path).unwrap();
|
||||
file.write_all(&serde_json::to_vec(&req.tasks).unwrap())
|
||||
.unwrap();
|
||||
|
||||
println!("changed");
|
||||
}
|
41
src/cli/finish.rs
Normal file
41
src/cli/finish.rs
Normal 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");
|
||||
}
|
||||
}
|
||||
}
|
23
src/cli/list.rs
Normal file
23
src/cli/list.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use crate::{CurrentTaskInfo, Task};
|
||||
|
||||
pub struct Request {
|
||||
pub tasks: Vec<Task>,
|
||||
pub current_task_info: Option<CurrentTaskInfo>,
|
||||
}
|
||||
|
||||
pub fn execute(req: Request) {
|
||||
for (i, task) in req.tasks.iter().enumerate() {
|
||||
let idx = i + 1;
|
||||
|
||||
match req.current_task_info {
|
||||
Some(CurrentTaskInfo { task_idx, .. }) if task_idx == idx => print!("> "),
|
||||
_ => print!(" "),
|
||||
}
|
||||
|
||||
print!("{}. ", idx);
|
||||
if task.link.is_some() {
|
||||
print!("(link) ");
|
||||
}
|
||||
println!("{}", task.name);
|
||||
}
|
||||
}
|
20
src/cli/pause.rs
Normal file
20
src/cli/pause.rs
Normal file
|
@ -0,0 +1,20 @@
|
|||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::CurrentTaskInfo;
|
||||
|
||||
pub struct Request {
|
||||
pub current_task_info: Option<CurrentTaskInfo>,
|
||||
pub current_task_info_file_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn execute(mut req: Request) {
|
||||
if req.current_task_info.take().is_none() {
|
||||
panic!("You can use the pause subcommand only when you have an active task");
|
||||
}
|
||||
|
||||
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!("paused");
|
||||
}
|
62
src/cli/priority.rs
Normal file
62
src/cli/priority.rs
Normal file
|
@ -0,0 +1,62 @@
|
|||
use crate::{CurrentTaskInfo, Task};
|
||||
|
||||
use std::cmp::Ordering;
|
||||
use std::io::Write;
|
||||
use std::path::PathBuf;
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct Args {
|
||||
idx: usize,
|
||||
#[clap(subcommand)]
|
||||
priority: Priority,
|
||||
}
|
||||
|
||||
#[derive(clap::Subcommand)]
|
||||
pub enum Priority {
|
||||
Before { idx: usize },
|
||||
After { idx: usize },
|
||||
}
|
||||
|
||||
pub struct Request {
|
||||
pub args: Args,
|
||||
pub tasks: Vec<Task>,
|
||||
pub current_task_info: Option<CurrentTaskInfo>,
|
||||
pub tasks_file_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn execute(mut req: Request) {
|
||||
if req.current_task_info.is_some() {
|
||||
panic!("You can change priority only when you don't have an active task, yet");
|
||||
}
|
||||
|
||||
let target_idx = req.args.idx;
|
||||
if target_idx == 0 || target_idx > req.tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
let idx = match req.args.priority {
|
||||
Priority::Before { idx } | Priority::After { idx } => match target_idx.cmp(&idx) {
|
||||
Ordering::Equal => return,
|
||||
Ordering::Less => idx - 1,
|
||||
Ordering::Greater => idx,
|
||||
},
|
||||
};
|
||||
if idx == 0 || idx > req.tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
let target = req.tasks.remove(target_idx - 1);
|
||||
|
||||
match req.args.priority {
|
||||
Priority::Before { .. } => {
|
||||
req.tasks.insert(idx - 1, target);
|
||||
}
|
||||
Priority::After { .. } => {
|
||||
req.tasks.insert(idx, target);
|
||||
}
|
||||
}
|
||||
|
||||
let mut file = std::fs::File::create(&req.tasks_file_path).unwrap();
|
||||
file.write_all(&serde_json::to_vec(&req.tasks).unwrap())
|
||||
.unwrap();
|
||||
}
|
56
src/cli/remove.rs
Normal file
56
src/cli/remove.rs
Normal file
|
@ -0,0 +1,56 @@
|
|||
use std::io::{BufRead, Write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{CurrentTaskInfo, Task};
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct Args {
|
||||
idx: usize,
|
||||
}
|
||||
|
||||
pub struct Request {
|
||||
pub args: Args,
|
||||
pub current_task_info: Option<CurrentTaskInfo>,
|
||||
pub tasks: Vec<Task>,
|
||||
pub tasks_file_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn execute(mut req: Request) {
|
||||
if req.current_task_info.is_some() {
|
||||
panic!("You can remove task only when you don't have an active task, yet");
|
||||
}
|
||||
|
||||
let idx = req.args.idx;
|
||||
if idx == 0 || idx > req.tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
let task = &req.tasks[idx - 1];
|
||||
println!("You are deleting task:");
|
||||
println!(" {}", task.name);
|
||||
if let Some(ref link) = task.link {
|
||||
println!(" link: {}", link);
|
||||
}
|
||||
|
||||
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."),
|
||||
}
|
||||
}
|
||||
|
||||
req.tasks.remove(idx - 1);
|
||||
|
||||
let mut file = std::fs::File::create(&req.tasks_file_path).unwrap();
|
||||
file.write_all(&serde_json::to_vec(&req.tasks).unwrap())
|
||||
.unwrap();
|
||||
|
||||
println!("removed");
|
||||
}
|
52
src/cli/start.rs
Normal file
52
src/cli/start.rs
Normal file
|
@ -0,0 +1,52 @@
|
|||
use crate::{CurrentTaskInfo, Task};
|
||||
use std::{io::Write, path::PathBuf};
|
||||
|
||||
#[derive(clap::Args)]
|
||||
pub struct Args {
|
||||
#[clap(short, long)]
|
||||
open: bool,
|
||||
|
||||
idx: Option<usize>,
|
||||
}
|
||||
|
||||
pub struct Request {
|
||||
pub args: Args,
|
||||
pub tasks: Vec<Task>,
|
||||
pub current_task_info: Option<CurrentTaskInfo>,
|
||||
pub current_task_info_file_path: PathBuf,
|
||||
}
|
||||
|
||||
pub fn execute(mut req: Request) {
|
||||
let idx = req.args.idx.unwrap_or(1);
|
||||
if idx == 0 || idx > req.tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
let task = &req.tasks[idx - 1];
|
||||
|
||||
req.current_task_info.replace(CurrentTaskInfo {
|
||||
task_idx: idx,
|
||||
task: task.clone(),
|
||||
});
|
||||
|
||||
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!("started");
|
||||
|
||||
if let (Some(link), true) = (task.link.as_ref(), req.args.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");
|
||||
*/
|
||||
}
|
16
src/cli/status.rs
Normal file
16
src/cli/status.rs
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
274
src/main.rs
274
src/main.rs
|
@ -28,10 +28,7 @@
|
|||
|
||||
use clap::Parser;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::{
|
||||
cmp::Ordering,
|
||||
io::{BufRead, Write},
|
||||
};
|
||||
use std::io::Write;
|
||||
use xdg::BaseDirectories;
|
||||
|
||||
mod cli;
|
||||
|
@ -49,240 +46,87 @@ fn main() {
|
|||
.unwrap_or_default();
|
||||
|
||||
let current_task_info_file_path = xdg_dirs.place_data_file("current.json").unwrap();
|
||||
let mut current_task_info: Option<CurrentTaskInfo> =
|
||||
let 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 {
|
||||
cli::SubCommand::Add { link, name } => {
|
||||
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())
|
||||
.unwrap();
|
||||
|
||||
println!("added");
|
||||
cli::SubCommand::Add(args) => {
|
||||
cli::add::execute(cli::add::Request {
|
||||
args,
|
||||
tasks,
|
||||
tasks_file_path,
|
||||
});
|
||||
}
|
||||
cli::SubCommand::Edit {
|
||||
idx,
|
||||
name,
|
||||
link,
|
||||
no_link,
|
||||
} => {
|
||||
if current_task_info.is_some() {
|
||||
panic!("You can edit task only when you don't have an active task, yet");
|
||||
}
|
||||
|
||||
if idx == 0 || idx > tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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::Edit(args) => {
|
||||
cli::edit::execute(cli::edit::Request {
|
||||
args,
|
||||
current_task_info,
|
||||
tasks,
|
||||
tasks_file_path,
|
||||
});
|
||||
}
|
||||
cli::SubCommand::Remove { idx } => {
|
||||
if current_task_info.is_some() {
|
||||
panic!("You can remove task only when you don't have an active task, yet");
|
||||
}
|
||||
|
||||
if idx == 0 || idx > tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
let task = &tasks[idx - 1];
|
||||
println!("You are deleting task:");
|
||||
println!(" {}", task.name);
|
||||
if let Some(ref link) = task.link {
|
||||
println!(" link: {}", link);
|
||||
}
|
||||
|
||||
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."),
|
||||
}
|
||||
}
|
||||
|
||||
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::Remove(args) => {
|
||||
cli::remove::execute(cli::remove::Request {
|
||||
args,
|
||||
current_task_info,
|
||||
tasks,
|
||||
tasks_file_path,
|
||||
});
|
||||
}
|
||||
cli::SubCommand::List => {
|
||||
for (i, task) in tasks.iter().enumerate() {
|
||||
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) ");
|
||||
}
|
||||
println!("{}", task.name);
|
||||
}
|
||||
}
|
||||
cli::SubCommand::Priority {
|
||||
idx: target_idx,
|
||||
priority,
|
||||
} => {
|
||||
if current_task_info.is_some() {
|
||||
panic!("You can change priority only when you don't have an active task, yet");
|
||||
}
|
||||
|
||||
if target_idx == 0 || target_idx > tasks.len() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
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() {
|
||||
panic!("invalid index");
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
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(),
|
||||
cli::list::execute(cli::list::Request {
|
||||
tasks,
|
||||
current_task_info,
|
||||
});
|
||||
}
|
||||
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,
|
||||
});
|
||||
|
||||
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");
|
||||
*/
|
||||
}
|
||||
cli::SubCommand::Pause => {
|
||||
if current_task_info.take().is_none() {
|
||||
panic!("You can use the pause subcommand only when you have an active task");
|
||||
}
|
||||
|
||||
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!("paused");
|
||||
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);
|
||||
}
|
||||
cli::SubCommand::Finish => match 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(&finished_tasks_file_path)
|
||||
.map(|file| serde_json::from_reader(file).unwrap())
|
||||
.unwrap_or_default();
|
||||
|
||||
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(¤t_task_info_file_path).unwrap();
|
||||
file.write_all(&serde_json::to_vec(¤t_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)]
|
||||
struct Task {
|
||||
pub struct Task {
|
||||
name: String,
|
||||
link: Option<String>,
|
||||
// created_at
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
struct CurrentTaskInfo {
|
||||
pub struct CurrentTaskInfo {
|
||||
task_idx: usize,
|
||||
task: Task,
|
||||
// started_at
|
||||
|
|
Loading…
Reference in a new issue