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
This commit is contained in:
Celeo 2020-04-19 16:56:54 -07:00 committed by Casey Rodarmor
parent 838167c4d3
commit deca555ac3
No known key found for this signature in database
GPG Key ID: 556186B153EC6FE0
12 changed files with 104 additions and 22 deletions

View File

@ -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 <casey@rodarmor.com>_
- :sparkles: [`xxxxxxxxxxxx`](https://github.com/casey/intermodal/commits/master) Allow suppressing output with `--quiet` - Fixes [#174](https://github.com/casey/intermodal/issues/174) - _Celeo <celeodor@gmail.com>_
- :books: [`838167c4d3bc`](https://github.com/casey/intermodal/commit/838167c4d3bcbe2fa28f27a00bd94b959ad31e15) Describe in FAQ creating torrent from git repo - _Casey Rodarmor <casey@rodarmor.com>_
- :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 <celeodor@gmail.com>_
- :books: [`9f480624616b`](https://github.com/casey/intermodal/commit/9f480624616b77995befec722effda22cc2d06ad) Improve FAQ template - _Casey Rodarmor <casey@rodarmor.com>_
- :wrench: [`1380290eb8e2`](https://github.com/casey/intermodal/commit/1380290eb8e222605f368bc8346a1e63c83d9af7) Make `publish-check` recipe stricter - _Casey Rodarmor <casey@rodarmor.com>_

View File

@ -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.

View File

@ -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.]' \

View File

@ -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.')

View File

@ -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

View File

@ -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.'

View File

@ -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.'

View File

@ -11,6 +11,9 @@ imdl [FLAGS] [OPTIONS] <SUBCOMMAND>
\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.

View File

@ -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",

View File

@ -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),
}
}
}

View File

@ -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(), "");
}
}

View File

@ -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(())
}
}