From 42e20a4a6ad3497d85c1886e93240afafb88faa3 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 8 Sep 2020 15:21:53 -0700 Subject: [PATCH] Suppress stderr output if --quiet is passed 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 --- .github/workflows/build.yaml | 2 +- src/env.rs | 27 +++++++++++++++++++++++ src/output_stream.rs | 37 ++++++++++++++++++++++++++++++-- src/subcommand/torrent/create.rs | 20 ++++++----------- src/subcommand/torrent/verify.rs | 18 ++++++---------- src/test_env_builder.rs | 3 ++- 6 files changed, 77 insertions(+), 30 deletions(-) diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 701b424..36efd16 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -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: | diff --git a/src/env.rs b/src/env.rs index 2ed0bcb..0c5f80f 100644 --- a/src/env.rs +++ b/src/env.rs @@ -61,6 +61,10 @@ impl Env { self.out.set_is_term(true); } + if args.options().quiet { + self.err.set_active(false); + } + args.run(self) } @@ -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! { diff --git a/src/output_stream.rs b/src/output_stream.rs index 5c27223..d443beb 100644 --- a/src/output_stream.rs +++ b/src/output_stream.rs @@ -4,6 +4,7 @@ pub(crate) struct OutputStream { stream: Box, style: bool, term: bool, + active: bool, } impl OutputStream { @@ -12,6 +13,7 @@ impl OutputStream { Self { stream: Box::new(io::stdout()), style: style && term, + active: true, term, } } @@ -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, style: bool, term: bool) -> OutputStream { + pub(crate) fn new(stream: Box, style: bool, term: bool, active: bool) -> OutputStream { Self { + active, stream, style, term, @@ -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 { - 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(), ""); + } +} diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index 3c11a6e..49b7634 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -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)?; @@ -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, @@ -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, @@ -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. diff --git a/src/subcommand/torrent/verify.rs b/src/subcommand/torrent/verify.rs index e100f0d..64e80a3 100644 --- a/src/subcommand/torrent/verify.rs +++ b/src/subcommand/torrent/verify.rs @@ -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())?; @@ -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) diff --git a/src/test_env_builder.rs b/src/test_env_builder.rs index 1aa1dfa..58759ff 100644 --- a/src/test_env_builder.rs +++ b/src/test_env_builder.rs @@ -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,