From 6c1def1a2dea05a941d8d14b6a2988c4b2a50d47 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Fri, 5 Aug 2022 18:25:01 +0300 Subject: [PATCH] cli: extract list subcommand --- src/cli.rs | 1 + src/cli/list.rs | 23 +++++++++++++++++++++++ src/cli/pause.rs | 1 + src/cli/priority.rs | 2 +- src/cli/start.rs | 8 ++++---- src/main.rs | 25 ++++++------------------- 6 files changed, 36 insertions(+), 24 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index 94161e6..24f44ed 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -15,6 +15,7 @@ //! pub mod finish; +pub mod list; pub mod pause; pub mod priority; pub mod start; diff --git a/src/cli/list.rs b/src/cli/list.rs index e69de29..818bdae 100644 --- a/src/cli/list.rs +++ b/src/cli/list.rs @@ -0,0 +1,23 @@ +use crate::{CurrentTaskInfo, Task}; + +pub struct Request { + pub tasks: Vec, + pub current_task_info: Option, +} + +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); + } +} diff --git a/src/cli/pause.rs b/src/cli/pause.rs index 221a356..d1b34b2 100644 --- a/src/cli/pause.rs +++ b/src/cli/pause.rs @@ -1,3 +1,4 @@ +use std::io::Write; use std::path::PathBuf; use crate::CurrentTaskInfo; diff --git a/src/cli/priority.rs b/src/cli/priority.rs index 2382702..23d08d0 100644 --- a/src/cli/priority.rs +++ b/src/cli/priority.rs @@ -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"); } diff --git a/src/cli/start.rs b/src/cli/start.rs index 43f6386..4e05a42 100644 --- a/src/cli/start.rs +++ b/src/cli/start.rs @@ -10,10 +10,10 @@ pub struct Args { } pub struct Request { - args: Args, - tasks: Vec, - current_task_info: Option, - current_task_info_file_path: PathBuf, + pub args: Args, + pub tasks: Vec, + pub current_task_info: Option, + pub current_task_info_file_path: PathBuf, } pub fn execute(mut req: Request) { diff --git a/src/main.rs b/src/main.rs index 200a5f9..cec3bd0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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 = + let current_task_info: Option = std::fs::File::open(¤t_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 {