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 finish;
pub mod list;
pub mod pause; pub mod pause;
pub mod priority; pub mod priority;
pub mod start; 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 std::path::PathBuf;
use crate::CurrentTaskInfo; use crate::CurrentTaskInfo;

View file

@ -24,7 +24,7 @@ pub struct Request {
pub tasks_file_path: PathBuf, pub tasks_file_path: PathBuf,
} }
pub fn execute(req: Request) { pub fn execute(mut req: Request) {
if req.current_task_info.is_some() { if req.current_task_info.is_some() {
panic!("You can change priority only when you don't have an active task, yet"); 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 { pub struct Request {
args: Args, pub args: Args,
tasks: Vec<Task>, pub tasks: Vec<Task>,
current_task_info: Option<CurrentTaskInfo>, pub current_task_info: Option<CurrentTaskInfo>,
current_task_info_file_path: PathBuf, pub current_task_info_file_path: PathBuf,
} }
pub fn execute(mut req: Request) { pub fn execute(mut req: Request) {

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::{BufRead, Write};
cmp::Ordering,
io::{BufRead, Write},
};
use xdg::BaseDirectories; use xdg::BaseDirectories;
mod cli; mod cli;
@ -49,7 +46,7 @@ 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();
@ -133,20 +130,10 @@ fn main() {
println!("removed"); 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!(" "),
}
print!("{}. ", idx);
if task.link.is_some() {
print!("(link) ");
}
println!("{}", task.name);
}
} }
cli::SubCommand::Priority(args) => { cli::SubCommand::Priority(args) => {
cli::priority::execute(cli::priority::Request { cli::priority::execute(cli::priority::Request {