nushell/crates/nu-command/src/filters/select.rs

136 lines
3.9 KiB
Rust
Raw Normal View History

2021-10-02 06:55:05 +02:00
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
2021-10-25 18:58:58 +02:00
use nu_protocol::engine::{Command, EngineState, Stack};
2021-10-25 06:01:02 +02:00
use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
Signature, Span, SyntaxShape, Value,
2021-10-25 06:01:02 +02:00
};
2021-10-02 06:55:05 +02:00
2021-10-25 06:01:02 +02:00
#[derive(Clone)]
2021-10-02 06:55:05 +02:00
pub struct Select;
impl Command for Select {
fn name(&self) -> &str {
"select"
}
fn signature(&self) -> Signature {
Signature::build("select")
.rest(
"rest",
SyntaxShape::CellPath,
"the columns to select from the table",
)
.category(Category::Filters)
2021-10-02 06:55:05 +02:00
}
fn usage(&self) -> &str {
"Down-select table to only these columns."
}
fn run(
&self,
2021-10-25 08:31:39 +02:00
engine_state: &EngineState,
stack: &mut Stack,
2021-10-02 06:55:05 +02:00
call: &Call,
2021-10-25 06:01:02 +02:00
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
2021-10-25 08:31:39 +02:00
let columns: Vec<CellPath> = call.rest(engine_state, stack, 0)?;
2021-10-02 06:55:05 +02:00
let span = call.head;
2021-10-28 06:13:10 +02:00
select(engine_state, span, columns, input)
2021-10-02 06:55:05 +02:00
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Select just the name column",
example: "ls | select name",
result: None,
},
Example {
description: "Select the name and size columns",
example: "ls | select name size",
result: None,
},
]
}
}
2021-10-25 06:01:02 +02:00
fn select(
2021-10-28 06:13:10 +02:00
engine_state: &EngineState,
2021-10-25 06:01:02 +02:00
span: Span,
columns: Vec<CellPath>,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
2021-10-02 06:55:05 +02:00
if columns.is_empty() {
2021-10-25 06:01:02 +02:00
return Err(ShellError::CantFindColumn(span, span)); //FIXME?
2021-10-02 06:55:05 +02:00
}
match input {
PipelineData::Value(
Value::List {
vals: input_vals,
span,
},
..,
) => {
2021-10-02 06:55:05 +02:00
let mut output = vec![];
for input_val in input_vals {
let mut cols = vec![];
let mut vals = vec![];
for path in &columns {
//FIXME: improve implementation to not clone
let fetcher = input_val.clone().follow_cell_path(&path.members)?;
cols.push(path.into_string());
vals.push(fetcher);
}
output.push(Value::Record { cols, vals, span })
}
2021-10-28 06:13:10 +02:00
Ok(output
.into_iter()
.into_pipeline_data(engine_state.ctrlc.clone()))
2021-10-02 06:55:05 +02:00
}
PipelineData::ListStream(stream, ..) => Ok(stream
2021-10-25 06:01:02 +02:00
.map(move |x| {
let mut cols = vec![];
let mut vals = vec![];
for path in &columns {
//FIXME: improve implementation to not clone
match x.clone().follow_cell_path(&path.members) {
Ok(value) => {
cols.push(path.into_string());
vals.push(value);
}
Err(error) => {
cols.push(path.into_string());
vals.push(Value::Error { error });
2021-10-02 06:55:05 +02:00
}
}
2021-10-25 06:01:02 +02:00
}
2021-10-02 06:55:05 +02:00
2021-10-25 06:01:02 +02:00
Value::Record { cols, vals, span }
})
2021-10-28 06:13:10 +02:00
.into_pipeline_data(engine_state.ctrlc.clone())),
PipelineData::Value(v, ..) => {
2021-10-02 06:55:05 +02:00
let mut cols = vec![];
let mut vals = vec![];
for cell_path in columns {
// FIXME: remove clone
let result = v.clone().follow_cell_path(&cell_path.members)?;
cols.push(cell_path.into_string());
vals.push(result);
}
2021-10-25 06:01:02 +02:00
Ok(Value::Record { cols, vals, span }.into_pipeline_data())
2021-10-02 06:55:05 +02:00
}
_ => Ok(PipelineData::new(span)),
2021-10-02 06:55:05 +02:00
}
}