Skip to content

Commit

Permalink
Suppress stderr output if --quiet is passed
Browse files Browse the repository at this point in the history
Instead of checking if `options.quiet` is set whenever printing, disable
all printing to stderr if the `--quiet` flag is passed.

Although it isn't strictly necessary, I still don't construct a progress
bar if `options.quiet` is true, since progress bars might be a little
more expensive than just printing error messages.

A future diff might add checking to the `errln` and `outln` macros, so
that they don't print at all if the respective output stream is
inactive.

type: reform
  • Loading branch information
casey committed Sep 8, 2020
1 parent 39dcb5e commit 42e20a4
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ jobs:
- name: Install `mdbook`
uses: peaceiris/actions-mdbook@v1
with:
mdbook-version: latest
mdbook-version: '0.4.2'

- name: Build Book
run: |
Expand Down
27 changes: 27 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ impl Env {
self.out.set_is_term(true);
}

if args.options().quiet {
self.err.set_active(false);
}

args.run(self)
}

Expand Down Expand Up @@ -248,6 +252,29 @@ mod tests {
assert_eq!(env.out(), "");
}

#[test]
fn quiet() {
let mut env = test_env! {
args: [
"--quiet",
"torrent",
"create",
"--input",
"foo",
"--announce",
"udp:bar.com",
"--announce-tier",
"foo",
],
tree: {
foo: "",
}
};
env.status().ok();
assert_eq!(env.err(), "");
assert_eq!(env.out(), "");
}

#[test]
fn terminal() -> Result<()> {
let mut create_env = test_env! {
Expand Down
37 changes: 35 additions & 2 deletions src/output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub(crate) struct OutputStream {
stream: Box<dyn Write>,
style: bool,
term: bool,
active: bool,
}

impl OutputStream {
Expand All @@ -12,6 +13,7 @@ impl OutputStream {
Self {
stream: Box::new(io::stdout()),
style: style && term,
active: true,
term,
}
}
Expand All @@ -20,13 +22,15 @@ impl OutputStream {
Self {
term: style && atty::is(atty::Stream::Stderr),
stream: Box::new(io::stderr()),
active: true,
style,
}
}

#[cfg(test)]
pub(crate) fn new(stream: Box<dyn Write>, style: bool, term: bool) -> OutputStream {
pub(crate) fn new(stream: Box<dyn Write>, style: bool, term: bool, active: bool) -> OutputStream {
Self {
active,
stream,
style,
term,
Expand Down Expand Up @@ -60,14 +64,43 @@ impl OutputStream {
pub(crate) fn style(&self) -> Style {
Style::from_active(self.style)
}

pub(crate) fn set_active(&mut self, active: bool) {
self.active = active;
}
}

impl Write for OutputStream {
fn write(&mut self, data: &[u8]) -> io::Result<usize> {
self.stream.write(data)
if self.active {
self.stream.write(data)
} else {
Ok(data.len())
}
}

fn flush(&mut self) -> io::Result<()> {
self.stream.flush()
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn active() {
let capture = Capture::new();
let mut stream = OutputStream::new(Box::new(capture.clone()), false, false, true);
stream.write_all("hello".as_bytes()).unwrap();
assert_eq!(capture.string(), "hello");
}

#[test]
fn inactive() {
let capture = Capture::new();
let mut stream = OutputStream::new(Box::new(capture.clone()), false, false, false);
stream.write_all("hello".as_bytes()).unwrap();
assert_eq!(capture.string(), "");
}
}
20 changes: 6 additions & 14 deletions src/subcommand/torrent/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,9 +315,7 @@ impl Create {
)
};

if !options.quiet {
CreateStep::Searching { input: &input }.print(env)?;
}
CreateStep::Searching { input: &input }.print(env)?;

let content = CreateContent::from_create(&self, &input, env)?;

Expand Down Expand Up @@ -354,9 +352,7 @@ impl Create {
Some(String::from(consts::CREATED_BY_DEFAULT))
};

if !options.quiet {
CreateStep::Hashing.print(env)?;
}
CreateStep::Hashing.print(env)?;

let hasher = Hasher::new(
self.md5sum,
Expand All @@ -374,12 +370,10 @@ impl Create {
hasher.hash_stdin(&mut env.input())?
};

if !options.quiet {
CreateStep::Writing {
output: &content.output,
}
.print(env)?;
CreateStep::Writing {
output: &content.output,
}
.print(env)?;

let info = Info {
name: content.name,
Expand Down Expand Up @@ -449,9 +443,7 @@ impl Create {
}
}

if !options.quiet {
errln!(env, "\u{2728}\u{2728} Done! \u{2728}\u{2728}")?;
}
errln!(env, "\u{2728}\u{2728} Done! \u{2728}\u{2728}")?;

if self.show {
// We just created this torrent, so no extra fields have been discarded.
Expand Down
18 changes: 6 additions & 12 deletions src/subcommand/torrent/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,7 @@ impl Verify {
&self.input_flag,
)?;

if !options.quiet {
VerifyStep::Loading { metainfo: &target }.print(env)?;
}
VerifyStep::Loading { metainfo: &target }.print(env)?;

let input = env.read(target.clone())?;

Expand All @@ -86,21 +84,17 @@ impl Verify {
None
};

if !options.quiet {
VerifyStep::Verifying { content: &content }.print(env)?;
}
VerifyStep::Verifying { content: &content }.print(env)?;

let status = metainfo.verify(&env.resolve(content)?, progress_bar)?;

status.print(env)?;

if status.good() {
if !options.quiet {
errln!(
env,
"\u{2728}\u{2728} Verification succeeded! \u{2728}\u{2728}"
)?;
}
errln!(
env,
"\u{2728}\u{2728} Verification succeeded! \u{2728}\u{2728}"
)?;
Ok(())
} else {
Err(Error::Verify)
Expand Down
3 changes: 2 additions & 1 deletion src/test_env_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,10 @@ impl TestEnvBuilder {
Box::new(out.clone()),
self.use_color && self.out_is_term,
self.out_is_term,
true,
);

let err_stream = OutputStream::new(Box::new(err.clone()), self.err_style, false);
let err_stream = OutputStream::new(Box::new(err.clone()), self.err_style, false, true);

let env = Env::new(
current_dir,
Expand Down

0 comments on commit 42e20a4

Please sign in to comment.