From bb34936c2ffd5e4caaea92a91bc132ee117021ed Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Thu, 12 Mar 2020 01:08:40 -0700 Subject: [PATCH] Only write spinner and progress bar when connected to terminal type: fixed --- src/env.rs | 9 +++++++++ src/hasher.rs | 10 ++++++---- src/main.rs | 1 + src/subcommand/torrent/create.rs | 32 ++++++++++++++++++++------------ src/test_env_builder.rs | 1 + src/walker.rs | 7 ++----- 6 files changed, 39 insertions(+), 21 deletions(-) diff --git a/src/env.rs b/src/env.rs index 6b116e37..65572dac 100644 --- a/src/env.rs +++ b/src/env.rs @@ -8,6 +8,7 @@ pub(crate) struct Env { err_style: Style, out_style: Style, out_is_term: bool, + err_is_term: bool, } impl Env { @@ -33,6 +34,7 @@ impl Env { }; let out_is_term = atty::is(atty::Stream::Stdout); + let err_is_term = atty::is(atty::Stream::Stderr); Self::new( dir, @@ -41,6 +43,7 @@ impl Env { out_is_term, io::stderr(), err_style, + err_is_term, env::args(), ) } @@ -70,6 +73,7 @@ impl Env { out_is_term: bool, err: E, err_style: Style, + err_is_term: bool, args: I, ) -> Self where @@ -86,6 +90,7 @@ impl Env { out_style, out_is_term, err_style, + err_is_term, } } @@ -146,6 +151,10 @@ impl Env { pub(crate) fn out_style(&self) -> Style { self.out_style } + + pub(crate) fn err_is_term(&self) -> bool { + self.err_is_term + } } #[cfg(test)] diff --git a/src/hasher.rs b/src/hasher.rs index 98931580..c90ca2a5 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -8,7 +8,7 @@ pub(crate) struct Hasher { piece_length: usize, pieces: PieceList, sha1: Sha1, - progress_bar: ProgressBar, + progress_bar: Option, } impl Hasher { @@ -16,12 +16,12 @@ impl Hasher { files: &Files, md5sum: bool, piece_length: usize, - progress_bar: ProgressBar, + progress_bar: Option, ) -> Result<(Mode, PieceList), Error> { Self::new(md5sum, piece_length, progress_bar).hash_files(files) } - fn new(md5sum: bool, piece_length: usize, progress_bar: ProgressBar) -> Self { + fn new(md5sum: bool, piece_length: usize, progress_bar: Option) -> Self { Self { buffer: vec![0; piece_length], length: 0, @@ -126,7 +126,9 @@ impl Hasher { remaining -= buffer.len().into_u64(); - self.progress_bar.inc(to_buffer.into_u64()); + if let Some(progress_bar) = &self.progress_bar { + progress_bar.inc(to_buffer.into_u64()); + } } self.length += length; diff --git a/src/main.rs b/src/main.rs index bcbde6c8..6262762b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -17,6 +17,7 @@ clippy::result_expect_used, clippy::result_unwrap_used, clippy::shadow_reuse, + clippy::too_many_arguments, clippy::too_many_lines, clippy::unreachable, clippy::unseparated_literal_suffix, diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index deae2a50..b20c09e2 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -205,11 +205,15 @@ impl Create { errln!(env, "[1/3] \u{1F9FF} Searching for files…"); - let style = ProgressStyle::default_spinner() - .template("{spinner:.green} {msg:.bold}…") - .tick_chars(&Self::tick_chars()); + let spinner = if env.err_is_term() { + let style = ProgressStyle::default_spinner() + .template("{spinner:.green} {msg:.bold}…") + .tick_chars(&Self::tick_chars()); - let spinner = ProgressBar::new_spinner().with_style(style); + Some(ProgressBar::new_spinner().with_style(style)) + } else { + None + }; let files = Walker::new(&input) .include_junk(self.include_junk) @@ -293,15 +297,19 @@ impl Create { errln!(env, "[2/3] \u{1F9EE} Hashing pieces…"); - let style = ProgressStyle::default_bar() - .template( - "{spinner:.green} ⟪{elapsed_precise}⟫ ⟦{bar:40.cyan}⟧ {binary_bytes}/{binary_total_bytes} \ - ⟨{binary_bytes_per_sec}, {eta}⟩", - ) - .tick_chars(&Self::tick_chars()) - .progress_chars("█▉▊▋▌▍▎▏ "); + let progress_bar = if env.err_is_term() { + let style = ProgressStyle::default_bar() + .template( + "{spinner:.green} ⟪{elapsed_precise}⟫ ⟦{bar:40.cyan}⟧ \ + {binary_bytes}/{binary_total_bytes} ⟨{binary_bytes_per_sec}, {eta}⟩", + ) + .tick_chars(&Self::tick_chars()) + .progress_chars("█▉▊▋▌▍▎▏ "); - let progress_bar = ProgressBar::new(files.total_size().count()).with_style(style); + Some(ProgressBar::new(files.total_size().count()).with_style(style)) + } else { + None + }; let (mode, pieces) = Hasher::hash( &files, diff --git a/src/test_env_builder.rs b/src/test_env_builder.rs index 5a51ed62..f9e50d5c 100644 --- a/src/test_env_builder.rs +++ b/src/test_env_builder.rs @@ -69,6 +69,7 @@ impl TestEnvBuilder { self.out_is_term, err.clone(), Style::inactive(), + false, self.args, ); diff --git a/src/walker.rs b/src/walker.rs index 5ac42e69..61c73c48 100644 --- a/src/walker.rs +++ b/src/walker.rs @@ -63,11 +63,8 @@ impl Walker { } } - pub(crate) fn spinner(self, spinner: ProgressBar) -> Self { - Self { - spinner: Some(spinner), - ..self - } + pub(crate) fn spinner(self, spinner: Option) -> Self { + Self { spinner, ..self } } pub(crate) fn files(self) -> Result {