Only write spinner and progress bar when connected to terminal

type: fixed
This commit is contained in:
Casey Rodarmor 2020-03-12 01:08:40 -07:00
parent 2cfdad2597
commit bb34936c2f
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
6 changed files with 39 additions and 21 deletions

View File

@ -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)]

View File

@ -8,7 +8,7 @@ pub(crate) struct Hasher {
piece_length: usize,
pieces: PieceList,
sha1: Sha1,
progress_bar: ProgressBar,
progress_bar: Option<ProgressBar>,
}
impl Hasher {
@ -16,12 +16,12 @@ impl Hasher {
files: &Files,
md5sum: bool,
piece_length: usize,
progress_bar: ProgressBar,
progress_bar: Option<ProgressBar>,
) -> 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<ProgressBar>) -> 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;

View File

@ -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,

View File

@ -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,

View File

@ -69,6 +69,7 @@ impl TestEnvBuilder {
self.out_is_term,
err.clone(),
Style::inactive(),
false,
self.args,
);

View File

@ -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<ProgressBar>) -> Self {
Self { spinner, ..self }
}
pub(crate) fn files(self) -> Result<Files, Error> {