mirror of
https://github.com/nushell/nushell.git
synced 2025-01-03 04:50:28 +01:00
Merge pull request #41 from jonathandturner/select_cmd
Add select command which opens cell
This commit is contained in:
commit
856ca44679
@ -54,6 +54,7 @@ pub async fn cli() -> Result<(), Box<Error>> {
|
|||||||
command("column", column::column),
|
command("column", column::column),
|
||||||
command("split", split::split),
|
command("split", split::split),
|
||||||
command("reject", reject::reject),
|
command("reject", reject::reject),
|
||||||
|
command("select", select::select),
|
||||||
command("to-array", to_array::to_array),
|
command("to-array", to_array::to_array),
|
||||||
command("to-json", to_json::to_json),
|
command("to-json", to_json::to_json),
|
||||||
Arc::new(Where),
|
Arc::new(Where),
|
||||||
|
@ -8,6 +8,7 @@ crate mod ls;
|
|||||||
crate mod open;
|
crate mod open;
|
||||||
crate mod ps;
|
crate mod ps;
|
||||||
crate mod reject;
|
crate mod reject;
|
||||||
|
crate mod select;
|
||||||
crate mod size;
|
crate mod size;
|
||||||
crate mod skip;
|
crate mod skip;
|
||||||
crate mod sort_by;
|
crate mod sort_by;
|
||||||
|
36
src/commands/select.rs
Normal file
36
src/commands/select.rs
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
use crate::errors::ShellError;
|
||||||
|
use crate::object::Value;
|
||||||
|
use crate::object::base::select_fields;
|
||||||
|
use crate::prelude::*;
|
||||||
|
|
||||||
|
pub fn select(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||||
|
if args.args.is_empty() {
|
||||||
|
return Err(ShellError::string("select requires a field"));
|
||||||
|
}
|
||||||
|
|
||||||
|
let fields: Result<Vec<String>, _> = args.args.iter().map(|a| a.as_string()).collect();
|
||||||
|
let fields = fields?;
|
||||||
|
|
||||||
|
let stream = args
|
||||||
|
.input
|
||||||
|
.map(move |item| {
|
||||||
|
let mut result = VecDeque::new();
|
||||||
|
let column = select_fields(&item, &fields);
|
||||||
|
for field in &fields {
|
||||||
|
match column.get_data_by_key(&field) {
|
||||||
|
Some(Value::List(l)) => {
|
||||||
|
for item in l {
|
||||||
|
result.push_back(ReturnValue::Value(item.copy()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some(x) => result.push_back(ReturnValue::Value(x.copy())),
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
})
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
Ok(stream.boxed())
|
||||||
|
}
|
@ -17,7 +17,6 @@ impl Scope {
|
|||||||
|
|
||||||
crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, ShellError> {
|
crate fn evaluate_expr(expr: &ast::Expression, scope: &Scope) -> Result<Value, ShellError> {
|
||||||
use ast::*;
|
use ast::*;
|
||||||
|
|
||||||
match expr {
|
match expr {
|
||||||
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
||||||
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
||||||
|
Loading…
Reference in New Issue
Block a user