Skip to content

Commit

Permalink
Add --terminal override terminal autodetection
Browse files Browse the repository at this point in the history
The `--terminal` flag can be used to override terminal autodetection and
force `imdl` to behave as if standard error and standard output are
connected to a terminal.

type: added
fixes:
- #398
  • Loading branch information
casey committed Apr 20, 2020
1 parent 70dbe93 commit 65597c9
Show file tree
Hide file tree
Showing 11 changed files with 82 additions and 5 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ Changelog

UNRELEASED - 2020-04-20
-----------------------
- :books: [`xxxxxxxxxxxx`](https://github.com/casey/intermodal/commits/master) Note install script only works on Linux and MacOS - Fixes [#371](https://github.com/casey/intermodal/issues/371) - _Casey Rodarmor <casey@rodarmor.com>_
- :sparkles: [`xxxxxxxxxxxx`](https://github.com/casey/intermodal/commits/master) Add `--terminal` override terminal autodetection - Fixes [#398](https://github.com/casey/intermodal/issues/398) - _Casey Rodarmor <casey@rodarmor.com>_
- :books: [`70dbe93c6ab0`](https://github.com/casey/intermodal/commit/70dbe93c6ab01408dfc42e1b75664de13de16a51) Note install script only works on Linux and MacOS - Fixes [#371](https://github.com/casey/intermodal/issues/371) - _Casey Rodarmor <casey@rodarmor.com>_
- :books: [`b67a2f1885c9`](https://github.com/casey/intermodal/commit/b67a2f1885c9445c08411457809f8893ebfa2045) Fix 404.css link - _Casey Rodarmor <casey@rodarmor.com>_
- :books: [`87687f4decbb`](https://github.com/casey/intermodal/commit/87687f4decbbd216b32ea3c9001122c56d5a93fc) Add custom 404 page to site - _Casey Rodarmor <casey@rodarmor.com>_
- :books: [`09b0ee316c03`](https://github.com/casey/intermodal/commit/09b0ee316c034848c3b50966e7b5e3ed720aef2b) Document piece length selection algorithm ([#392](https://github.com/casey/intermodal/pull/392)) - Fixes [#367](https://github.com/casey/intermodal/issues/367) - _Casey Rodarmor <casey@rodarmor.com>_
Expand Down
5 changes: 4 additions & 1 deletion book/src/commands/imdl.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ USAGE:
FLAGS:
-h, --help Print help message.
-t, --terminal Disable automatic terminal detection and behave as if both
standard output and standard error are connected to a
terminal.
-u, --unstable Enable unstable features. To avoid premature stabilization
and excessive version churn, unstable features are
unavailable unless this flag is set. Unstable features are
Expand All @@ -18,7 +21,7 @@ FLAGS:
-V, --version Print version number.
OPTIONS:
--color <WHEN> Print colorful output according to `WHEN`. When
-c, --color <WHEN> 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
Expand Down
3 changes: 3 additions & 0 deletions completions/_imdl
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ _imdl() {

local context curcontext="$curcontext" state line
_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)' \
'-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.]' \
'--terminal[Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal.]' \
'-h[Print help message.]' \
'--help[Print help message.]' \
'-V[Print version number.]' \
Expand Down
3 changes: 3 additions & 0 deletions completions/_imdl.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ Register-ArgumentCompleter -Native -CommandName 'imdl' -ScriptBlock {

$completions = @(switch ($command) {
'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('-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.')
[CompletionResult]::new('--terminal', 'terminal', [CompletionResultType]::ParameterName, 'Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal.')
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help message.')
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help message.')
[CompletionResult]::new('-V', 'V', [CompletionResultType]::ParameterName, 'Print version number.')
Expand Down
6 changes: 5 additions & 1 deletion completions/imdl.bash
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ _imdl() {

case "${cmd}" in
imdl)
opts=" -u -h -V --unstable --help --version --color torrent completions help"
opts=" -u -t -h -V -c --unstable --terminal --help --version --color torrent completions help"
if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
Expand All @@ -61,6 +61,10 @@ _imdl() {
COMPREPLY=($(compgen -W "auto always never" -- "${cur}"))
return 0
;;
-c)
COMPREPLY=($(compgen -W "auto always never" -- "${cur}"))
return 0
;;
*)
COMPREPLY=()
;;
Expand Down
3 changes: 3 additions & 0 deletions completions/imdl.elvish
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@ edit:completion:arg-completer[imdl] = [@words]{
}
completions = [
&'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 -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.'
cand --terminal 'Disable automatic terminal detection and behave as if both standard output and standard error are connected to a terminal.'
cand -h 'Print help message.'
cand --help 'Print help message.'
cand -V 'Print version number.'
Expand Down
3 changes: 2 additions & 1 deletion completions/imdl.fish
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
complete -c imdl -n "__fish_use_subcommand" -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 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 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.'
complete -c imdl -n "__fish_use_subcommand" -s V -l version -d 'Print version number.'
complete -c imdl -n "__fish_use_subcommand" -f -a "torrent" -d 'Subcommands related to the BitTorrent protocol.'
Expand Down
6 changes: 5 additions & 1 deletion man/imdl.1
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ imdl [FLAGS] [OPTIONS] <SUBCOMMAND>
\fB\-h\fR, \fB\-\-help\fR
Print help message.
.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.
.TP
\fB\-u\fR, \fB\-\-unstable\fR
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
Expand All @@ -20,7 +24,7 @@ versioning stability guarantees, and may be changed or removed at any time.
Print version number.
.SH "OPTIONS:"
.TP
\fB\-\-color\fR <WHEN>
\fB\-c\fR, \fB\-\-color\fR <WHEN>
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`. [default: auto]
Expand Down
43 changes: 43 additions & 0 deletions src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ impl Env {
self.err.set_use_color(use_color);
self.out.set_use_color(use_color);

if args.options().terminal {
self.err.set_is_term(true);
self.out.set_is_term(true);
}

args.run(self)
}

Expand Down Expand Up @@ -242,4 +247,42 @@ mod tests {

assert_eq!(env.out(), "");
}

#[test]
fn terminal() -> Result<()> {
let mut create_env = test_env! {
args: [
"torrent",
"create",
"--input",
"foo",
"--announce",
"udp:bar.com",
],
tree: {
foo: "",
}
};

create_env.assert_ok();

let mut env = test_env! {
args: [
"--terminal",
"torrent",
"show",
"--input",
create_env.resolve("foo.torrent")?,
],
tree: {
}
};

env.assert_ok();

assert_eq!(env.err(), "");
assert!(env.out().starts_with(" Name"));

Ok(())
}
}
8 changes: 8 additions & 0 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) struct Options {
unstable: bool,
#[structopt(
long = "color",
short = "c",
value_name = "WHEN",
default_value = UseColor::Auto.into(),
possible_values = UseColor::VARIANTS,
Expand All @@ -22,6 +23,13 @@ pub(crate) struct Options {
`dumb`.",
)]
pub(crate) use_color: UseColor,
#[structopt(
long = "terminal",
short = "t",
help = "Disable automatic terminal detection and behave as if both standard output and \
standard error are connected to a terminal."
)]
pub(crate) terminal: bool,
}

impl Options {
Expand Down
4 changes: 4 additions & 0 deletions src/output_stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ impl OutputStream {
}
}

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

pub(crate) fn is_term(&self) -> bool {
self.term
}
Expand Down

0 comments on commit 65597c9

Please sign in to comment.