mirror of
https://github.com/nushell/nushell.git
synced 2025-02-08 06:30:00 +01:00
522a828687
* Refactor InputStream and affected commands. First, making `values` private and leaning on the `Stream` implementation makes consumes of `InputStream` less likely to have to change in the future, if we change what an `InputStream` is internally. Second, we're dropping `Option<InputStream>` as the input to pipelines, internals, and externals. Instead, `InputStream.is_empty` can be used to check for "emptiness". Empty streams are typically only ever used as the first input to a pipeline. * Add run_external internal command. We want to push external commands closer to internal commands, eventually eliminating the concept of "external" completely. This means we can consolidate a couple of things: - Variable evaluation (for example, `$it`, `$nu`, alias vars) - Behaviour of whole stream vs per-item external execution It should also make it easier for us to start introducing argument signatures for external commands, * Update run_external.rs * Update run_external.rs * Update run_external.rs * Update run_external.rs Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
111 lines
2.9 KiB
Rust
111 lines
2.9 KiB
Rust
use crate::commands::WholeStreamCommand;
|
|
use crate::prelude::*;
|
|
use indexmap::map::IndexMap;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
|
use nu_source::Tagged;
|
|
|
|
pub struct Which;
|
|
|
|
impl WholeStreamCommand for Which {
|
|
fn name(&self) -> &str {
|
|
"which"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("which")
|
|
.required("application", SyntaxShape::String, "application")
|
|
.switch("all", "list all executables", Some('a'))
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Finds a program file."
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
args: CommandArgs,
|
|
registry: &CommandRegistry,
|
|
) -> Result<OutputStream, ShellError> {
|
|
args.process(registry, which)?.run()
|
|
}
|
|
}
|
|
|
|
/// Shortcuts for creating an entry to the output table
|
|
fn entry(arg: impl Into<String>, path: Value, builtin: bool, tag: Tag) -> Value {
|
|
let mut map = IndexMap::new();
|
|
map.insert(
|
|
"arg".to_string(),
|
|
UntaggedValue::Primitive(Primitive::String(arg.into())).into_value(tag.clone()),
|
|
);
|
|
map.insert("path".to_string(), path);
|
|
map.insert(
|
|
"builtin".to_string(),
|
|
UntaggedValue::Primitive(Primitive::Boolean(builtin)).into_value(tag.clone()),
|
|
);
|
|
|
|
UntaggedValue::row(map).into_value(tag)
|
|
}
|
|
|
|
macro_rules! entry_builtin {
|
|
($arg:expr, $tag:expr) => {
|
|
entry(
|
|
$arg.clone(),
|
|
UntaggedValue::Primitive(Primitive::String("nushell built-in command".to_string()))
|
|
.into_value($tag.clone()),
|
|
true,
|
|
$tag,
|
|
)
|
|
};
|
|
}
|
|
|
|
macro_rules! entry_path {
|
|
($arg:expr, $path:expr, $tag:expr) => {
|
|
entry(
|
|
$arg.clone(),
|
|
UntaggedValue::Primitive(Primitive::Path($path)).into_value($tag.clone()),
|
|
false,
|
|
$tag,
|
|
)
|
|
};
|
|
}
|
|
|
|
#[derive(Deserialize, Debug)]
|
|
struct WhichArgs {
|
|
application: Tagged<String>,
|
|
all: bool,
|
|
}
|
|
|
|
fn which(
|
|
WhichArgs { application, all }: WhichArgs,
|
|
RunnableContext { registry, .. }: RunnableContext,
|
|
) -> Result<OutputStream, ShellError> {
|
|
let external = application.starts_with('^');
|
|
let item = if external {
|
|
application.item[1..].to_string()
|
|
} else {
|
|
application.item.clone()
|
|
};
|
|
|
|
let stream = async_stream! {
|
|
if !external {
|
|
let builtin = registry.has(&item);
|
|
if builtin {
|
|
yield ReturnSuccess::value(entry_builtin!(item, application.tag.clone()));
|
|
}
|
|
}
|
|
|
|
if let Ok(paths) = ichwh::which_all(&item).await {
|
|
for path in paths {
|
|
yield ReturnSuccess::value(entry_path!(item, path.into(), application.tag.clone()));
|
|
}
|
|
}
|
|
};
|
|
|
|
if all {
|
|
Ok(stream.to_output_stream())
|
|
} else {
|
|
Ok(stream.take(1).to_output_stream())
|
|
}
|
|
}
|