extract-cli #9

Merged
pleshevskiy merged 7 commits from extract-cli into main 2022-08-05 22:07:16 +03:00
3 changed files with 60 additions and 40 deletions
Showing only changes of commit ec880dad6e - Show all commits

View file

@ -14,6 +14,7 @@
//! along with tas. If not, see <https://www.gnu.org/licenses/>. //! along with tas. If not, see <https://www.gnu.org/licenses/>.
//! //!
pub mod edit;
pub mod finish; pub mod finish;
pub mod list; pub mod list;
pub mod pause; pub mod pause;
@ -37,17 +38,7 @@ pub enum SubCommand {
name: String, name: String,
}, },
Edit { Edit(self::edit::Args),
#[clap(short, long)]
link: Option<String>,
#[clap(long)]
no_link: bool,
#[clap(short, long)]
name: Option<String>,
idx: usize,
},
Remove(self::remove::Args), Remove(self::remove::Args),
Priority(self::priority::Args), Priority(self::priority::Args),
List, List,

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");
}

View file

@ -61,35 +61,13 @@ fn main() {
println!("added"); 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(args) => { cli::SubCommand::Remove(args) => {
cli::remove::execute(cli::remove::Request { cli::remove::execute(cli::remove::Request {