From deca555ac3b3b8f665ee6415f80e05b2bb5e4af7 Mon Sep 17 00:00:00 2001 From: Celeo Date: Sun, 19 Apr 2020 16:56:54 -0700 Subject: [PATCH] Allow suppressing output with `--quiet` Add a global flag `--quiet`, which allows supressing output from `imdl torrent create` and `imdl torrent verify`. Since it's a global option, it should be given before the subcommand, e.g.: imdl --quiet torrent create --input . type: added fixes: - https://github.com/casey/intermodal/issues/174 --- CHANGELOG.md | 3 +- book/src/commands/imdl.md | 1 + completions/_imdl | 2 ++ completions/_imdl.ps1 | 2 ++ completions/imdl.bash | 2 +- completions/imdl.elvish | 2 ++ completions/imdl.fish | 1 + man/imdl.1 | 3 ++ src/options.rs | 2 ++ src/subcommand/torrent.rs | 4 +-- src/subcommand/torrent/create.rs | 43 +++++++++++++++++----- src/subcommand/torrent/verify.rs | 61 ++++++++++++++++++++++++++------ 12 files changed, 104 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 909248e..367ee11 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,7 +4,8 @@ Changelog UNRELEASED - 2020-04-21 ----------------------- -- :books: [`xxxxxxxxxxxx`](https://github.com/casey/intermodal/commits/master) Describe in FAQ creating torrent from git repo - _Casey Rodarmor _ +- :sparkles: [`xxxxxxxxxxxx`](https://github.com/casey/intermodal/commits/master) Allow suppressing output with `--quiet` - Fixes [#174](https://github.com/casey/intermodal/issues/174) - _Celeo _ +- :books: [`838167c4d3bc`](https://github.com/casey/intermodal/commit/838167c4d3bcbe2fa28f27a00bd94b959ad31e15) Describe in FAQ creating torrent from git repo - _Casey Rodarmor _ - :sparkles: [`9b72873ed13e`](https://github.com/casey/intermodal/commit/9b72873ed13e8f0ae747714545c48c6e37c67dd0) Optionally respect `.gitignore` in `imdl torrent create` - Fixes [#378](https://github.com/casey/intermodal/issues/378) - _Celeo _ - :books: [`9f480624616b`](https://github.com/casey/intermodal/commit/9f480624616b77995befec722effda22cc2d06ad) Improve FAQ template - _Casey Rodarmor _ - :wrench: [`1380290eb8e2`](https://github.com/casey/intermodal/commit/1380290eb8e222605f368bc8346a1e63c83d9af7) Make `publish-check` recipe stricter - _Casey Rodarmor _ diff --git a/book/src/commands/imdl.md b/book/src/commands/imdl.md index 16a06e9..c7078cd 100644 --- a/book/src/commands/imdl.md +++ b/book/src/commands/imdl.md @@ -10,6 +10,7 @@ USAGE: FLAGS: -h, --help Print help message. + -q, --quiet Suppress normal output. -t, --terminal Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal. diff --git a/completions/_imdl b/completions/_imdl index d79855f..9b4a7fa 100644 --- a/completions/_imdl +++ b/completions/_imdl @@ -17,6 +17,8 @@ _imdl() { _arguments "${_arguments_options[@]}" \ '-c+[Print colorful output according to `WHEN`. When `auto`, the default, colored output is only enabled if imdl detects that it is connected to a terminal, the `NO_COLOR` environment variable is not set, and the `TERM` environment variable is not set to `dumb`.]: :(auto always never)' \ '--color=[Print colorful output according to `WHEN`. When `auto`, the default, colored output is only enabled if imdl detects that it is connected to a terminal, the `NO_COLOR` environment variable is not set, and the `TERM` environment variable is not set to `dumb`.]: :(auto always never)' \ +'-q[Suppress normal output.]' \ +'--quiet[Suppress normal output.]' \ '-u[Enable unstable features. To avoid premature stabilization and excessive version churn, unstable features are unavailable unless this flag is set. Unstable features are not bound by semantic versioning stability guarantees, and may be changed or removed at any time.]' \ '--unstable[Enable unstable features. To avoid premature stabilization and excessive version churn, unstable features are unavailable unless this flag is set. Unstable features are not bound by semantic versioning stability guarantees, and may be changed or removed at any time.]' \ '-t[Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal.]' \ diff --git a/completions/_imdl.ps1 b/completions/_imdl.ps1 index f30751e..ccd5d95 100644 --- a/completions/_imdl.ps1 +++ b/completions/_imdl.ps1 @@ -21,6 +21,8 @@ Register-ArgumentCompleter -Native -CommandName 'imdl' -ScriptBlock { 'imdl' { [CompletionResult]::new('-c', 'c', [CompletionResultType]::ParameterName, 'Print colorful output according to `WHEN`. When `auto`, the default, colored output is only enabled if imdl detects that it is connected to a terminal, the `NO_COLOR` environment variable is not set, and the `TERM` environment variable is not set to `dumb`.') [CompletionResult]::new('--color', 'color', [CompletionResultType]::ParameterName, 'Print colorful output according to `WHEN`. When `auto`, the default, colored output is only enabled if imdl detects that it is connected to a terminal, the `NO_COLOR` environment variable is not set, and the `TERM` environment variable is not set to `dumb`.') + [CompletionResult]::new('-q', 'q', [CompletionResultType]::ParameterName, 'Suppress normal output.') + [CompletionResult]::new('--quiet', 'quiet', [CompletionResultType]::ParameterName, 'Suppress normal output.') [CompletionResult]::new('-u', 'u', [CompletionResultType]::ParameterName, 'Enable unstable features. To avoid premature stabilization and excessive version churn, unstable features are unavailable unless this flag is set. Unstable features are not bound by semantic versioning stability guarantees, and may be changed or removed at any time.') [CompletionResult]::new('--unstable', 'unstable', [CompletionResultType]::ParameterName, 'Enable unstable features. To avoid premature stabilization and excessive version churn, unstable features are unavailable unless this flag is set. Unstable features are not bound by semantic versioning stability guarantees, and may be changed or removed at any time.') [CompletionResult]::new('-t', 't', [CompletionResultType]::ParameterName, 'Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal.') diff --git a/completions/imdl.bash b/completions/imdl.bash index 62b8469..2652bae 100644 --- a/completions/imdl.bash +++ b/completions/imdl.bash @@ -50,7 +50,7 @@ _imdl() { case "${cmd}" in imdl) - opts=" -u -t -h -V -c --unstable --terminal --help --version --color torrent completions help" + opts=" -q -u -t -h -V -c --quiet --unstable --terminal --help --version --color torrent completions help" if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") ) return 0 diff --git a/completions/imdl.elvish b/completions/imdl.elvish index ddc783c..f35079a 100644 --- a/completions/imdl.elvish +++ b/completions/imdl.elvish @@ -16,6 +16,8 @@ edit:completion:arg-completer[imdl] = [@words]{ &'imdl'= { cand -c 'Print colorful output according to `WHEN`. When `auto`, the default, colored output is only enabled if imdl detects that it is connected to a terminal, the `NO_COLOR` environment variable is not set, and the `TERM` environment variable is not set to `dumb`.' cand --color 'Print colorful output according to `WHEN`. When `auto`, the default, colored output is only enabled if imdl detects that it is connected to a terminal, the `NO_COLOR` environment variable is not set, and the `TERM` environment variable is not set to `dumb`.' + cand -q 'Suppress normal output.' + cand --quiet 'Suppress normal output.' cand -u 'Enable unstable features. To avoid premature stabilization and excessive version churn, unstable features are unavailable unless this flag is set. Unstable features are not bound by semantic versioning stability guarantees, and may be changed or removed at any time.' cand --unstable 'Enable unstable features. To avoid premature stabilization and excessive version churn, unstable features are unavailable unless this flag is set. Unstable features are not bound by semantic versioning stability guarantees, and may be changed or removed at any time.' cand -t 'Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal.' diff --git a/completions/imdl.fish b/completions/imdl.fish index 71934c5..e635a8b 100644 --- a/completions/imdl.fish +++ b/completions/imdl.fish @@ -1,4 +1,5 @@ complete -c imdl -n "__fish_use_subcommand" -s c -l color -d 'Print colorful output according to `WHEN`. When `auto`, the default, colored output is only enabled if imdl detects that it is connected to a terminal, the `NO_COLOR` environment variable is not set, and the `TERM` environment variable is not set to `dumb`.' -r -f -a "auto always never" +complete -c imdl -n "__fish_use_subcommand" -s q -l quiet -d 'Suppress normal output.' complete -c imdl -n "__fish_use_subcommand" -s u -l unstable -d 'Enable unstable features. To avoid premature stabilization and excessive version churn, unstable features are unavailable unless this flag is set. Unstable features are not bound by semantic versioning stability guarantees, and may be changed or removed at any time.' complete -c imdl -n "__fish_use_subcommand" -s t -l terminal -d 'Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal.' complete -c imdl -n "__fish_use_subcommand" -s h -l help -d 'Print help message.' diff --git a/man/imdl.1 b/man/imdl.1 index 21827ad..08de9ea 100644 --- a/man/imdl.1 +++ b/man/imdl.1 @@ -11,6 +11,9 @@ imdl [FLAGS] [OPTIONS] \fB\-h\fR, \fB\-\-help\fR Print help message. .TP +\fB\-q\fR, \fB\-\-quiet\fR +Suppress normal output. +.TP \fB\-t\fR, \fB\-\-terminal\fR Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal. diff --git a/src/options.rs b/src/options.rs index af742a8..794451d 100644 --- a/src/options.rs +++ b/src/options.rs @@ -2,6 +2,8 @@ use crate::common::*; #[derive(StructOpt)] pub(crate) struct Options { + #[structopt(long = "quiet", short = "q", help = "Suppress normal output.")] + pub(crate) quiet: bool, #[structopt( long = "unstable", short = "u", diff --git a/src/subcommand/torrent.rs b/src/subcommand/torrent.rs index d8aa6c3..fa895a1 100644 --- a/src/subcommand/torrent.rs +++ b/src/subcommand/torrent.rs @@ -26,12 +26,12 @@ pub(crate) enum Torrent { impl Torrent { pub(crate) fn run(self, env: &mut Env, options: &Options) -> Result<(), Error> { match self { - Self::Create(create) => create.run(env), + Self::Create(create) => create.run(env, options), Self::Link(link) => link.run(env), Self::PieceLength(piece_length) => piece_length.run(env), Self::Show(show) => show.run(env), Self::Stats(stats) => stats.run(env, options), - Self::Verify(verify) => verify.run(env), + Self::Verify(verify) => verify.run(env, options), } } } diff --git a/src/subcommand/torrent/create.rs b/src/subcommand/torrent/create.rs index dd331ac..e981c53 100644 --- a/src/subcommand/torrent/create.rs +++ b/src/subcommand/torrent/create.rs @@ -249,7 +249,7 @@ Sort in ascending order by size, break ties in descending path order: } impl Create { - pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> { + pub(crate) fn run(self, env: &mut Env, options: &Options) -> Result<(), Error> { let mut linter = Linter::new(); linter.allow(self.allowed_lints.iter().cloned()); @@ -280,7 +280,9 @@ impl Create { ) }; - CreateStep::Searching { input: &self.input }.print(env)?; + if !options.quiet { + CreateStep::Searching { input: &self.input }.print(env)?; + } let content = CreateContent::from_create(&self, env)?; @@ -317,12 +319,14 @@ impl Create { Some(String::from(consts::CREATED_BY_DEFAULT)) }; - CreateStep::Hashing.print(env)?; + if !options.quiet { + CreateStep::Hashing.print(env)?; + } let hasher = Hasher::new( self.md5sum, content.piece_length.as_piece_length()?.into_usize(), - if env.err().is_styled_term() { + if env.err().is_styled_term() && !options.quiet { Some(content.progress_bar) } else { None @@ -335,10 +339,12 @@ impl Create { hasher.hash_stdin(&mut env.input())? }; - CreateStep::Writing { - output: &content.output, + if !options.quiet { + CreateStep::Writing { + output: &content.output, + } + .print(env)?; } - .print(env)?; let info = Info { source: self.source, @@ -407,7 +413,9 @@ impl Create { } } - errln!(env, "\u{2728}\u{2728} Done! \u{2728}\u{2728}")?; + if !options.quiet { + errln!(env, "\u{2728}\u{2728} Done! \u{2728}\u{2728}")?; + } if self.show { TorrentSummary::from_metainfo(metainfo.clone())?.write(env)?; @@ -2970,4 +2978,23 @@ Content Size 9 bytes Ok(()) } + + #[test] + fn no_output_when_quiet() { + let mut env = test_env! { + args: [ + "--quiet", + "torrent", + "create", + "--input", + "foo" + ], + tree: { + foo: "", + } + }; + env.assert_ok(); + assert_eq!(env.out(), ""); + assert_eq!(env.err(), ""); + } } diff --git a/src/subcommand/torrent/verify.rs b/src/subcommand/torrent/verify.rs index 39e1fd1..24e3065 100644 --- a/src/subcommand/torrent/verify.rs +++ b/src/subcommand/torrent/verify.rs @@ -33,11 +33,13 @@ pub(crate) struct Verify { } impl Verify { - pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> { - VerifyStep::Loading { - metainfo: &self.metainfo, + pub(crate) fn run(self, env: &mut Env, options: &Options) -> Result<(), Error> { + if !options.quiet { + VerifyStep::Loading { + metainfo: &self.metainfo, + } + .print(env)?; } - .print(env)?; let input = env.read(self.metainfo.clone())?; @@ -52,7 +54,7 @@ impl Verify { } }; - let progress_bar = if env.err().is_styled_term() { + let progress_bar = if env.err().is_styled_term() && !options.quiet { let style = ProgressStyle::default_bar() .template(consts::PROGRESS_STYLE) .tick_chars(consts::TICK_CHARS) @@ -63,17 +65,21 @@ impl Verify { None }; - VerifyStep::Verifying { content: &content }.print(env)?; + if !options.quiet { + VerifyStep::Verifying { content: &content }.print(env)?; + } let status = metainfo.verify(&env.resolve(content)?, progress_bar)?; status.print(env)?; if status.good() { - errln!( - env, - "\u{2728}\u{2728} Verification succeeded! \u{2728}\u{2728}" - )?; + if !options.quiet { + errln!( + env, + "\u{2728}\u{2728} Verification succeeded! \u{2728}\u{2728}" + )?; + } Ok(()) } else { Err(Error::Verify) @@ -618,4 +624,39 @@ mod tests { Ok(()) } + + #[test] + fn no_output_when_quiet() -> Result<()> { + let mut create_env = test_env! { + args: [ + "torrent", + "create", + "--input", + "foo" + ], + tree: { + foo: "", + } + }; + create_env.assert_ok(); + + let torrent = create_env.resolve("foo.torrent")?; + + let mut verify_env = test_env! { + args: [ + "--quiet", + "torrent", + "verify", + "--input", + &torrent, + ], + tree: {}, + }; + verify_env.assert_ok(); + + assert_eq!(verify_env.out(), ""); + assert_eq!(verify_env.err(), ""); + + Ok(()) + } }