Add colored output and configuring of a paperoni root directory for logs

This commit is contained in:
Kenneth Gitere 2021-04-24 15:13:44 +03:00
parent 65f8ebda56
commit a9787d7b5a
4 changed files with 112 additions and 23 deletions

58
Cargo.lock generated
View file

@ -406,6 +406,17 @@ dependencies = [
"vec_map", "vec_map",
] ]
[[package]]
name = "colored"
version = "2.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd"
dependencies = [
"atty",
"lazy_static",
"winapi",
]
[[package]] [[package]]
name = "comfy-table" name = "comfy-table"
version = "2.1.0" version = "2.1.0"
@ -639,6 +650,26 @@ dependencies = [
"generic-array", "generic-array",
] ]
[[package]]
name = "directories"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e69600ff1703123957937708eb27f7a564e48885c537782722ed0ba3189ce1d7"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "03d86534ed367a67548dc68113a0f5db55432fdfbb6e6f9d77704397d95d5780"
dependencies = [
"libc",
"redox_users",
"winapi",
]
[[package]] [[package]]
name = "discard" name = "discard"
version = "1.0.4" version = "1.0.4"
@ -925,6 +956,17 @@ dependencies = [
"wasi 0.9.0+wasi-snapshot-preview1", "wasi 0.9.0+wasi-snapshot-preview1",
] ]
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if 1.0.0",
"libc",
"wasi 0.10.0+wasi-snapshot-preview1",
]
[[package]] [[package]]
name = "ghash" name = "ghash"
version = "0.3.0" version = "0.3.0"
@ -1445,7 +1487,9 @@ dependencies = [
"async-std", "async-std",
"atty", "atty",
"clap", "clap",
"colored",
"comfy-table", "comfy-table",
"directories",
"epub-builder", "epub-builder",
"flexi_logger", "flexi_logger",
"futures", "futures",
@ -1680,7 +1724,7 @@ version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
dependencies = [ dependencies = [
"getrandom", "getrandom 0.1.15",
"libc", "libc",
"rand_chacha", "rand_chacha",
"rand_core 0.5.1", "rand_core 0.5.1",
@ -1719,7 +1763,7 @@ version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
dependencies = [ dependencies = [
"getrandom", "getrandom 0.1.15",
] ]
[[package]] [[package]]
@ -1764,6 +1808,16 @@ dependencies = [
"bitflags", "bitflags",
] ]
[[package]]
name = "redox_users"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
dependencies = [
"getrandom 0.2.2",
"redox_syscall 0.2.6",
]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.4.5" version = "1.4.5"

View file

@ -15,7 +15,9 @@ readme = "README.md"
async-std = "1.9.0" async-std = "1.9.0"
atty = "0.2.14" atty = "0.2.14"
clap = "2.33.3" clap = "2.33.3"
colored = "2.0.0"
comfy-table = "2.1.0" comfy-table = "2.1.0"
directories = "3.0.2"
epub-builder = "0.4.8" epub-builder = "0.4.8"
flexi_logger = "0.17.1" flexi_logger = "0.17.1"
futures = "0.3.14" futures = "0.3.14"

View file

@ -1,5 +1,6 @@
use colored::*;
use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY; use comfy_table::presets::UTF8_HORIZONTAL_BORDERS_ONLY;
use comfy_table::{Attribute, Cell, CellAlignment, ContentArrangement, Table}; use comfy_table::{Cell, CellAlignment, ContentArrangement, Table};
use log::error; use log::error;
use crate::errors::PaperoniError; use crate::errors::PaperoniError;
@ -18,17 +19,14 @@ pub fn display_summary(
successfully_downloaded_count, successfully_downloaded_count,
errors.len() errors.len()
) )
.bold()
); );
if successfully_downloaded_count > 0 { if successfully_downloaded_count > 0 {
println!("{}", succesful_articles_table); println!("{}", succesful_articles_table);
} }
if !errors.is_empty() { if !errors.is_empty() {
println!( println!("\n{}", "Failed article downloads".bright_red().bold());
"{}Failed article downloads{}",
Attribute::Bold,
Attribute::NormalIntensity
);
let mut table_failed = Table::new(); let mut table_failed = Table::new();
table_failed table_failed
.load_preset(UTF8_HORIZONTAL_BORDERS_ONLY) .load_preset(UTF8_HORIZONTAL_BORDERS_ONLY)
@ -56,10 +54,14 @@ fn short_summary(initial_count: usize, successful_count: usize, failed_count: us
panic!("initial_count must be equal to the sum of failed and successful count") panic!("initial_count must be equal to the sum of failed and successful count")
} }
let get_noun = |count: usize| if count == 1 { "article" } else { "articles" }; let get_noun = |count: usize| if count == 1 { "article" } else { "articles" };
if successful_count == initial_count { if successful_count == initial_count && successful_count == 1 {
"All articles downloaded successfully".into() "Article downloaded successfully".green().to_string()
} else if initial_count == failed_count && failed_count == 1 {
"Article failed to download".red().to_string()
} else if successful_count == initial_count {
"All articles downloaded successfully".green().to_string()
} else if successful_count == 0 { } else if successful_count == 0 {
"All articles failed to download".into() "All articles failed to download".red().to_string()
} else { } else {
format!( format!(
"{} {} downloaded successfully, {} {} failed", "{} {} downloaded successfully, {} {} failed",
@ -68,33 +70,50 @@ fn short_summary(initial_count: usize, successful_count: usize, failed_count: us
failed_count, failed_count,
get_noun(failed_count) get_noun(failed_count)
) )
.yellow()
.to_string()
} }
} }
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::short_summary; use super::short_summary;
use colored::*;
#[test] #[test]
fn test_short_summary() { fn test_short_summary() {
assert_eq!(
short_summary(1, 1, 0),
"Article downloaded successfully".green().to_string()
);
assert_eq!(
short_summary(1, 0, 1),
"Article failed to download".red().to_string()
);
assert_eq!( assert_eq!(
short_summary(10, 10, 0), short_summary(10, 10, 0),
"All articles downloaded successfully".to_string() "All articles downloaded successfully".green().to_string()
); );
assert_eq!( assert_eq!(
short_summary(10, 0, 10), short_summary(10, 0, 10),
"All articles failed to download".to_string() "All articles failed to download".red().to_string()
); );
assert_eq!( assert_eq!(
short_summary(10, 8, 2), short_summary(10, 8, 2),
"8 articles downloaded successfully, 2 articles failed".to_string() "8 articles downloaded successfully, 2 articles failed"
.yellow()
.to_string()
); );
assert_eq!( assert_eq!(
short_summary(10, 1, 9), short_summary(10, 1, 9),
"1 article downloaded successfully, 9 articles failed".to_string() "1 article downloaded successfully, 9 articles failed"
.yellow()
.to_string()
); );
assert_eq!( assert_eq!(
short_summary(7, 6, 1), short_summary(7, 6, 1),
"6 articles downloaded successfully, 1 article failed".to_string() "6 articles downloaded successfully, 1 article failed"
.yellow()
.to_string()
); );
} }

View file

@ -5,6 +5,7 @@ use async_std::stream;
use async_std::task; use async_std::task;
use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY}; use comfy_table::presets::{UTF8_FULL, UTF8_HORIZONTAL_BORDERS_ONLY};
use comfy_table::{ContentArrangement, Table}; use comfy_table::{ContentArrangement, Table};
use directories::UserDirs;
use futures::stream::StreamExt; use futures::stream::StreamExt;
use indicatif::{ProgressBar, ProgressStyle}; use indicatif::{ProgressBar, ProgressStyle};
use log::{debug, warn}; use log::{debug, warn};
@ -30,13 +31,26 @@ fn main() {
let app_config = cli::cli_init(); let app_config = cli::cli_init();
if !app_config.urls().is_empty() { if !app_config.urls().is_empty() {
match flexi_logger::Logger::with_str("paperoni=debug") match UserDirs::new() {
.log_to_file() Some(user_dirs) => {
.start() let home_dir = user_dirs.home_dir();
{ let paperoni_dir = home_dir.join(".paperoni");
Ok(_) => (), let log_dir = paperoni_dir.join("logs");
Err(e) => eprintln!("Unable to start logger!\n{}", e), if !paperoni_dir.is_dir() || !log_dir.is_dir() {
} std::fs::create_dir_all(&log_dir)
.expect("Unable to create paperoni directories on home directory for logging purposes");
}
match flexi_logger::Logger::with_str("paperoni=debug")
.directory(log_dir)
.log_to_file()
.start()
{
Ok(_) => (),
Err(e) => eprintln!("Unable to start logger!\n{}", e),
}
}
None => eprintln!("Unable to get user directories for logging purposes"),
};
download(app_config); download(app_config);
} }
} }