extract-cli #9

Merged
pleshevskiy merged 7 commits from extract-cli into main 2022-08-05 22:07:16 +03:00
11 changed files with 422 additions and 252 deletions

View file

@ -14,6 +14,16 @@
//! along with tas. If not, see <https://www.gnu.org/licenses/>. //! 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)] #[derive(clap::Parser)]
#[clap(author, version, about = "Experemental task manager")] #[clap(author, version, about = "Experemental task manager")]
pub struct Args { pub struct Args {
@ -23,45 +33,13 @@ pub struct Args {
#[derive(clap::Subcommand)] #[derive(clap::Subcommand)]
pub enum SubCommand { pub enum SubCommand {
Add { Add(self::add::Args),
#[clap(short, long)] Edit(self::edit::Args),
link: Option<String>, Remove(self::remove::Args),
Priority(self::priority::Args),
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,
},
List, List,
Start { Start(self::start::Args),
#[clap(short, long)]
open: bool,
idx: Option<usize>,
},
Pause, Pause,
Finish, Finish,
Status, Status,
} }
#[derive(clap::Subcommand)]
pub enum Priority {
Before { idx: usize },
After { idx: usize },
}

27
src/cli/add.rs Normal file
View 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
View 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
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");
}
}
}

23
src/cli/list.rs Normal file
View 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
View 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
View 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
View 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
View 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
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

@ -28,10 +28,7 @@
use clap::Parser; use clap::Parser;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::{ use std::io::Write;
cmp::Ordering,
io::{BufRead, Write},
};
use xdg::BaseDirectories; use xdg::BaseDirectories;
mod cli; mod cli;
@ -49,240 +46,87 @@ fn main() {
.unwrap_or_default(); .unwrap_or_default();
let current_task_info_file_path = xdg_dirs.place_data_file("current.json").unwrap(); 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(&current_task_info_file_path) std::fs::File::open(&current_task_info_file_path)
.map(|file| serde_json::from_reader(file).unwrap()) .map(|file| serde_json::from_reader(file).unwrap())
.unwrap_or_default(); .unwrap_or_default();
match args.command { match args.command {
cli::SubCommand::Add { link, name } => { cli::SubCommand::Add(args) => {
tasks.push(Task { name, link }); cli::add::execute(cli::add::Request {
args,
let mut file = std::fs::File::create(&tasks_file_path).unwrap(); tasks,
file.write_all(&serde_json::to_vec(&tasks).unwrap()) tasks_file_path,
.unwrap(); });
println!("added");
} }
cli::SubCommand::Edit { cli::SubCommand::Edit(args) => {
idx, cli::edit::execute(cli::edit::Request {
name, args,
link, current_task_info,
no_link, tasks,
} => { tasks_file_path,
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::Remove { idx } => { cli::SubCommand::Remove(args) => {
if current_task_info.is_some() { cli::remove::execute(cli::remove::Request {
panic!("You can remove task only when you don't have an active task, yet"); args,
} current_task_info,
tasks,
if idx == 0 || idx > tasks.len() { tasks_file_path,
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::List => { cli::SubCommand::List => {
for (i, task) in tasks.iter().enumerate() { cli::list::execute(cli::list::Request {
let idx = i + 1; tasks,
current_task_info,
match current_task_info { });
Some(CurrentTaskInfo { task_idx, .. }) if task_idx == idx => print!("> "), }
_ => print!(" "), cli::SubCommand::Priority(args) => {
} cli::priority::execute(cli::priority::Request {
args,
print!("{}. ", idx); tasks,
if task.link.is_some() { tasks_file_path,
print!("(link) "); current_task_info,
} });
println!("{}", task.name); }
} cli::SubCommand::Start(args) => {
} cli::start::execute(cli::start::Request {
cli::SubCommand::Priority { args,
idx: target_idx, tasks,
priority, current_task_info,
} => { current_task_info_file_path,
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(),
}); });
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!("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 => { cli::SubCommand::Pause => {
if current_task_info.take().is_none() { cli::pause::execute(cli::pause::Request {
panic!("You can use the pause subcommand only when you have an active task"); current_task_info,
} current_task_info_file_path,
});
let mut file = std::fs::File::create(&current_task_info_file_path).unwrap(); }
file.write_all(&serde_json::to_vec(&current_task_info).unwrap()) cli::SubCommand::Finish => {
.unwrap(); cli::finish::execute(cli::finish::Request {
println!("paused"); 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(&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