mirror of
https://github.com/nushell/nushell.git
synced 2025-04-09 21:28:55 +02:00
parent
852ec3f9a0
commit
b16b3c0b7f
@ -138,6 +138,7 @@ pub fn create_default_context() -> EngineState {
|
|||||||
Upsert,
|
Upsert,
|
||||||
Update,
|
Update,
|
||||||
UpdateCells,
|
UpdateCells,
|
||||||
|
Values,
|
||||||
Where,
|
Where,
|
||||||
Window,
|
Window,
|
||||||
Wrap,
|
Wrap,
|
||||||
|
@ -9,8 +9,9 @@ pub fn test_examples(cmd: impl Command + 'static) {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test_examples {
|
mod test_examples {
|
||||||
use super::super::{
|
use super::super::{
|
||||||
Ansi, Date, Echo, From, If, Into, Let, LetEnv, Math, MathEuler, MathPi, MathRound, Path,
|
Ansi, Date, Echo, From, If, Into, IntoString, Let, LetEnv, Math, MathEuler, MathPi,
|
||||||
Random, Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace, Url, Wrap,
|
MathRound, Path, Random, Split, SplitColumn, SplitRow, Str, StrJoin, StrLength, StrReplace,
|
||||||
|
Url, Values, Wrap,
|
||||||
};
|
};
|
||||||
use crate::{Break, Mut, To};
|
use crate::{Break, Mut, To};
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
@ -69,6 +70,7 @@ mod test_examples {
|
|||||||
working_set.add_decl(Box::new(If));
|
working_set.add_decl(Box::new(If));
|
||||||
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));
|
||||||
|
working_set.add_decl(Box::new(IntoString));
|
||||||
working_set.add_decl(Box::new(Random));
|
working_set.add_decl(Box::new(Random));
|
||||||
working_set.add_decl(Box::new(Split));
|
working_set.add_decl(Box::new(Split));
|
||||||
working_set.add_decl(Box::new(SplitColumn));
|
working_set.add_decl(Box::new(SplitColumn));
|
||||||
@ -77,6 +79,7 @@ mod test_examples {
|
|||||||
working_set.add_decl(Box::new(Path));
|
working_set.add_decl(Box::new(Path));
|
||||||
working_set.add_decl(Box::new(Date));
|
working_set.add_decl(Box::new(Date));
|
||||||
working_set.add_decl(Box::new(Url));
|
working_set.add_decl(Box::new(Url));
|
||||||
|
working_set.add_decl(Box::new(Values));
|
||||||
working_set.add_decl(Box::new(Ansi));
|
working_set.add_decl(Box::new(Ansi));
|
||||||
working_set.add_decl(Box::new(Wrap));
|
working_set.add_decl(Box::new(Wrap));
|
||||||
working_set.add_decl(Box::new(LetEnv));
|
working_set.add_decl(Box::new(LetEnv));
|
||||||
|
@ -24,7 +24,11 @@ impl Command for Columns {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Show the columns in the input."
|
"Given a record or table, produce a list of its columns' names."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
"This is a counterpart to `values`, which produces a list of columns' values."
|
||||||
}
|
}
|
||||||
|
|
||||||
fn examples(&self) -> Vec<Example> {
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
@ -73,20 +73,29 @@ with 'transpose' first."#
|
|||||||
vec![
|
vec![
|
||||||
Example {
|
Example {
|
||||||
example: "[1 2 3] | each {|e| 2 * $e }",
|
example: "[1 2 3] | each {|e| 2 * $e }",
|
||||||
description: "Multiplies elements in list",
|
description: "Multiplies elements in the list",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: stream_test_1,
|
vals: stream_test_1,
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
example: "{major:2, minor:1, patch:4} | values | each { into string }",
|
||||||
|
description: "Produce a list of values in the record, converted to string",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_string("2"),
|
||||||
|
Value::test_string("1"),
|
||||||
|
Value::test_string("4"),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
Example {
|
Example {
|
||||||
example: r#"[1 2 3 2] | each {|e| if $e == 2 { "two" } }"#,
|
example: r#"[1 2 3 2] | each {|e| if $e == 2 { "two" } }"#,
|
||||||
description: "Produce a list that has \"two\" for each 2 in the input",
|
description: "Produce a list that has \"two\" for each 2 in the input",
|
||||||
result: Some(Value::List {
|
result: Some(Value::List {
|
||||||
vals: vec![
|
vals: vec![Value::test_string("two"), Value::test_string("two")],
|
||||||
Value::string("two", Span::test_data()),
|
|
||||||
Value::string("two", Span::test_data()),
|
|
||||||
],
|
|
||||||
span: Span::test_data(),
|
span: Span::test_data(),
|
||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
|
@ -47,6 +47,7 @@ mod update;
|
|||||||
mod update_cells;
|
mod update_cells;
|
||||||
mod upsert;
|
mod upsert;
|
||||||
mod utils;
|
mod utils;
|
||||||
|
mod values;
|
||||||
mod where_;
|
mod where_;
|
||||||
mod window;
|
mod window;
|
||||||
mod wrap;
|
mod wrap;
|
||||||
@ -100,6 +101,7 @@ pub use uniq_by::UniqBy;
|
|||||||
pub use update::Update;
|
pub use update::Update;
|
||||||
pub use update_cells::UpdateCells;
|
pub use update_cells::UpdateCells;
|
||||||
pub use upsert::Upsert;
|
pub use upsert::Upsert;
|
||||||
|
pub use values::Values;
|
||||||
pub use where_::Where;
|
pub use where_::Where;
|
||||||
pub use window::Window;
|
pub use window::Window;
|
||||||
pub use wrap::Wrap;
|
pub use wrap::Wrap;
|
||||||
|
@ -274,7 +274,7 @@ pub fn transpose(
|
|||||||
}
|
}
|
||||||
v => Value::List {
|
v => Value::List {
|
||||||
vals: vec![v.clone(), x.clone()],
|
vals: vec![v.clone(), x.clone()],
|
||||||
span: v.span().expect("this should be a valid span"),
|
span: v.expect_span(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
cols.remove(index);
|
cols.remove(index);
|
||||||
@ -313,7 +313,7 @@ pub fn transpose(
|
|||||||
}
|
}
|
||||||
v => Value::List {
|
v => Value::List {
|
||||||
vals: vec![v.clone(), Value::nothing(name)],
|
vals: vec![v.clone(), Value::nothing(name)],
|
||||||
span: v.span().expect("this should be a valid span"),
|
span: v.expect_span(),
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
cols.remove(index);
|
cols.remove(index);
|
||||||
|
213
crates/nu-command/src/filters/values.rs
Normal file
213
crates/nu-command/src/filters/values.rs
Normal file
@ -0,0 +1,213 @@
|
|||||||
|
use indexmap::IndexMap;
|
||||||
|
use nu_protocol::ast::Call;
|
||||||
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
use nu_protocol::{
|
||||||
|
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
|
||||||
|
Type, Value,
|
||||||
|
};
|
||||||
|
|
||||||
|
#[derive(Clone)]
|
||||||
|
pub struct Values;
|
||||||
|
|
||||||
|
impl Command for Values {
|
||||||
|
fn name(&self) -> &str {
|
||||||
|
"values"
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature(&self) -> Signature {
|
||||||
|
Signature::build(self.name())
|
||||||
|
.input_output_types(vec![
|
||||||
|
(Type::Record(vec![]), Type::List(Box::new(Type::Any))),
|
||||||
|
(Type::Table(vec![]), Type::List(Box::new(Type::Any))),
|
||||||
|
])
|
||||||
|
.category(Category::Filters)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn usage(&self) -> &str {
|
||||||
|
"Given a record or table, produce a list of its columns' values."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn extra_usage(&self) -> &str {
|
||||||
|
"This is a counterpart to `columns`, which produces a list of columns' names."
|
||||||
|
}
|
||||||
|
|
||||||
|
fn examples(&self) -> Vec<Example> {
|
||||||
|
vec![
|
||||||
|
Example {
|
||||||
|
example: "{ mode:normal userid:31415 } | values",
|
||||||
|
description: "Get the values from the record (produce a list)",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![Value::test_string("normal"), Value::test_int(31415)],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
example: "{ f:250 g:191 c:128 d:1024 e:2000 a:16 b:32 } | values",
|
||||||
|
description: "Values are ordered by the column order of the record",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_int(250),
|
||||||
|
Value::test_int(191),
|
||||||
|
Value::test_int(128),
|
||||||
|
Value::test_int(1024),
|
||||||
|
Value::test_int(2000),
|
||||||
|
Value::test_int(16),
|
||||||
|
Value::test_int(32),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
Example {
|
||||||
|
example: "[[name meaning]; [ls list] [mv move] [cd 'change directory']] | values",
|
||||||
|
description: "Get the values from the table (produce a list of lists)",
|
||||||
|
result: Some(Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_string("ls"),
|
||||||
|
Value::test_string("mv"),
|
||||||
|
Value::test_string("cd"),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
},
|
||||||
|
Value::List {
|
||||||
|
vals: vec![
|
||||||
|
Value::test_string("list"),
|
||||||
|
Value::test_string("move"),
|
||||||
|
Value::test_string("change directory"),
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
},
|
||||||
|
],
|
||||||
|
span: Span::test_data(),
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run(
|
||||||
|
&self,
|
||||||
|
engine_state: &EngineState,
|
||||||
|
_stack: &mut Stack,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let span = call.head;
|
||||||
|
values(engine_state, span, input)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// The semantics of `values` are as follows:
|
||||||
|
// For each column, get the values for that column, in row order.
|
||||||
|
// Holes are not preserved, i.e. position in the resulting list
|
||||||
|
// does not necessarily equal row number.
|
||||||
|
pub fn get_values<'a>(
|
||||||
|
input: impl IntoIterator<Item = &'a Value>,
|
||||||
|
head: Span,
|
||||||
|
input_span: Span,
|
||||||
|
) -> Result<Vec<Value>, ShellError> {
|
||||||
|
let mut output: IndexMap<String, Vec<Value>> = IndexMap::new();
|
||||||
|
|
||||||
|
for item in input {
|
||||||
|
match item {
|
||||||
|
Value::Record { cols, vals, .. } => {
|
||||||
|
for (k, v) in cols.iter().zip(vals.iter()) {
|
||||||
|
if let Some(vec) = output.get_mut(k) {
|
||||||
|
vec.push(v.clone());
|
||||||
|
} else {
|
||||||
|
output.insert(k.clone(), vec![v.clone()]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Value::Error { error } => return Err(error.clone()),
|
||||||
|
_ => {
|
||||||
|
return Err(ShellError::OnlySupportsThisInputType(
|
||||||
|
"record or table".into(),
|
||||||
|
item.get_type().to_string(),
|
||||||
|
head,
|
||||||
|
input_span,
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(output.into_values().map(|v| Value::list(v, head)).collect())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn values(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
head: Span,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
let ctrlc = engine_state.ctrlc.clone();
|
||||||
|
let metadata = input.metadata();
|
||||||
|
match input {
|
||||||
|
PipelineData::Empty => Ok(PipelineData::Empty),
|
||||||
|
PipelineData::Value(Value::List { vals, span }, ..) => {
|
||||||
|
match get_values(&vals, head, span) {
|
||||||
|
Ok(cols) => Ok(cols
|
||||||
|
.into_iter()
|
||||||
|
.into_pipeline_data(ctrlc)
|
||||||
|
.set_metadata(metadata)),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PipelineData::Value(Value::CustomValue { val, span }, ..) => {
|
||||||
|
let input_as_base_value = val.to_base_value(span)?;
|
||||||
|
match get_values(&[input_as_base_value], head, span) {
|
||||||
|
Ok(cols) => Ok(cols
|
||||||
|
.into_iter()
|
||||||
|
.into_pipeline_data(ctrlc)
|
||||||
|
.set_metadata(metadata)),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PipelineData::ListStream(stream, ..) => {
|
||||||
|
let vals: Vec<_> = stream.into_iter().collect();
|
||||||
|
match get_values(&vals, head, head) {
|
||||||
|
Ok(cols) => Ok(cols
|
||||||
|
.into_iter()
|
||||||
|
.into_pipeline_data(ctrlc)
|
||||||
|
.set_metadata(metadata)),
|
||||||
|
Err(err) => Err(err),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
PipelineData::Value(Value::Record { vals, .. }, ..) => {
|
||||||
|
Ok(vals.into_pipeline_data(ctrlc).set_metadata(metadata))
|
||||||
|
}
|
||||||
|
// Propagate errors
|
||||||
|
PipelineData::Value(Value::Error { error: _ }, ..) => Ok(input),
|
||||||
|
PipelineData::Value(other, ..) => {
|
||||||
|
Err(ShellError::OnlySupportsThisInputType(
|
||||||
|
"record or table".into(),
|
||||||
|
other.get_type().to_string(),
|
||||||
|
head,
|
||||||
|
// This line requires the Value::Error match above.
|
||||||
|
other.expect_span(),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
PipelineData::ExternalStream { .. } => {
|
||||||
|
Err(ShellError::OnlySupportsThisInputType(
|
||||||
|
"record or table".into(),
|
||||||
|
"raw data".into(),
|
||||||
|
head,
|
||||||
|
// This line requires the PipelineData::Empty and PipelineData::ListStream matches above.
|
||||||
|
input
|
||||||
|
.span()
|
||||||
|
.expect("PipelineData::ExternalStream had no span"),
|
||||||
|
))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_examples() {
|
||||||
|
use crate::test_examples;
|
||||||
|
|
||||||
|
test_examples(Values {})
|
||||||
|
}
|
||||||
|
}
|
@ -723,7 +723,7 @@ Format: #
|
|||||||
_ => {
|
_ => {
|
||||||
return Err(ShellError::IncompatibleParametersSingle(
|
return Err(ShellError::IncompatibleParametersSingle(
|
||||||
format!("problem with key: {}", k),
|
format!("problem with key: {}", k),
|
||||||
code.span().expect("error with span"),
|
code.expect_span(),
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -654,7 +654,7 @@ fn handle_row_stream(
|
|||||||
ctrlc,
|
ctrlc,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
// Next, `into html -l` sources:
|
// Next, `to html -l` sources:
|
||||||
Some(PipelineMetadata {
|
Some(PipelineMetadata {
|
||||||
data_source: DataSource::HtmlThemes,
|
data_source: DataSource::HtmlThemes,
|
||||||
}) => {
|
}) => {
|
||||||
|
@ -5,7 +5,7 @@ pub fn get_columns<'a>(input: impl IntoIterator<Item = &'a Value>) -> Vec<String
|
|||||||
let mut columns = vec![];
|
let mut columns = vec![];
|
||||||
|
|
||||||
for item in input {
|
for item in input {
|
||||||
if let Value::Record { cols, vals: _, .. } = item {
|
if let Value::Record { cols, .. } = item {
|
||||||
for col in cols {
|
for col in cols {
|
||||||
if !columns.contains(col) {
|
if !columns.contains(col) {
|
||||||
columns.push(col.to_string());
|
columns.push(col.to_string());
|
||||||
|
@ -53,7 +53,7 @@ fn in_and_if_else() -> TestResult {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn help_works_with_missing_requirements() -> TestResult {
|
fn help_works_with_missing_requirements() -> TestResult {
|
||||||
run_test(r#"each --help | lines | length"#, "40")
|
run_test(r#"each --help | lines | length"#, "43")
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user