Only write spinner and progress bar when connected to terminal
type: fixed
This commit is contained in:
parent
2cfdad2597
commit
bb34936c2f
|
@ -8,6 +8,7 @@ pub(crate) struct Env {
|
||||||
err_style: Style,
|
err_style: Style,
|
||||||
out_style: Style,
|
out_style: Style,
|
||||||
out_is_term: bool,
|
out_is_term: bool,
|
||||||
|
err_is_term: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Env {
|
impl Env {
|
||||||
|
@ -33,6 +34,7 @@ impl Env {
|
||||||
};
|
};
|
||||||
|
|
||||||
let out_is_term = atty::is(atty::Stream::Stdout);
|
let out_is_term = atty::is(atty::Stream::Stdout);
|
||||||
|
let err_is_term = atty::is(atty::Stream::Stderr);
|
||||||
|
|
||||||
Self::new(
|
Self::new(
|
||||||
dir,
|
dir,
|
||||||
|
@ -41,6 +43,7 @@ impl Env {
|
||||||
out_is_term,
|
out_is_term,
|
||||||
io::stderr(),
|
io::stderr(),
|
||||||
err_style,
|
err_style,
|
||||||
|
err_is_term,
|
||||||
env::args(),
|
env::args(),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -70,6 +73,7 @@ impl Env {
|
||||||
out_is_term: bool,
|
out_is_term: bool,
|
||||||
err: E,
|
err: E,
|
||||||
err_style: Style,
|
err_style: Style,
|
||||||
|
err_is_term: bool,
|
||||||
args: I,
|
args: I,
|
||||||
) -> Self
|
) -> Self
|
||||||
where
|
where
|
||||||
|
@ -86,6 +90,7 @@ impl Env {
|
||||||
out_style,
|
out_style,
|
||||||
out_is_term,
|
out_is_term,
|
||||||
err_style,
|
err_style,
|
||||||
|
err_is_term,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -146,6 +151,10 @@ impl Env {
|
||||||
pub(crate) fn out_style(&self) -> Style {
|
pub(crate) fn out_style(&self) -> Style {
|
||||||
self.out_style
|
self.out_style
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn err_is_term(&self) -> bool {
|
||||||
|
self.err_is_term
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
|
|
|
@ -8,7 +8,7 @@ pub(crate) struct Hasher {
|
||||||
piece_length: usize,
|
piece_length: usize,
|
||||||
pieces: PieceList,
|
pieces: PieceList,
|
||||||
sha1: Sha1,
|
sha1: Sha1,
|
||||||
progress_bar: ProgressBar,
|
progress_bar: Option<ProgressBar>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Hasher {
|
impl Hasher {
|
||||||
|
@ -16,12 +16,12 @@ impl Hasher {
|
||||||
files: &Files,
|
files: &Files,
|
||||||
md5sum: bool,
|
md5sum: bool,
|
||||||
piece_length: usize,
|
piece_length: usize,
|
||||||
progress_bar: ProgressBar,
|
progress_bar: Option<ProgressBar>,
|
||||||
) -> Result<(Mode, PieceList), Error> {
|
) -> Result<(Mode, PieceList), Error> {
|
||||||
Self::new(md5sum, piece_length, progress_bar).hash_files(files)
|
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 {
|
Self {
|
||||||
buffer: vec![0; piece_length],
|
buffer: vec![0; piece_length],
|
||||||
length: 0,
|
length: 0,
|
||||||
|
@ -126,7 +126,9 @@ impl Hasher {
|
||||||
|
|
||||||
remaining -= buffer.len().into_u64();
|
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;
|
self.length += length;
|
||||||
|
|
|
@ -17,6 +17,7 @@
|
||||||
clippy::result_expect_used,
|
clippy::result_expect_used,
|
||||||
clippy::result_unwrap_used,
|
clippy::result_unwrap_used,
|
||||||
clippy::shadow_reuse,
|
clippy::shadow_reuse,
|
||||||
|
clippy::too_many_arguments,
|
||||||
clippy::too_many_lines,
|
clippy::too_many_lines,
|
||||||
clippy::unreachable,
|
clippy::unreachable,
|
||||||
clippy::unseparated_literal_suffix,
|
clippy::unseparated_literal_suffix,
|
||||||
|
|
|
@ -205,11 +205,15 @@ impl Create {
|
||||||
|
|
||||||
errln!(env, "[1/3] \u{1F9FF} Searching for files…");
|
errln!(env, "[1/3] \u{1F9FF} Searching for files…");
|
||||||
|
|
||||||
let style = ProgressStyle::default_spinner()
|
let spinner = if env.err_is_term() {
|
||||||
.template("{spinner:.green} {msg:.bold}…")
|
let style = ProgressStyle::default_spinner()
|
||||||
.tick_chars(&Self::tick_chars());
|
.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)
|
let files = Walker::new(&input)
|
||||||
.include_junk(self.include_junk)
|
.include_junk(self.include_junk)
|
||||||
|
@ -293,15 +297,19 @@ impl Create {
|
||||||
|
|
||||||
errln!(env, "[2/3] \u{1F9EE} Hashing pieces…");
|
errln!(env, "[2/3] \u{1F9EE} Hashing pieces…");
|
||||||
|
|
||||||
let style = ProgressStyle::default_bar()
|
let progress_bar = if env.err_is_term() {
|
||||||
.template(
|
let style = ProgressStyle::default_bar()
|
||||||
"{spinner:.green} ⟪{elapsed_precise}⟫ ⟦{bar:40.cyan}⟧ {binary_bytes}/{binary_total_bytes} \
|
.template(
|
||||||
⟨{binary_bytes_per_sec}, {eta}⟩",
|
"{spinner:.green} ⟪{elapsed_precise}⟫ ⟦{bar:40.cyan}⟧ \
|
||||||
)
|
{binary_bytes}/{binary_total_bytes} ⟨{binary_bytes_per_sec}, {eta}⟩",
|
||||||
.tick_chars(&Self::tick_chars())
|
)
|
||||||
.progress_chars("█▉▊▋▌▍▎▏ ");
|
.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(
|
let (mode, pieces) = Hasher::hash(
|
||||||
&files,
|
&files,
|
||||||
|
|
|
@ -69,6 +69,7 @@ impl TestEnvBuilder {
|
||||||
self.out_is_term,
|
self.out_is_term,
|
||||||
err.clone(),
|
err.clone(),
|
||||||
Style::inactive(),
|
Style::inactive(),
|
||||||
|
false,
|
||||||
self.args,
|
self.args,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
|
@ -63,11 +63,8 @@ impl Walker {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn spinner(self, spinner: ProgressBar) -> Self {
|
pub(crate) fn spinner(self, spinner: Option<ProgressBar>) -> Self {
|
||||||
Self {
|
Self { spinner, ..self }
|
||||||
spinner: Some(spinner),
|
|
||||||
..self
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn files(self) -> Result<Files, Error> {
|
pub(crate) fn files(self) -> Result<Files, Error> {
|
||||||
|
|
Loading…
Reference in New Issue
Block a user