Consistent wrap (#7159)

* Consistent wrap

* Signature fix
This commit is contained in:
Nano 2022-11-19 09:39:40 +12:00 committed by GitHub
parent 2dd4cb9f7d
commit bd30ea723e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -20,7 +20,10 @@ impl Command for Wrap {
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("wrap") Signature::build("wrap")
.input_output_types(vec![(Type::List(Box::new(Type::Any)), Type::Table(vec![]))]) .input_output_types(vec![
(Type::List(Box::new(Type::Any)), Type::Table(vec![])),
(Type::Range, Type::Table(vec![])),
])
.required("name", SyntaxShape::String, "the name of the column") .required("name", SyntaxShape::String, "the name of the column")
.category(Category::Filters) .category(Category::Filters)
} }
@ -36,7 +39,9 @@ impl Command for Wrap {
let name: String = call.req(engine_state, stack, 0)?; let name: String = call.req(engine_state, stack, 0)?;
match input { match input {
PipelineData::Value(Value::List { vals, .. }, ..) => Ok(vals PipelineData::Value(Value::Range { .. }, ..)
| PipelineData::Value(Value::List { .. }, ..)
| PipelineData::ListStream { .. } => Ok(input
.into_iter() .into_iter()
.map(move |x| Value::Record { .map(move |x| Value::Record {
cols: vec![name.clone()], cols: vec![name.clone()],
@ -44,13 +49,6 @@ impl Command for Wrap {
span, span,
}) })
.into_pipeline_data(engine_state.ctrlc.clone())), .into_pipeline_data(engine_state.ctrlc.clone())),
PipelineData::ListStream(stream, ..) => Ok(stream
.map(move |x| Value::Record {
cols: vec![name.clone()],
vals: vec![x],
span,
})
.into_pipeline_data(engine_state.ctrlc.clone())),
PipelineData::ExternalStream { .. } => Ok(Value::Record { PipelineData::ExternalStream { .. } => Ok(Value::Record {
cols: vec![name], cols: vec![name],
vals: vec![input.into_value(call.head)], vals: vec![input.into_value(call.head)],
@ -67,30 +65,56 @@ impl Command for Wrap {
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
vec![Example { vec![
description: "Wrap a list into a table with a given column name", Example {
example: "[1 2 3] | wrap num", description: "Wrap a list into a table with a given column name",
result: Some(Value::List { example: "[1 2 3] | wrap num",
vals: vec![ result: Some(Value::List {
Value::Record { vals: vec![
cols: vec!["num".into()], Value::Record {
vals: vec![Value::test_int(1)], cols: vec!["num".into()],
span: Span::test_data(), vals: vec![Value::test_int(1)],
}, span: Span::test_data(),
Value::Record { },
cols: vec!["num".into()], Value::Record {
vals: vec![Value::test_int(2)], cols: vec!["num".into()],
span: Span::test_data(), vals: vec![Value::test_int(2)],
}, span: Span::test_data(),
Value::Record { },
cols: vec!["num".into()], Value::Record {
vals: vec![Value::test_int(3)], cols: vec!["num".into()],
span: Span::test_data(), vals: vec![Value::test_int(3)],
}, span: Span::test_data(),
], },
span: Span::test_data(), ],
}), span: Span::test_data(),
}] }),
},
Example {
description: "Wrap a range into a table with a given column name",
example: "1..3 | wrap num",
result: Some(Value::List {
vals: vec![
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(1)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(2)],
span: Span::test_data(),
},
Value::Record {
cols: vec!["num".into()],
vals: vec![Value::test_int(3)],
span: Span::test_data(),
},
],
span: Span::test_data(),
}),
},
]
} }
} }