2022-01-01 02:39:58 +01:00
|
|
|
use nu_engine::column::get_columns;
|
2021-12-18 19:14:28 +01:00
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
|
|
use nu_protocol::{
|
|
|
|
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError,
|
|
|
|
Signature, Span, Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Columns;
|
|
|
|
|
|
|
|
impl Command for Columns {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"columns"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build(self.name()).category(Category::Filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Show the columns in the input."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
example: "[[name,age,grade]; [bill,20,a]] | columns",
|
|
|
|
description: "Get the columns from the table",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
example: "[[name,age,grade]; [bill,20,a]] | columns | first",
|
|
|
|
description: "Get the first column from the table",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
example: "[[name,age,grade]; [bill,20,a]] | columns | nth 1",
|
|
|
|
description: "Get the second column from the table",
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
_stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let span = call.head;
|
|
|
|
getcol(engine_state, span, input)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn getcol(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
span: Span,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
match input {
|
|
|
|
PipelineData::Value(
|
|
|
|
Value::List {
|
|
|
|
vals: input_vals,
|
2021-12-19 08:46:13 +01:00
|
|
|
span,
|
2021-12-18 19:14:28 +01:00
|
|
|
},
|
|
|
|
..,
|
|
|
|
) => {
|
2022-01-01 02:39:58 +01:00
|
|
|
let input_cols = get_columns(&input_vals);
|
2021-12-18 19:14:28 +01:00
|
|
|
Ok(input_cols
|
|
|
|
.into_iter()
|
2021-12-19 08:46:13 +01:00
|
|
|
.map(move |x| Value::String { val: x, span })
|
2021-12-18 19:14:28 +01:00
|
|
|
.into_pipeline_data(engine_state.ctrlc.clone()))
|
|
|
|
}
|
2021-12-24 08:22:11 +01:00
|
|
|
PipelineData::ListStream(stream, ..) => {
|
2021-12-18 19:14:28 +01:00
|
|
|
let v: Vec<_> = stream.into_iter().collect();
|
2022-01-01 02:39:58 +01:00
|
|
|
let input_cols = get_columns(&v);
|
2021-12-18 19:14:28 +01:00
|
|
|
|
|
|
|
Ok(input_cols
|
|
|
|
.into_iter()
|
2021-12-19 08:46:13 +01:00
|
|
|
.map(move |x| Value::String { val: x, span })
|
2021-12-18 19:14:28 +01:00
|
|
|
.into_pipeline_data(engine_state.ctrlc.clone()))
|
|
|
|
}
|
2022-02-04 03:01:45 +01:00
|
|
|
PipelineData::Value(Value::Record { cols, .. }, ..) => Ok(cols
|
|
|
|
.into_iter()
|
|
|
|
.map(move |x| Value::String { val: x, span })
|
|
|
|
.into_pipeline_data(engine_state.ctrlc.clone())),
|
2022-01-28 19:32:33 +01:00
|
|
|
PipelineData::Value(..) | PipelineData::RawStream(..) => {
|
2021-12-18 19:14:28 +01:00
|
|
|
let cols = vec![];
|
|
|
|
let vals = vec![];
|
|
|
|
Ok(Value::Record { cols, vals, span }.into_pipeline_data())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(Columns {})
|
|
|
|
}
|
|
|
|
}
|