cli: move printing task detail to the util

This commit is contained in:
Dmitriy Pleshevskiy 2022-08-16 16:44:04 +03:00
parent 519162a8db
commit 7f8ddd8d2a
Signed by: pleshevskiy
GPG Key ID: 1B59187B161C0215
10 changed files with 40 additions and 48 deletions

View File

@ -14,6 +14,8 @@
//! along with tas. If not, see <https://www.gnu.org/licenses/>. //! along with tas. If not, see <https://www.gnu.org/licenses/>.
//! //!
use crate::domain;
pub mod add; pub mod add;
pub mod edit; pub mod edit;
pub mod finish; pub mod finish;
@ -43,3 +45,15 @@ pub enum SubCommand {
Finish(self::finish::Args), Finish(self::finish::Args),
Status, Status,
} }
pub fn print_task_detail(task: &domain::Task) {
print!(" ");
if let Some(group) = task.group.as_ref() {
print!("[{}]: ", group);
}
println!("{}", task.name);
if let Some(link) = task.link.as_ref() {
println!(" link: {}", link);
}
}

View File

@ -1,4 +1,7 @@
use crate::repo::{self, Repository}; use crate::{
cli::print_task_detail,
repo::{self, Repository},
};
#[derive(clap::Args)] #[derive(clap::Args)]
pub struct Args { pub struct Args {
@ -22,16 +25,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match res { match res {
Ok(task) => { Ok(task) => {
println!("The task was added successfully"); println!("The task was added successfully");
print_task_detail(&task);
print!(" ");
if let Some(group) = task.group {
print!("[{}]: ", group);
}
println!("{}", task.name);
if let Some(link) = task.link {
println!(" link: {}", link);
}
} }
Err(err) => { Err(err) => {
eprintln!("Cannot insert a new task: {}", err); eprintln!("Cannot insert a new task: {}", err);

View File

@ -1,3 +1,4 @@
use crate::cli::print_task_detail;
use crate::repo::{self, Repository}; use crate::repo::{self, Repository};
#[derive(clap::Args)] #[derive(clap::Args)]
@ -40,10 +41,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match res { match res {
Ok(task) => { Ok(task) => {
println!("The task was changed successfully"); println!("The task was changed successfully");
println!(" {}", task.name); print_task_detail(&task);
if let Some(link) = task.link {
println!(" link: {}", link);
}
} }
Err(err) => { Err(err) => {
eprintln!("Cannot update the task: {}", err); eprintln!("Cannot update the task: {}", err);

View File

@ -1,3 +1,4 @@
use crate::cli::print_task_detail;
use crate::repo::{self, Repository}; use crate::repo::{self, Repository};
#[derive(clap::Args)] #[derive(clap::Args)]
@ -16,10 +17,7 @@ pub fn execute(repo: impl Repository, args: Args) {
}; };
println!("The task was finished successfully"); println!("The task was finished successfully");
println!(" {}", task.name); print_task_detail(&task);
if let Some(link) = task.link.as_ref() {
println!(" link: {}", link);
}
if let (Some(link), true) = (task.link.as_ref(), args.open) { if let (Some(link), true) = (task.link.as_ref(), args.open) {
log::debug!("opening link..."); log::debug!("opening link...");

View File

@ -23,13 +23,10 @@ pub fn execute(repo: impl Repository) {
} }
print!("{}. ", idx); print!("{}. ", idx);
if let Some(group) = task.group.as_ref() { if let Some(group) = task.group.as_ref() {
print!("[{}]: ", group); print!("[{}]: ", group);
} }
print!("{}", task.name); print!("{}", task.name);
if task.link.is_some() { if task.link.is_some() {
print!(" (link)"); print!(" (link)");
} }

View File

@ -1,3 +1,4 @@
use crate::cli::print_task_detail;
use crate::repo::{self, Repository}; use crate::repo::{self, Repository};
pub fn execute(repo: impl Repository) { pub fn execute(repo: impl Repository) {
@ -14,10 +15,7 @@ pub fn execute(repo: impl Repository) {
match repo.stop_task() { match repo.stop_task() {
Ok(_) => { Ok(_) => {
println!("The task was paused successfully"); println!("The task was paused successfully");
println!(" {}", task.name); print_task_detail(&task);
if let Some(link) = task.link {
println!(" link: {}", link);
}
} }
Err(err) => { Err(err) => {
eprintln!("Cannot pause the task: {}", err); eprintln!("Cannot pause the task: {}", err);

View File

@ -1,3 +1,4 @@
use crate::cli::print_task_detail;
use crate::repo::{self, Repository}; use crate::repo::{self, Repository};
use std::cmp::Ordering; use std::cmp::Ordering;
@ -64,10 +65,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match res { match res {
Ok(task) => { Ok(task) => {
println!("The task was reordered successfully"); println!("The task was reordered successfully");
println!(" {}", task.name); print_task_detail(&task);
if let Some(link) = task.link {
println!(" link: {}", link);
}
} }
Err(err) => { Err(err) => {
eprintln!("Cannot reorder the task: {}", err); eprintln!("Cannot reorder the task: {}", err);

View File

@ -1,4 +1,7 @@
use crate::repo::{self, Repository}; use crate::{
cli::print_task_detail,
repo::{self, Repository},
};
use std::io::{BufRead, Write}; use std::io::{BufRead, Write};
#[derive(clap::Args)] #[derive(clap::Args)]
@ -24,10 +27,7 @@ pub fn execute(repo: impl Repository, args: Args) {
}; };
println!("You are deleting task:"); println!("You are deleting task:");
println!(" {}", task.name); print_task_detail(&task);
if let Some(ref link) = task.link {
println!(" link: {}", link);
}
println!("In most cases you need to `finish` command"); println!("In most cases you need to `finish` command");
loop { loop {
@ -46,10 +46,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match repo.remove_task(args.idx) { match repo.remove_task(args.idx) {
Ok(_) => { Ok(_) => {
println!("The task was removed successfully"); println!("The task was removed successfully");
println!(" {}", task.name); print_task_detail(&task);
if let Some(link) = task.link {
println!(" link: {}", link);
}
} }
Err(err) => { Err(err) => {
eprintln!("Cannot remove the task: {}", err); eprintln!("Cannot remove the task: {}", err);

View File

@ -1,4 +1,7 @@
use crate::repo::{self, Repository}; use crate::{
cli::print_task_detail,
repo::{self, Repository},
};
#[derive(clap::Args)] #[derive(clap::Args)]
pub struct Args { pub struct Args {
@ -16,10 +19,7 @@ pub fn execute(repo: impl Repository, args: Args) {
}; };
println!("The task was started successfully"); println!("The task was started successfully");
println!(" {}", task.name); print_task_detail(&task);
if let Some(link) = task.link.as_ref() {
println!(" link: {}", link);
}
if let (Some(link), true) = (task.link.as_ref(), args.open) { if let (Some(link), true) = (task.link.as_ref(), args.open) {
log::debug!("opening link..."); log::debug!("opening link...");

View File

@ -1,3 +1,4 @@
use crate::cli::print_task_detail;
use crate::repo::Repository; use crate::repo::Repository;
pub fn execute(repo: impl Repository) { pub fn execute(repo: impl Repository) {
@ -7,10 +8,7 @@ pub fn execute(repo: impl Repository) {
} }
Ok(Some(info)) => { Ok(Some(info)) => {
println!("Information about your current task:"); println!("Information about your current task:");
println!(" {}", info.task.name); print_task_detail(&info.task);
if let Some(link) = info.task.link {
println!(" link: {}", link);
}
} }
Err(err) => { Err(err) => {
eprintln!("Cannot read current task: {}", err); eprintln!("Cannot read current task: {}", err);