Add rest to get, bump reedline (#760)

This commit is contained in:
JT 2022-01-16 10:40:11 -05:00 committed by GitHub
parent bfe3c50dce
commit 9b128b7a03
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 63 additions and 10 deletions

2
Cargo.lock generated
View file

@ -2852,7 +2852,7 @@ dependencies = [
[[package]] [[package]]
name = "reedline" name = "reedline"
version = "0.2.0" version = "0.2.0"
source = "git+https://github.com/nushell/reedline?branch=main#c0fbcf0ed15a02861e20001c8a38023be4b2c1e6" source = "git+https://github.com/nushell/reedline?branch=main#32338dc18aaa1633bf636717c334f22dae8e374a"
dependencies = [ dependencies = [
"chrono", "chrono",
"crossterm", "crossterm",

View file

@ -1,7 +1,10 @@
use nu_engine::CallExt; use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath}; use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Category, IntoPipelineData, PipelineData, Signature, SyntaxShape, Value}; use nu_protocol::{
Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, Signature,
SyntaxShape, Value,
};
#[derive(Clone)] #[derive(Clone)]
pub struct Get; pub struct Get;
@ -22,6 +25,7 @@ impl Command for Get {
SyntaxShape::CellPath, SyntaxShape::CellPath,
"the cell path to the data", "the cell path to the data",
) )
.rest("rest", SyntaxShape::CellPath, "additional cell paths")
.switch( .switch(
"ignore-errors", "ignore-errors",
"return nothing if path can't be found", "return nothing if path can't be found",
@ -37,20 +41,69 @@ impl Command for Get {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let span = call.head;
let cell_path: CellPath = call.req(engine_state, stack, 0)?; let cell_path: CellPath = call.req(engine_state, stack, 0)?;
let rest: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
let ignore_errors = call.has_flag("ignore-errors"); let ignore_errors = call.has_flag("ignore-errors");
let ctrlc = engine_state.ctrlc.clone();
let output = input if rest.is_empty() {
.follow_cell_path(&cell_path.members, call.head) let output = input
.map(|x| x.into_pipeline_data()); .follow_cell_path(&cell_path.members, call.head)
.map(|x| x.into_pipeline_data());
if ignore_errors { if ignore_errors {
match output { match output {
Ok(output) => Ok(output), Ok(output) => Ok(output),
Err(_) => Ok(Value::Nothing { span: call.head }.into_pipeline_data()), Err(_) => Ok(Value::Nothing { span: call.head }.into_pipeline_data()),
}
} else {
output
} }
} else { } else {
output let mut output = vec![];
let paths = vec![cell_path].into_iter().chain(rest);
let input = input.into_value(span);
for path in paths {
let val = input.clone().follow_cell_path(&path.members);
if ignore_errors {
if let Ok(val) = val {
output.push(val);
}
} else {
output.push(val?);
}
}
Ok(output.into_iter().into_pipeline_data(ctrlc))
} }
} }
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Extract the name of files as a list",
example: "ls | get name",
result: None,
},
Example {
description: "Extract the name of the 3rd entry of a file list",
example: "ls | get name.2",
result: None,
},
Example {
description: "Extract the name of the 3rd entry of a file list (alternative)",
example: "ls | get 2.name",
result: None,
},
Example {
description: "Extract the cpu list from the sys information record",
example: "sys | get cpu",
result: None,
},
]
}
} }