Add wrap and get and cell_path parsing

This commit is contained in:
JT
2021-10-02 15:59:11 +13:00
parent 3567bbbf32
commit 5843acec02
18 changed files with 290 additions and 81 deletions

View File

@ -5,10 +5,7 @@ use nu_protocol::{
Signature,
};
use crate::{
Alias, Benchmark, BuildString, Def, Do, Each, External, For, From, FromJson, Git, GitCheckout,
Help, If, Length, Let, LetEnv, Lines, ListGitBranches, Ls, Module, Ps, Sys, Table, Use, Where,
};
use crate::*;
pub fn create_default_context() -> Rc<RefCell<EngineState>> {
let engine_state = Rc::new(RefCell::new(EngineState::new()));
@ -26,6 +23,7 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
working_set.add_decl(Box::new(For));
working_set.add_decl(Box::new(From));
working_set.add_decl(Box::new(FromJson));
working_set.add_decl(Box::new(Get));
working_set.add_decl(Box::new(Help));
working_set.add_decl(Box::new(If));
working_set.add_decl(Box::new(Length));
@ -39,6 +37,7 @@ pub fn create_default_context() -> Rc<RefCell<EngineState>> {
working_set.add_decl(Box::new(Table));
working_set.add_decl(Box::new(Use));
working_set.add_decl(Box::new(Where));
working_set.add_decl(Box::new(Wrap));
// This is a WIP proof of concept
working_set.add_decl(Box::new(ListGitBranches));

View File

@ -63,8 +63,8 @@ impl Command for Ls {
} else {
Value::Nothing { span: call_span }
},
Value::Int {
val: filesize as i64,
Value::Filesize {
val: filesize,
span: call_span,
},
],

View File

@ -0,0 +1,35 @@
use nu_engine::CallExt;
use nu_protocol::ast::{Call, CellPath};
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{Signature, SyntaxShape, Value};
pub struct Get;
impl Command for Get {
fn name(&self) -> &str {
"get"
}
fn usage(&self) -> &str {
"Extract data using a cell path."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("wrap").required(
"cell_path",
SyntaxShape::CellPath,
"the cell path to the data",
)
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let cell_path: CellPath = call.req(context, 0)?;
input.follow_cell_path(&cell_path.members)
}
}

View File

@ -1,11 +1,15 @@
mod each;
mod for_;
mod get;
mod length;
mod lines;
mod where_;
mod wrap;
pub use each::Each;
pub use for_::For;
pub use get::Get;
pub use length::Length;
pub use lines::Lines;
pub use where_::Where;
pub use wrap::Wrap;

View File

@ -0,0 +1,59 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EvaluationContext};
use nu_protocol::{IntoValueStream, Signature, SyntaxShape, Value};
pub struct Wrap;
impl Command for Wrap {
fn name(&self) -> &str {
"wrap"
}
fn usage(&self) -> &str {
"Wrap the value into a column."
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("wrap").required("name", SyntaxShape::String, "the name of the column")
}
fn run(
&self,
context: &EvaluationContext,
call: &Call,
input: Value,
) -> Result<nu_protocol::Value, nu_protocol::ShellError> {
let span = call.head;
let name: String = call.req(context, 0)?;
match input {
Value::List { vals, .. } => Ok(Value::List {
vals: vals
.into_iter()
.map(move |x| Value::Record {
cols: vec![name.clone()],
vals: vec![x],
span,
})
.collect(),
span,
}),
Value::Stream { stream, .. } => Ok(Value::Stream {
stream: stream
.map(move |x| Value::Record {
cols: vec![name.clone()],
vals: vec![x],
span,
})
.into_value_stream(),
span,
}),
_ => Ok(Value::Record {
cols: vec![name],
vals: vec![input],
span,
}),
}
}
}

View File

@ -274,10 +274,11 @@ pub fn host(sys: &mut System, span: Span) -> Option<Value> {
span,
});
}
// dict.insert_untagged(
// "uptime",
// UntaggedValue::duration(1000000000 * sys.uptime() as i64),
// );
cols.push("uptime".into());
vals.push(Value::Duration {
val: 1000000000 * sys.uptime() as u64,
span,
});
let mut users = vec![];
for user in sys.users() {

View File

@ -63,7 +63,7 @@ impl Command for Table {
output.push(vec![
StyledString {
contents: c,
style: nu_table::TextStyle::default_header(),
style: nu_table::TextStyle::default_field(),
},
StyledString {
contents: v.into_string(),