mirror of
https://github.com/nushell/nushell
synced 2025-01-23 10:25:22 +00:00
193b00764b
* Moves off of draining between filters. Instead, the sink will pull on the stream, and will drain element-wise. This moves the whole stream to being lazy. * Adds ctrl-c support and connects it into some of the key points where we pull on the stream. If a ctrl-c is detect, we immediately halt pulling on the stream and return to the prompt. * Moves away from having a SourceMap where anchor locations are stored. Now AnchorLocation is kept directly in the Tag. * To make this possible, split tag and span. Span is largely used in the parser and is copyable. Tag is now no longer copyable.
70 lines
2.2 KiB
Rust
70 lines
2.2 KiB
Rust
use crate::commands::command::Command;
|
|
use crate::data::{TaggedDictBuilder, TaggedListBuilder, Value};
|
|
use crate::parser::registry::{NamedType, PositionalType, Signature};
|
|
use crate::prelude::*;
|
|
use std::ops::Deref;
|
|
|
|
pub(crate) fn command_dict(command: Arc<Command>, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
let tag = tag.into();
|
|
|
|
let mut cmd_dict = TaggedDictBuilder::new(&tag);
|
|
|
|
cmd_dict.insert("name", Value::string(command.name()));
|
|
|
|
cmd_dict.insert(
|
|
"type",
|
|
Value::string(match command.deref() {
|
|
Command::WholeStream(_) => "Command",
|
|
Command::PerItem(_) => "Filter",
|
|
}),
|
|
);
|
|
|
|
cmd_dict.insert_tagged("signature", signature_dict(command.signature(), tag));
|
|
cmd_dict.insert("usage", Value::string(command.usage()));
|
|
|
|
cmd_dict.into_tagged_value()
|
|
}
|
|
|
|
fn for_spec(name: &str, ty: &str, required: bool, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
let tag = tag.into();
|
|
|
|
let mut spec = TaggedDictBuilder::new(tag);
|
|
|
|
spec.insert("name", Value::string(name));
|
|
spec.insert("type", Value::string(ty));
|
|
spec.insert(
|
|
"required",
|
|
Value::string(if required { "yes" } else { "no" }),
|
|
);
|
|
|
|
spec.into_tagged_value()
|
|
}
|
|
|
|
fn signature_dict(signature: Signature, tag: impl Into<Tag>) -> Tagged<Value> {
|
|
let tag = tag.into();
|
|
let mut sig = TaggedListBuilder::new(&tag);
|
|
|
|
for arg in signature.positional.iter() {
|
|
let is_required = match arg {
|
|
PositionalType::Mandatory(_, _) => true,
|
|
PositionalType::Optional(_, _) => false,
|
|
};
|
|
|
|
sig.insert_tagged(for_spec(arg.name(), "argument", is_required, &tag));
|
|
}
|
|
|
|
if let Some(_) = signature.rest_positional {
|
|
let is_required = false;
|
|
sig.insert_tagged(for_spec("rest", "argument", is_required, &tag));
|
|
}
|
|
|
|
for (name, ty) in signature.named.iter() {
|
|
match ty {
|
|
NamedType::Mandatory(_) => sig.insert_tagged(for_spec(name, "flag", true, &tag)),
|
|
NamedType::Optional(_) => sig.insert_tagged(for_spec(name, "flag", false, &tag)),
|
|
NamedType::Switch => sig.insert_tagged(for_spec(name, "switch", false, &tag)),
|
|
}
|
|
}
|
|
|
|
sig.into_tagged_value()
|
|
}
|