cli: extract list subcommand

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-05 18:25:01 +03:00
parent 04c25e2589
commit 6c1def1a2d
Signed by: pleshevskiy
GPG key ID: 1B59187B161C0215
6 changed files with 36 additions and 24 deletions

View file

@ -15,6 +15,7 @@
//!
pub mod finish;
pub mod list;
pub mod pause;
pub mod priority;
pub mod start;

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

View file

@ -1,3 +1,4 @@
use std::io::Write;
use std::path::PathBuf;
use crate::CurrentTaskInfo;

View file

@ -24,7 +24,7 @@ pub struct Request {
pub tasks_file_path: PathBuf,
}
pub fn execute(req: Request) {
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");
}

View file

@ -10,10 +10,10 @@ pub struct Args {
}
pub struct Request {
args: Args,
tasks: Vec<Task>,
current_task_info: Option<CurrentTaskInfo>,
current_task_info_file_path: PathBuf,
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) {

View file

@ -28,10 +28,7 @@
use clap::Parser;
use serde::{Deserialize, Serialize};
use std::{
cmp::Ordering,
io::{BufRead, Write},
};
use std::io::{BufRead, Write};
use xdg::BaseDirectories;
mod cli;
@ -49,7 +46,7 @@ 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(&current_task_info_file_path)
.map(|file| serde_json::from_reader(file).unwrap())
.unwrap_or_default();
@ -133,20 +130,10 @@ fn main() {
println!("removed");
}
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::list::execute(cli::list::Request {
tasks,
current_task_info,
});
}
cli::SubCommand::Priority(args) => {
cli::priority::execute(cli::priority::Request {