Skip to content

Commit

Permalink
Only write spinner and progress bar when connected to terminal
Browse files Browse the repository at this point in the history
type: fixed
  • Loading branch information
casey committed Apr 8, 2020
1 parent 2cfdad2 commit bb34936
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 21 deletions.
9 changes: 9 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub(crate) struct Env {
err_style: Style,
out_style: Style,
out_is_term: bool,
err_is_term: bool,
}

impl Env {
Expand All @@ -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,
Expand All @@ -41,6 +43,7 @@ impl Env {
out_is_term,
io::stderr(),
err_style,
err_is_term,
env::args(),
)
}
Expand Down Expand Up @@ -70,6 +73,7 @@ impl Env {
out_is_term: bool,
err: E,
err_style: Style,
err_is_term: bool,
args: I,
) -> Self
where
Expand All @@ -86,6 +90,7 @@ impl Env {
out_style,
out_is_term,
err_style,
err_is_term,
}
}

Expand Down Expand Up @@ -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)]
Expand Down
10 changes: 6 additions & 4 deletions src/hasher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@ pub(crate) struct Hasher {
piece_length: usize,
pieces: PieceList,
sha1: Sha1,
progress_bar: ProgressBar,
progress_bar: Option<ProgressBar>,
}

impl Hasher {
pub(crate) fn hash(
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,
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
32 changes: 20 additions & 12 deletions src/subcommand/torrent/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/test_env_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ impl TestEnvBuilder {
self.out_is_term,
err.clone(),
Style::inactive(),
false,
self.args,
);

Expand Down
7 changes: 2 additions & 5 deletions src/walker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
Expand Down

0 comments on commit bb34936

Please sign in to comment.