mirror of
https://github.com/nushell/nushell.git
synced 2024-11-25 18:03:51 +01:00
Add is-terminal to determine if stdin/out/err are a terminal (#10970)
# Description I'm not sure if "is-terminal" is the best name for this command as there is also "term size". Uses [`is_terminal()`](https://doc.rust-lang.org/stable/std/io/trait.IsTerminal.html#tymethod.is_terminal) which is cross-platform. Possible alternative names: * `term is-tty --stdout` * `term is-tty stdout` * `term is-terminal stdout` If multiple streams are provided an error is returned. The error span covers all arguments as the incompatible one is not known. This may be new? Fixes #10517 # User-Facing Changes * Add `is-terminal` to check if stdin, stdout, or stderr are a terminal (TTY) # Tests + Formatting The nu tests always redirect stdin, stdout, and stderr so a positive test case is not possible without extra work - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` # After Submitting The new command will be added automatically --------- Co-authored-by: Darren Schroeder <343840+fdncred@users.noreply.github.com>
This commit is contained in:
parent
64288b4350
commit
d5677625a7
@ -225,6 +225,7 @@ pub fn add_shell_command_context(mut engine_state: EngineState) -> EngineState {
|
||||
Input,
|
||||
InputList,
|
||||
InputListen,
|
||||
IsTerminal,
|
||||
Kill,
|
||||
Sleep,
|
||||
TermSize,
|
||||
|
78
crates/nu-command/src/platform/is_terminal.rs
Normal file
78
crates/nu-command/src/platform/is_terminal.rs
Normal file
@ -0,0 +1,78 @@
|
||||
use nu_protocol::{
|
||||
ast::Call,
|
||||
engine::{Command, EngineState, Stack},
|
||||
span, Category, Example, PipelineData, ShellError, Signature, Type, Value,
|
||||
};
|
||||
use std::io::IsTerminal as _;
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct IsTerminal;
|
||||
|
||||
impl Command for IsTerminal {
|
||||
fn name(&self) -> &str {
|
||||
"is-terminal"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("is-terminal")
|
||||
.input_output_type(Type::Nothing, Type::Bool)
|
||||
.switch("stdin", "Check if stdin is a terminal", Some('i'))
|
||||
.switch("stdout", "Check if stdout is a terminal", Some('o'))
|
||||
.switch("stderr", "Check if stderr is a terminal", Some('e'))
|
||||
.category(Category::Platform)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Check if stdin, stdout, or stderr is a terminal"
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![Example {
|
||||
description: r#"Return "terminal attached" if standard input is attached to a terminal, and "no terminal" if not."#,
|
||||
example: r#"if (is-terminal --stdin) { "terminal attached" } else { "no terminal" }"#,
|
||||
result: Some(Value::test_string("terminal attached")),
|
||||
}]
|
||||
}
|
||||
|
||||
fn search_terms(&self) -> Vec<&str> {
|
||||
vec!["input", "output", "stdin", "stdout", "stderr", "tty"]
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
_engine_state: &EngineState,
|
||||
_stack: &mut Stack,
|
||||
call: &Call,
|
||||
_input: PipelineData,
|
||||
) -> Result<PipelineData, ShellError> {
|
||||
let stdin = call.has_flag("stdin");
|
||||
let stdout = call.has_flag("stdout");
|
||||
let stderr = call.has_flag("stderr");
|
||||
|
||||
let is_terminal = match (stdin, stdout, stderr) {
|
||||
(true, false, false) => std::io::stdin().is_terminal(),
|
||||
(false, true, false) => std::io::stdout().is_terminal(),
|
||||
(false, false, true) => std::io::stderr().is_terminal(),
|
||||
(false, false, false) => {
|
||||
return Err(ShellError::MissingParameter {
|
||||
param_name: "one of --stdin, --stdout, --stderr".into(),
|
||||
span: call.head,
|
||||
});
|
||||
}
|
||||
_ => {
|
||||
let spans: Vec<_> = call.arguments.iter().map(|arg| arg.span()).collect();
|
||||
let span = span(&spans);
|
||||
|
||||
return Err(ShellError::IncompatibleParametersSingle {
|
||||
msg: "Only one stream may be checked".into(),
|
||||
span,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
Ok(PipelineData::Value(
|
||||
Value::bool(is_terminal, call.head),
|
||||
None,
|
||||
))
|
||||
}
|
||||
}
|
@ -3,6 +3,7 @@ mod clear;
|
||||
mod dir_info;
|
||||
mod du;
|
||||
mod input;
|
||||
mod is_terminal;
|
||||
mod kill;
|
||||
mod sleep;
|
||||
mod term_size;
|
||||
@ -15,6 +16,7 @@ pub use du::Du;
|
||||
pub use input::Input;
|
||||
pub use input::InputList;
|
||||
pub use input::InputListen;
|
||||
pub use is_terminal::IsTerminal;
|
||||
pub use kill::Kill;
|
||||
pub use sleep::Sleep;
|
||||
pub use term_size::TermSize;
|
||||
|
@ -100,6 +100,7 @@ mod split_row;
|
||||
mod str_;
|
||||
mod table;
|
||||
mod take;
|
||||
mod terminal;
|
||||
mod to_text;
|
||||
mod touch;
|
||||
mod transpose;
|
||||
|
24
crates/nu-command/tests/commands/terminal.rs
Normal file
24
crates/nu-command/tests/commands/terminal.rs
Normal file
@ -0,0 +1,24 @@
|
||||
use nu_test_support::{nu, pipeline};
|
||||
|
||||
// Inside nu! stdout is piped so it won't be a terminal
|
||||
#[test]
|
||||
fn is_terminal_stdout_piped() {
|
||||
let actual = nu!(pipeline(
|
||||
r#"
|
||||
is-terminal --stdout
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "false");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn is_terminal_two_streams() {
|
||||
let actual = nu!(pipeline(
|
||||
r#"
|
||||
is-terminal --stdin --stderr
|
||||
"#
|
||||
));
|
||||
|
||||
assert!(actual.err.contains("Only one stream may be checked"));
|
||||
}
|
Loading…
Reference in New Issue
Block a user