mirror of
https://github.com/nushell/nushell.git
synced 2024-12-22 07:02:19 +01:00
Add select command which opens cell
This commit is contained in:
parent
2afa785e0e
commit
8e00cd43a8
@ -54,6 +54,7 @@ pub async fn cli() -> Result<(), Box<Error>> {
|
||||
command("column", column::column),
|
||||
command("split", split::split),
|
||||
command("reject", reject::reject),
|
||||
command("select", select::select),
|
||||
command("to-array", to_array::to_array),
|
||||
command("to-json", to_json::to_json),
|
||||
Arc::new(Where),
|
||||
|
@ -8,6 +8,7 @@ crate mod ls;
|
||||
crate mod open;
|
||||
crate mod ps;
|
||||
crate mod reject;
|
||||
crate mod select;
|
||||
crate mod size;
|
||||
crate mod skip;
|
||||
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> {
|
||||
use ast::*;
|
||||
|
||||
match expr {
|
||||
Expression::Leaf(l) => Ok(evaluate_leaf(l)),
|
||||
Expression::Parenthesized(p) => evaluate_expr(&p.expr, scope),
|
||||
|
Loading…
Reference in New Issue
Block a user