From 9a27c6c5eea5e1a7170d96969bab2ad8d9ad61fe Mon Sep 17 00:00:00 2001 From: Sunshine Date: Fri, 29 Jan 2021 20:24:35 -1000 Subject: [PATCH] add color to asset download log --- Cargo.lock | 23 ++++++++++++----------- Cargo.toml | 1 + src/opts.rs | 12 ++++++++++++ src/utils.rs | 17 ++++++++++++++--- 4 files changed, 39 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index bf82f34..85cbd00 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,9 +27,9 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "doc-comment 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", - "predicates 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)", - "predicates-core 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", - "predicates-tree 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-core 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-tree 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "wait-timeout 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -608,6 +608,7 @@ name = "monolith" version = "2.4.0" dependencies = [ "assert_cmd 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)", "base64 0.13.0 (registry+https://github.com/rust-lang/crates.io-index)", "chrono 0.4.19 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.33.3 (registry+https://github.com/rust-lang/crates.io-index)", @@ -858,24 +859,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "predicates" -version = "1.0.6" +version = "1.0.7" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "difference 2.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "predicates-core 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-core 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "predicates-core" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "predicates-tree" -version = "1.0.1" +version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "predicates-core 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "predicates-core 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "treeline 0.1.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -1755,9 +1756,9 @@ dependencies = [ "checksum pkg-config 0.3.19 (registry+https://github.com/rust-lang/crates.io-index)" = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c" "checksum ppv-lite86 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)" = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" "checksum precomputed-hash 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c" -"checksum predicates 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "73dd9b7b200044694dfede9edf907c1ca19630908443e9447e624993700c6932" -"checksum predicates-core 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "fb3dbeaaf793584e29c58c7e3a82bbb3c7c06b63cea68d13b0e3cddc124104dc" -"checksum predicates-tree 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aee95d988ee893cb35c06b148c80ed2cd52c8eea927f50ba7a0be1a786aeab73" +"checksum predicates 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "eeb433456c1a57cc93554dea3ce40b4c19c4057e41c55d4a0f3d84ea71c325aa" +"checksum predicates-core 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "57e35a3326b75e49aa85f5dc6ec15b41108cf5aee58eabb1f274dd18b73c2451" +"checksum predicates-tree 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "15f553275e5721409451eb85e15fd9a860a6e5ab4496eb215987502b5f5391f2" "checksum proc-macro-hack 0.5.19 (registry+https://github.com/rust-lang/crates.io-index)" = "dbf0c48bc1d91375ae5c3cd81e3722dff1abcf81a30960240640d223f59fe0e5" "checksum proc-macro2 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)" = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" "checksum quote 1.0.8 (registry+https://github.com/rust-lang/crates.io-index)" = "991431c3519a3f36861882da93630ce66b52918dcf1b8e2fd66b397fc96f28df" diff --git a/Cargo.toml b/Cargo.toml index eb5062d..f7a80c2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ include = [ license = "Unlicense" [dependencies] +atty = "0.2" # Used for highlighting network errors base64 = "0.13.0" chrono = "0.4.19" # Used for formatting creation timestamp clap = "2.33.3" diff --git a/src/opts.rs b/src/opts.rs index 84ee88c..15c3d3e 100644 --- a/src/opts.rs +++ b/src/opts.rs @@ -1,4 +1,5 @@ use clap::{App, Arg}; +use std::env; #[derive(Default)] pub struct Options { @@ -19,6 +20,7 @@ pub struct Options { pub user_agent: Option, pub no_video: bool, pub target: String, + pub no_color: bool, } const ASCII: &str = " \ @@ -33,6 +35,8 @@ const ASCII: &str = " \ const DEFAULT_NETWORK_TIMEOUT: u64 = 120; const DEFAULT_USER_AGENT: &str = "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:73.0) Gecko/20100101 Firefox/73.0"; +const ENV_VAR_NO_COLOR: &str = "NO_COLOR"; +const ENV_VAR_TERM: &str = "TERM"; impl Options { pub fn from_args() -> Options { @@ -98,6 +102,14 @@ impl Options { } options.no_video = app.is_present("no-video"); + options.no_color = + env::var_os(ENV_VAR_NO_COLOR).is_some() || atty::isnt(atty::Stream::Stderr); + if let Some(term) = env::var_os(ENV_VAR_TERM) { + if term == "dumb" { + options.no_color = true; + } + } + options } } diff --git a/src/utils.rs b/src/utils.rs index 5fd76be..ac9116a 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,8 +7,9 @@ use std::path::Path; use crate::opts::Options; use crate::url::{clean_url, file_url_to_fs_path, is_data_url, is_file_url, parse_data_url}; +const ANSI_COLOR_RED: &str = "\x1b[31m"; +const ANSI_COLOR_RESET: &str = "\x1b[0m"; const INDENT: &str = " "; - const MAGIC: [[&[u8]; 2]; 18] = [ // Image [b"GIF87a", b"image/gif"], @@ -32,7 +33,6 @@ const MAGIC: [[&[u8]; 2]; 18] = [ [b"....moov", b"video/quicktime"], [b"\x1A\x45\xDF\xA3", b"video/webm"], ]; - const PLAINTEXT_MEDIA_TYPES: &[&str] = &[ "image/svg+xml", "text/css", @@ -125,7 +125,18 @@ pub fn retrieve_asset( Ok(mut response) => { if !options.ignore_errors && response.status() != 200 { if !options.silent { - eprintln!("Unable to retrieve {} ({})", &url, response.status()); + eprintln!( + "{}{}{} ({}){}", + indent(depth).as_str(), + if options.no_color { "" } else { ANSI_COLOR_RED }, + &url, + response.status(), + if options.no_color { + "" + } else { + ANSI_COLOR_RESET + }, + ); } // Provoke error return Err(client.get("").send().unwrap_err());