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/>.
//!
use crate::domain;
pub mod add;
pub mod edit;
pub mod finish;
@ -43,3 +45,15 @@ pub enum SubCommand {
Finish(self::finish::Args),
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)]
pub struct Args {
@ -22,16 +25,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match res {
Ok(task) => {
println!("The task was added successfully");
print!(" ");
if let Some(group) = task.group {
print!("[{}]: ", group);
}
println!("{}", task.name);
if let Some(link) = task.link {
println!(" link: {}", link);
}
print_task_detail(&task);
}
Err(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};
#[derive(clap::Args)]
@ -40,10 +41,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match res {
Ok(task) => {
println!("The task was changed successfully");
println!(" {}", task.name);
if let Some(link) = task.link {
println!(" link: {}", link);
}
print_task_detail(&task);
}
Err(err) => {
eprintln!("Cannot update the task: {}", err);

View file

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

View file

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

View file

@ -1,3 +1,4 @@
use crate::cli::print_task_detail;
use crate::repo::{self, Repository};
pub fn execute(repo: impl Repository) {
@ -14,10 +15,7 @@ pub fn execute(repo: impl Repository) {
match repo.stop_task() {
Ok(_) => {
println!("The task was paused successfully");
println!(" {}", task.name);
if let Some(link) = task.link {
println!(" link: {}", link);
}
print_task_detail(&task);
}
Err(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 std::cmp::Ordering;
@ -64,10 +65,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match res {
Ok(task) => {
println!("The task was reordered successfully");
println!(" {}", task.name);
if let Some(link) = task.link {
println!(" link: {}", link);
}
print_task_detail(&task);
}
Err(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};
#[derive(clap::Args)]
@ -24,10 +27,7 @@ pub fn execute(repo: impl Repository, args: Args) {
};
println!("You are deleting task:");
println!(" {}", task.name);
if let Some(ref link) = task.link {
println!(" link: {}", link);
}
print_task_detail(&task);
println!("In most cases you need to `finish` command");
loop {
@ -46,10 +46,7 @@ pub fn execute(repo: impl Repository, args: Args) {
match repo.remove_task(args.idx) {
Ok(_) => {
println!("The task was removed successfully");
println!(" {}", task.name);
if let Some(link) = task.link {
println!(" link: {}", link);
}
print_task_detail(&task);
}
Err(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)]
pub struct Args {
@ -16,10 +19,7 @@ pub fn execute(repo: impl Repository, args: Args) {
};
println!("The task was started successfully");
println!(" {}", task.name);
if let Some(link) = task.link.as_ref() {
println!(" link: {}", link);
}
print_task_detail(&task);
if let (Some(link), true) = (task.link.as_ref(), args.open) {
log::debug!("opening link...");

View file

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