mirror of
https://github.com/nushell/nushell.git
synced 2025-05-13 14:34:28 +02:00
# Description The meaning of the word usage is specific to describing how a command function is *used* and not a synonym for general description. Usage can be used to describe the SYNOPSIS or EXAMPLES sections of a man page where the permitted argument combinations are shown or example *uses* are given. Let's not confuse people and call it what it is a description. Our `help` command already creates its own *Usage* section based on the available arguments and doesn't refer to the description with usage. # User-Facing Changes `help commands` and `scope commands` will now use `description` or `extra_description` `usage`-> `description` `extra_usage` -> `extra_description` Breaking change in the plugin protocol: In the signature record communicated with the engine. `usage`-> `description` `extra_usage` -> `extra_description` The same rename also takes place for the methods on `SimplePluginCommand` and `PluginCommand` # Tests + Formatting - Updated plugin protocol specific changes # After Submitting - [ ] update plugin protocol doc
72 lines
2.4 KiB
Rust
72 lines
2.4 KiB
Rust
use nu_engine::command_prelude::*;
|
|
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 description(&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(engine_state, stack, "stdin")?;
|
|
let stdout = call.has_flag(engine_state, stack, "stdout")?;
|
|
let stderr = call.has_flag(engine_state, stack, "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,
|
|
});
|
|
}
|
|
_ => {
|
|
return Err(ShellError::IncompatibleParametersSingle {
|
|
msg: "Only one stream may be checked".into(),
|
|
span: call.arguments_span(),
|
|
});
|
|
}
|
|
};
|
|
|
|
Ok(PipelineData::Value(
|
|
Value::bool(is_terminal, call.head),
|
|
None,
|
|
))
|
|
}
|
|
}
|