mirror of
https://github.com/nushell/nushell
synced 2024-12-27 21:43:09 +00:00
Add metadata command (#569)
* Add metadata command * Add string interpolation to testing
This commit is contained in:
parent
b719f8d4eb
commit
7f0921a14b
6 changed files with 135 additions and 20 deletions
|
@ -168,24 +168,23 @@ impl Command for For {
|
||||||
span,
|
span,
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
// FIXME? Numbered `for` is kinda strange, but was supported in previous nushell
|
Example {
|
||||||
// Example {
|
description: "Number each item and echo a message",
|
||||||
// description: "Number each item and echo a message",
|
example: "for $it in ['bob' 'fred'] --numbered { $\"($it.index) is ($it.item)\" }",
|
||||||
// example: "for $it in ['bob' 'fred'] --numbered { $\"($it.index) is ($it.item)\" }",
|
result: Some(Value::List {
|
||||||
// result: Some(Value::List {
|
vals: vec![
|
||||||
// vals: vec![
|
Value::String {
|
||||||
// Value::String {
|
val: "0 is bob".into(),
|
||||||
// val: "0 is bob".into(),
|
span,
|
||||||
// span,
|
},
|
||||||
// },
|
Value::String {
|
||||||
// Value::String {
|
val: "1 is fred".into(),
|
||||||
// val: "0 is fred".into(),
|
span,
|
||||||
// span,
|
},
|
||||||
// },
|
],
|
||||||
// ],
|
span,
|
||||||
// span,
|
}),
|
||||||
// }),
|
},
|
||||||
// },
|
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
103
crates/nu-command/src/core_commands/metadata.rs
Normal file
103
crates/nu-command/src/core_commands/metadata.rs
Normal file
|
@ -0,0 +1,103 @@
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, DataSource, Example, PipelineData, PipelineMetadata, Signature, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Metadata;
|
||||||
|
|
||||||
|
impl Command for Metadata {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"metadata"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Get the metadata for items in the stream"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
|
Signature::build("metadata").category(Category::Core)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
_stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
|
let ctrlc = engine_state.ctrlc.clone();
|
||||||
|
|
||||||
|
let metadata = input.metadata();
|
||||||
|
|
||||||
|
input.map(
|
||||||
|
move |x| {
|
||||||
|
let span = x.span();
|
||||||
|
|
||||||
|
let mut cols = vec![];
|
||||||
|
let mut vals = vec![];
|
||||||
|
|
||||||
|
cols.push("span".into());
|
||||||
|
if let Ok(span) = span {
|
||||||
|
vals.push(Value::Record {
|
||||||
|
cols: vec!["start".into(), "end".into()],
|
||||||
|
vals: vec![
|
||||||
|
Value::Int {
|
||||||
|
val: span.start as i64,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
Value::Int {
|
||||||
|
val: span.end as i64,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
span: head,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(x) = &metadata {
|
||||||
|
match x {
|
||||||
|
PipelineMetadata {
|
||||||
|
data_source: DataSource::Ls,
|
||||||
|
} => {
|
||||||
|
cols.push("source".into());
|
||||||
|
vals.push(Value::String {
|
||||||
|
val: "ls".into(),
|
||||||
|
span: head,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Value::Record {
|
||||||
|
cols,
|
||||||
|
vals,
|
||||||
|
span: head,
|
||||||
|
}
|
||||||
|
},
|
||||||
|
ctrlc,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![Example {
|
||||||
|
description: "Get the metadata of a value",
|
||||||
|
example: "3 | metadata",
|
||||||
|
result: None,
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(Metadata {})
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,6 +13,7 @@ mod hide;
|
||||||
mod history;
|
mod history;
|
||||||
mod if_;
|
mod if_;
|
||||||
mod let_;
|
mod let_;
|
||||||
|
mod metadata;
|
||||||
mod module;
|
mod module;
|
||||||
mod source;
|
mod source;
|
||||||
mod use_;
|
mod use_;
|
||||||
|
@ -33,6 +34,7 @@ pub use hide::Hide;
|
||||||
pub use history::History;
|
pub use history::History;
|
||||||
pub use if_::If;
|
pub use if_::If;
|
||||||
pub use let_::Let;
|
pub use let_::Let;
|
||||||
|
pub use metadata::Metadata;
|
||||||
pub use module::Module;
|
pub use module::Module;
|
||||||
pub use source::Source;
|
pub use source::Source;
|
||||||
pub use use_::Use;
|
pub use use_::Use;
|
||||||
|
|
|
@ -38,6 +38,7 @@ pub fn create_default_context() -> EngineState {
|
||||||
History,
|
History,
|
||||||
If,
|
If,
|
||||||
Let,
|
Let,
|
||||||
|
Metadata,
|
||||||
Module,
|
Module,
|
||||||
Source,
|
Source,
|
||||||
Use,
|
Use,
|
||||||
|
|
|
@ -16,6 +16,8 @@ use super::{Ansi, Date, From, Into, Math, Path, Random, Split, Str, StrCollect,
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub fn test_examples(cmd: impl Command + 'static) {
|
pub fn test_examples(cmd: impl Command + 'static) {
|
||||||
|
use crate::BuildString;
|
||||||
|
|
||||||
let examples = cmd.examples();
|
let examples = cmd.examples();
|
||||||
let mut engine_state = Box::new(EngineState::new());
|
let mut engine_state = Box::new(EngineState::new());
|
||||||
|
|
||||||
|
@ -25,6 +27,7 @@ pub fn test_examples(cmd: impl Command + 'static) {
|
||||||
let mut working_set = StateWorkingSet::new(&*engine_state);
|
let mut working_set = StateWorkingSet::new(&*engine_state);
|
||||||
working_set.add_decl(Box::new(Str));
|
working_set.add_decl(Box::new(Str));
|
||||||
working_set.add_decl(Box::new(StrCollect));
|
working_set.add_decl(Box::new(StrCollect));
|
||||||
|
working_set.add_decl(Box::new(BuildString));
|
||||||
working_set.add_decl(Box::new(From));
|
working_set.add_decl(Box::new(From));
|
||||||
working_set.add_decl(Box::new(To));
|
working_set.add_decl(Box::new(To));
|
||||||
working_set.add_decl(Box::new(Into));
|
working_set.add_decl(Box::new(Into));
|
||||||
|
|
|
@ -37,12 +37,12 @@ pub enum PipelineData {
|
||||||
Stream(ValueStream, Option<PipelineMetadata>),
|
Stream(ValueStream, Option<PipelineMetadata>),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct PipelineMetadata {
|
pub struct PipelineMetadata {
|
||||||
pub data_source: DataSource,
|
pub data_source: DataSource,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Clone)]
|
||||||
pub enum DataSource {
|
pub enum DataSource {
|
||||||
Ls,
|
Ls,
|
||||||
}
|
}
|
||||||
|
@ -52,6 +52,13 @@ impl PipelineData {
|
||||||
PipelineData::Value(Value::Nothing { span }, None)
|
PipelineData::Value(Value::Nothing { span }, None)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn metadata(&self) -> Option<PipelineMetadata> {
|
||||||
|
match self {
|
||||||
|
PipelineData::Stream(_, x) => x.clone(),
|
||||||
|
PipelineData::Value(_, x) => x.clone(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn into_value(self, span: Span) -> Value {
|
pub fn into_value(self, span: Span) -> Value {
|
||||||
match self {
|
match self {
|
||||||
PipelineData::Value(Value::Nothing { .. }, ..) => Value::nothing(span),
|
PipelineData::Value(Value::Nothing { .. }, ..) => Value::nothing(span),
|
||||||
|
|
Loading…
Reference in a new issue