Merge branch 'main' of https://github.com/nushell/engine-q into plugins

This commit is contained in:
Fernando Herrera 2021-11-04 22:04:31 +00:00
commit ae1109139d
26 changed files with 1412 additions and 126 deletions

View file

@ -2,8 +2,10 @@ mod binary;
mod command;
mod filesize;
mod int;
mod string;
pub use self::filesize::SubCommand as IntoFilesize;
pub use binary::SubCommand as IntoBinary;
pub use command::Into;
pub use int::SubCommand as IntoInt;
pub use string::SubCommand as IntoString;

View file

@ -0,0 +1,248 @@
use nu_engine::CallExt;
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
};
// TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml)
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"into string"
}
fn signature(&self) -> Signature {
Signature::build("into string")
// FIXME - need to support column paths
// .rest(
// "rest",
// SyntaxShape::ColumnPaths(),
// "column paths to convert to string (for table input)",
// )
.named(
"decimals",
SyntaxShape::Int,
"decimal digits to which to round",
Some('d'),
)
}
fn usage(&self) -> &str {
"Convert value to string"
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
string_helper(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "convert decimal to string and round to nearest integer",
example: "1.7 | into string -d 0",
result: Some(Value::String {
val: "2".to_string(),
span: Span::unknown(),
}),
},
Example {
description: "convert decimal to string",
example: "1.7 | into string -d 1",
result: Some(Value::String {
val: "1.7".to_string(),
span: Span::unknown(),
}),
},
Example {
description: "convert decimal to string and limit to 2 decimals",
example: "1.734 | into string -d 2",
result: Some(Value::String {
val: "1.73".to_string(),
span: Span::unknown(),
}),
},
Example {
description: "try to convert decimal to string and provide negative decimal points",
example: "1.734 | into string -d -2",
result: None,
// FIXME
// result: Some(Value::Error {
// error: ShellError::UnsupportedInput(
// String::from("Cannot accept negative integers for decimals arguments"),
// Span::unknown(),
// ),
// }),
},
Example {
description: "convert decimal to string",
example: "4.3 | into string",
result: Some(Value::String {
val: "4.3".to_string(),
span: Span::unknown(),
}),
},
Example {
description: "convert string to string",
example: "'1234' | into string",
result: Some(Value::String {
val: "1234".to_string(),
span: Span::unknown(),
}),
},
Example {
description: "convert boolean to string",
example: "$true | into string",
result: Some(Value::String {
val: "true".to_string(),
span: Span::unknown(),
}),
},
Example {
description: "convert date to string",
example: "date now | into string",
result: None,
},
Example {
description: "convert filepath to string",
example: "ls Cargo.toml | get name | into string",
result: None,
},
Example {
description: "convert filesize to string",
example: "ls Cargo.toml | get size | into string",
result: None,
},
]
}
}
fn string_helper(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, ShellError> {
let decimals = call.has_flag("decimals");
let head = call.head;
let decimals_value: Option<i64> = call.get_flag(engine_state, stack, "decimals")?;
if decimals && decimals_value.is_some() && decimals_value.unwrap().is_negative() {
return Err(ShellError::UnsupportedInput(
"Cannot accept negative integers for decimals arguments".to_string(),
head,
));
}
input.map(
move |v| action(v, head, decimals, decimals_value, false),
engine_state.ctrlc.clone(),
)
}
pub fn action(
input: Value,
span: Span,
decimals: bool,
digits: Option<i64>,
group_digits: bool,
) -> Value {
match input {
Value::Int { val, .. } => {
let res = if group_digits {
format_int(val) // int.to_formatted_string(*locale)
} else {
val.to_string()
};
Value::String { val: res, span }
}
Value::Float { val, .. } => {
if decimals {
let decimal_value = digits.unwrap() as usize;
Value::String {
val: format!("{:.*}", decimal_value, val),
span,
}
} else {
Value::String {
val: val.to_string(),
span,
}
}
}
Value::Bool { val, .. } => Value::String {
val: val.to_string(),
span,
},
Value::Date { val, .. } => Value::String {
val: val.format("%c").to_string(),
span,
},
Value::String { val, .. } => Value::String { val, span },
// FIXME - we do not have a FilePath type anymore. Do we need to support this?
// Value::FilePath(a_filepath) => a_filepath.as_path().display().to_string(),
Value::Filesize { val: _, .. } => Value::String {
val: input.into_string(),
span,
},
Value::Nothing { .. } => Value::String {
val: "nothing".to_string(),
span,
},
Value::Record {
cols: _,
vals: _,
span: _,
} => Value::Error {
error: ShellError::UnsupportedInput(
"Cannot convert Record into string".to_string(),
span,
),
},
_ => Value::Error {
error: ShellError::CantConvert(
String::from(" into string. Probably this type is not supported yet"),
span,
),
},
}
}
fn format_int(int: i64) -> String {
int.to_string()
// TODO once platform-specific dependencies are stable (see Cargo.toml)
// #[cfg(windows)]
// {
// int.to_formatted_string(&Locale::en)
// }
// #[cfg(not(windows))]
// {
// match SystemLocale::default() {
// Ok(locale) => int.to_formatted_string(&locale),
// Err(_) => int.to_formatted_string(&Locale::en),
// }
// }
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View file

@ -29,10 +29,23 @@ impl Command for Echo {
_input: PipelineData,
) -> Result<PipelineData, ShellError> {
call.rest(engine_state, stack, 0).map(|to_be_echoed| {
PipelineData::Stream(ValueStream::from_stream(
to_be_echoed.into_iter(),
engine_state.ctrlc.clone(),
))
let n = to_be_echoed.len();
match n.cmp(&1usize) {
// More than one value is converted in a stream of values
std::cmp::Ordering::Greater => PipelineData::Stream(ValueStream::from_stream(
to_be_echoed.into_iter(),
engine_state.ctrlc.clone(),
)),
// But a single value can be forwarded as it is
std::cmp::Ordering::Equal => PipelineData::Value(to_be_echoed[0].clone()),
// When there are no elements, we echo the empty string
std::cmp::Ordering::Less => PipelineData::Value(Value::String {
val: "".to_string(),
span: Span::unknown(),
}),
}
})
}
@ -41,10 +54,7 @@ impl Command for Echo {
Example {
description: "Put a hello message in the pipeline",
example: "echo 'hello'",
result: Some(Value::List {
vals: vec![Value::test_string("hello")],
span: Span::new(0, 0),
}),
result: Some(Value::test_string("hello")),
},
Example {
description: "Print the value of the special '$nu' variable",

View file

@ -42,6 +42,7 @@ pub fn create_default_context() -> EngineState {
External,
First,
For,
Format,
From,
FromJson,
Get,
@ -53,6 +54,7 @@ pub fn create_default_context() -> EngineState {
IntoBinary,
IntoFilesize,
IntoInt,
IntoString,
Last,
Length,
Let,
@ -62,8 +64,12 @@ pub fn create_default_context() -> EngineState {
Math,
MathAbs,
MathAvg,
MathCeil,
MathFloor,
MathMax,
MathMedian,
MathMin,
MathMode,
MathProduct,
MathRound,
MathSqrt,
@ -88,6 +94,7 @@ pub fn create_default_context() -> EngineState {
Touch,
Use,
Where,
WithEnv,
Wrap,
Zip
);

View file

@ -1,3 +1,5 @@
mod let_env;
mod with_env;
pub use let_env::LetEnv;
pub use with_env::WithEnv;

180
crates/nu-command/src/env/with_env.rs vendored Normal file
View file

@ -0,0 +1,180 @@
use std::{
collections::HashMap,
convert::{TryFrom, TryInto},
};
use nu_engine::{eval_block, CallExt};
use nu_protocol::{
ast::Call,
engine::{Command, EngineState, Stack},
Example, PipelineData, ShellError, Signature, SyntaxShape, Value,
};
#[derive(Clone)]
pub struct WithEnv;
impl Command for WithEnv {
fn name(&self) -> &str {
"with-env"
}
fn signature(&self) -> Signature {
Signature::build("with-env")
.required(
"variable",
SyntaxShape::Any,
"the environment variable to temporarily set",
)
.required(
"block",
SyntaxShape::Block(Some(vec![SyntaxShape::Any])),
"the block to run once the variable is set",
)
}
fn usage(&self) -> &str {
"Runs a block with an environment variable set."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
with_env(engine_state, stack, call, input)
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Set the MYENV environment variable",
example: r#"with-env [MYENV "my env value"] { $nu.env.MYENV }"#,
result: Some(Value::test_string("my env value")),
},
Example {
description: "Set by primitive value list",
example: r#"with-env [X Y W Z] { $nu.env.X }"#,
result: Some(Value::test_string("Y")),
},
Example {
description: "Set by single row table",
example: r#"with-env [[X W]; [Y Z]] { $nu.env.W }"#,
result: Some(Value::test_string("Z")),
},
Example {
description: "Set by row(e.g. `open x.json` or `from json`)",
example: r#"echo '{"X":"Y","W":"Z"}'|from json|with-env $it { echo $nu.env.X $nu.env.W }"#,
result: None,
},
]
}
}
#[derive(Debug, Clone)]
pub enum EnvVar {
Proper(String),
Nothing,
}
impl TryFrom<&Value> for EnvVar {
type Error = ShellError;
fn try_from(value: &Value) -> Result<Self, Self::Error> {
if matches!(value, Value::Nothing { .. }) {
Ok(EnvVar::Nothing)
} else if let Ok(s) = value.as_string() {
if s.is_empty() {
Ok(EnvVar::Nothing)
} else {
Ok(EnvVar::Proper(s))
}
} else {
Err(ShellError::CantConvert("string".into(), value.span()?))
}
}
}
fn with_env(
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
// let external_redirection = args.call_info.args.external_redirection;
let variable: Value = call.req(engine_state, stack, 0)?;
let block_id = call.positional[1]
.as_block()
.expect("internal error: expected block");
let block = engine_state.get_block(block_id).clone();
let mut stack = stack.collect_captures(&block.captures);
let mut env: HashMap<String, EnvVar> = HashMap::new();
match &variable {
Value::List { vals: table, .. } => {
if table.len() == 1 {
// single row([[X W]; [Y Z]])
match &table[0] {
Value::Record { cols, vals, .. } => {
for (k, v) in cols.iter().zip(vals.iter()) {
env.insert(k.to_string(), v.try_into()?);
}
}
_ => {
return Err(ShellError::CantConvert(
"string list or single row".into(),
call.positional[1].span,
));
}
}
} else {
// primitive values([X Y W Z])
for row in table.chunks(2) {
if row.len() == 2 {
env.insert(row[0].as_string()?, (&row[1]).try_into()?);
}
}
}
}
// when get object by `open x.json` or `from json`
Value::Record { cols, vals, .. } => {
for (k, v) in cols.iter().zip(vals) {
env.insert(k.clone(), v.try_into()?);
}
}
_ => {
return Err(ShellError::CantConvert(
"string list or single row".into(),
call.positional[1].span,
));
}
};
for (k, v) in env {
match v {
EnvVar::Nothing => {
stack.env_vars.remove(&k);
}
EnvVar::Proper(s) => {
stack.env_vars.insert(k, s);
}
}
}
eval_block(engine_state, &mut stack, &block, input)
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(WithEnv {})
}
}

View file

@ -24,6 +24,8 @@ pub fn test_examples(cmd: impl Command + 'static) {
working_set.add_decl(Box::new(Math));
working_set.add_decl(Box::new(Date));
use super::Echo;
working_set.add_decl(Box::new(Echo));
// Adding the command that is being tested to the working set
working_set.add_decl(Box::new(cmd));

View file

@ -2,7 +2,7 @@ use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value,
Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Type, Value,
};
#[derive(Clone)]
@ -62,26 +62,85 @@ fn first_helper(
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
let rows: Option<i64> = call.opt(engine_state, stack, 0)?;
let rows_desired: usize = match rows {
let mut rows_desired: usize = match rows {
Some(x) => x as usize,
None => 1,
};
if rows_desired == 1 {
let mut input_peek = input.into_iter().peekable();
match input_peek.next() {
Some(val) => Ok(val.into_pipeline_data()),
None => Err(ShellError::AccessBeyondEndOfStream(head)),
let mut input_peek = input.into_iter().peekable();
if input_peek.peek().is_some() {
match input_peek.peek().unwrap().get_type() {
Type::Binary => {
match &mut input_peek.next() {
Some(v) => match &v {
Value::Binary { val, .. } => {
let bytes = val;
if bytes.len() >= rows_desired {
// We only want to see a certain amount of the binary
// so let's grab those parts
let output_bytes = bytes[0..rows_desired].to_vec();
Ok(Value::Binary {
val: output_bytes,
span: head,
}
.into_pipeline_data())
} else {
// if we want more rows that the current chunk size (8192)
// we must gradually get bigger chunks while testing
// if it's within the requested rows_desired size
let mut bigger: Vec<u8> = vec![];
bigger.extend(bytes);
while bigger.len() < rows_desired {
match input_peek.next() {
Some(Value::Binary { val, .. }) => bigger.extend(val),
_ => {
// We're at the end of our data so let's break out of this loop
// and set the rows_desired to the size of our data
rows_desired = bigger.len();
break;
}
}
}
let output_bytes = bigger[0..rows_desired].to_vec();
Ok(Value::Binary {
val: output_bytes,
span: head,
}
.into_pipeline_data())
}
}
_ => todo!(),
},
None => Ok(Value::List {
vals: input_peek.into_iter().take(rows_desired).collect(),
span: head,
}
.into_pipeline_data()),
}
}
_ => {
if rows_desired == 1 {
match input_peek.next() {
Some(val) => Ok(val.into_pipeline_data()),
None => Err(ShellError::AccessBeyondEndOfStream(head)),
}
} else {
Ok(Value::List {
vals: input_peek.into_iter().take(rows_desired).collect(),
span: head,
}
.into_pipeline_data())
}
}
}
} else {
Ok(Value::List {
vals: input.into_iter().take(rows_desired).collect(),
span: head,
}
.into_pipeline_data())
Err(ShellError::UnsupportedInput(
String::from("Cannot perform into string on empty input"),
head,
))
}
}
#[cfg(test)]
mod test {
use super::*;

View file

@ -0,0 +1,73 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"math ceil"
}
fn signature(&self) -> Signature {
Signature::build("math ceil")
}
fn usage(&self) -> &str {
"Applies the ceil function to a list of numbers"
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
input.map(
move |value| operate(value, head),
engine_state.ctrlc.clone(),
)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Apply the ceil function to a list of numbers",
example: "[1.5 2.3 -3.1] | math ceil",
result: Some(Value::List {
vals: vec![Value::test_int(2), Value::test_int(3), Value::test_int(-3)],
span: Span::unknown(),
}),
}]
}
}
fn operate(value: Value, head: Span) -> Value {
match value {
Value::Int { .. } => value,
Value::Float { val, span } => Value::Float {
val: val.ceil(),
span,
},
other => Value::Error {
error: ShellError::UnsupportedInput(
String::from("Only numerical values are supported"),
other.span().unwrap_or(head),
),
},
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View file

@ -0,0 +1,73 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"math floor"
}
fn signature(&self) -> Signature {
Signature::build("math floor")
}
fn usage(&self) -> &str {
"Applies the floor function to a list of numbers"
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let head = call.head;
input.map(
move |value| operate(value, head),
engine_state.ctrlc.clone(),
)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Apply the floor function to a list of numbers",
example: "[1.5 2.3 -3.1] | math floor",
result: Some(Value::List {
vals: vec![Value::test_int(1), Value::test_int(2), Value::test_int(-4)],
span: Span::unknown(),
}),
}]
}
}
fn operate(value: Value, head: Span) -> Value {
match value {
Value::Int { .. } => value,
Value::Float { val, span } => Value::Float {
val: val.floor(),
span,
},
other => Value::Error {
error: ShellError::UnsupportedInput(
String::from("Only numerical values are supported"),
other.span().unwrap_or(head),
),
},
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View file

@ -0,0 +1,121 @@
use crate::math::avg::average;
use crate::math::utils::run_with_function;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value};
#[derive(Clone)]
pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"math median"
}
fn signature(&self) -> Signature {
Signature::build("math median")
}
fn usage(&self) -> &str {
"Gets the median of a list of numbers"
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
run_with_function(call, input, median)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Get the median of a list of numbers",
example: "[3 8 9 12 12 15] | math median",
result: Some(Value::Float {
val: 10.5,
span: Span::unknown(),
}),
}]
}
}
enum Pick {
MedianAverage,
Median,
}
pub fn median(values: &[Value], head: &Span) -> Result<Value, ShellError> {
let take = if values.len() % 2 == 0 {
Pick::MedianAverage
} else {
Pick::Median
};
let mut sorted = vec![];
for item in values {
sorted.push(item.clone());
}
if let Some(Err(values)) = values
.windows(2)
.map(|elem| {
if elem[0].partial_cmp(&elem[1]).is_none() {
return Err(ShellError::OperatorMismatch {
op_span: *head,
lhs_ty: elem[0].get_type(),
lhs_span: elem[0].span()?,
rhs_ty: elem[1].get_type(),
rhs_span: elem[1].span()?,
});
}
Ok(elem[0].partial_cmp(&elem[1]).unwrap())
})
.find(|elem| elem.is_err())
{
return Err(values);
}
sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
match take {
Pick::Median => {
let idx = (values.len() as f64 / 2.0).floor() as usize;
let out = sorted
.get(idx)
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), *head))?;
Ok(out.clone())
}
Pick::MedianAverage => {
let idx_end = (values.len() / 2) as usize;
let idx_start = idx_end - 1;
let left = sorted
.get(idx_start)
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), *head))?
.clone();
let right = sorted
.get(idx_end)
.ok_or_else(|| ShellError::UnsupportedInput("Empty input".to_string(), *head))?
.clone();
average(&[left, right], head)
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View file

@ -1,8 +1,12 @@
mod abs;
mod avg;
mod ceil;
pub mod command;
mod floor;
mod max;
mod median;
mod min;
mod mode;
mod product;
mod reducers;
mod round;
@ -12,9 +16,13 @@ mod utils;
pub use abs::SubCommand as MathAbs;
pub use avg::SubCommand as MathAvg;
pub use ceil::SubCommand as MathCeil;
pub use command::MathCommand as Math;
pub use floor::SubCommand as MathFloor;
pub use max::SubCommand as MathMax;
pub use median::SubCommand as MathMedian;
pub use min::SubCommand as MathMin;
pub use mode::SubCommand as MathMode;
pub use product::SubCommand as MathProduct;
pub use round::SubCommand as MathRound;
pub use sqrt::SubCommand as MathSqrt;

View file

@ -0,0 +1,174 @@
use crate::math::utils::run_with_function;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, Value};
use std::cmp::Ordering;
#[derive(Clone)]
pub struct SubCommand;
#[derive(Hash, Eq, PartialEq, Debug)]
enum NumberTypes {
Float,
Int,
Duration,
Filesize,
}
#[derive(Hash, Eq, PartialEq, Debug)]
struct HashableType {
bytes: [u8; 8],
original_type: NumberTypes,
}
impl HashableType {
fn new(bytes: [u8; 8], original_type: NumberTypes) -> HashableType {
HashableType {
bytes,
original_type,
}
}
}
impl Command for SubCommand {
fn name(&self) -> &str {
"math mode"
}
fn signature(&self) -> Signature {
Signature::build("math mode")
}
fn usage(&self) -> &str {
"Gets the most frequent element(s) from a list of numbers or tables"
}
fn run(
&self,
_engine_state: &EngineState,
_stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
run_with_function(call, input, mode)
}
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Get the mode(s) of a list of numbers",
example: "[3 3 9 12 12 15] | math mode",
result: Some(Value::List {
vals: vec![Value::test_int(3), Value::test_int(12)],
span: Span::unknown(),
}),
}]
}
}
pub fn mode(values: &[Value], head: &Span) -> Result<Value, ShellError> {
if let Some(Err(values)) = values
.windows(2)
.map(|elem| {
if elem[0].partial_cmp(&elem[1]).is_none() {
return Err(ShellError::OperatorMismatch {
op_span: *head,
lhs_ty: elem[0].get_type(),
lhs_span: elem[0].span()?,
rhs_ty: elem[1].get_type(),
rhs_span: elem[1].span()?,
});
}
Ok(elem[0].partial_cmp(&elem[1]).unwrap())
})
.find(|elem| elem.is_err())
{
return Err(values);
}
//In e-q, Value doesn't implement Hash or Eq, so we have to get the values inside
// But f64 doesn't implement Hash, so we get the binary representation to use as
// key in the HashMap
let hashable_values: Result<Vec<HashableType>, ShellError> = values
.iter()
.map(|val| match val {
Value::Int { val, .. } => Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Int)),
Value::Duration { val, .. } => {
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Duration))
}
Value::Float { val, .. } => {
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Float))
}
Value::Filesize { val, .. } => {
Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Filesize))
}
other => Err(ShellError::UnsupportedInput(
"Unable to give a result with this input".to_string(),
other.span().unwrap(),
)),
})
.collect::<Result<Vec<HashableType>, ShellError>>();
if let Err(not_hashable) = hashable_values {
return Err(not_hashable);
}
let mut frequency_map = std::collections::HashMap::new();
for v in hashable_values.unwrap() {
let counter = frequency_map.entry(v).or_insert(0);
*counter += 1;
}
let mut max_freq = -1;
let mut modes = Vec::<Value>::new();
for (value, frequency) in &frequency_map {
match max_freq.cmp(frequency) {
Ordering::Less => {
max_freq = *frequency;
modes.clear();
modes.push(recreate_value(value, *head));
}
Ordering::Equal => {
modes.push(recreate_value(value, *head));
}
Ordering::Greater => (),
}
}
modes.sort_by(|a, b| a.partial_cmp(b).unwrap());
Ok(Value::List {
vals: modes,
span: *head,
})
}
fn recreate_value(hashable_value: &HashableType, head: Span) -> Value {
let bytes = hashable_value.bytes;
match &hashable_value.original_type {
NumberTypes::Int => Value::Int {
val: i64::from_ne_bytes(bytes),
span: head,
},
NumberTypes::Float => Value::Float {
val: f64::from_ne_bytes(bytes),
span: head,
},
NumberTypes::Duration => Value::Duration {
val: i64::from_ne_bytes(bytes),
span: head,
},
NumberTypes::Filesize => Value::Filesize {
val: i64::from_ne_bytes(bytes),
span: head,
},
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_examples() {
use crate::test_examples;
test_examples(SubCommand {})
}
}

View file

@ -0,0 +1,200 @@
use nu_engine::CallExt;
use nu_protocol::ast::{Call, PathMember};
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{
Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value, ValueStream,
};
#[derive(Clone)]
pub struct Format;
impl Command for Format {
fn name(&self) -> &str {
"format"
}
fn signature(&self) -> Signature {
Signature::build("format").required(
"pattern",
SyntaxShape::String,
"the pattern to output. e.g.) \"{foo}: {bar}\"",
)
}
fn usage(&self) -> &str {
"Format columns into a string using a simple pattern."
}
fn run(
&self,
engine_state: &EngineState,
stack: &mut Stack,
call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let specified_pattern: Result<Value, ShellError> = call.req(engine_state, stack, 0);
match specified_pattern {
Err(e) => Err(e),
Ok(pattern) => {
let string_pattern = pattern.as_string().unwrap();
let ops = extract_formatting_operations(string_pattern);
format(input, &ops)
}
}
}
fn examples(&self) -> Vec<Example> {
vec![
Example {
description: "Print filenames with their sizes",
example: "ls | format '{name}: {size}'",
result: None,
},
Example {
description: "Print elements from some columns of a table",
example: "echo [[col1, col2]; [v1, v2] [v3, v4]] | format '{col2}'",
result: Some(Value::List {
vals: vec![Value::test_string("v2"), Value::test_string("v4")],
span: Span::new(0, 0),
}),
},
]
}
}
#[derive(Debug)]
enum FormatOperation {
FixedText(String),
ValueFromColumn(String),
}
/// Given a pattern that is fed into the Format command, we can process it and subdivide it
/// in two kind of operations.
/// FormatOperation::FixedText contains a portion of the patter that has to be placed
/// there without any further processing.
/// FormatOperation::ValueFromColumn contains the name of a column whose values will be
/// formatted according to the input pattern.
fn extract_formatting_operations(input: String) -> Vec<FormatOperation> {
let mut output = vec![];
let mut characters = input.chars();
'outer: loop {
let mut before_bracket = String::new();
for ch in &mut characters {
if ch == '{' {
break;
}
before_bracket.push(ch);
}
if !before_bracket.is_empty() {
output.push(FormatOperation::FixedText(before_bracket.to_string()));
}
let mut column_name = String::new();
for ch in &mut characters {
if ch == '}' {
break;
}
column_name.push(ch);
}
if !column_name.is_empty() {
output.push(FormatOperation::ValueFromColumn(column_name.clone()));
}
if before_bracket.is_empty() && column_name.is_empty() {
break 'outer;
}
}
output
}
/// Format the incoming PipelineData according to the pattern
fn format(
input_data: PipelineData,
format_operations: &[FormatOperation],
) -> Result<PipelineData, ShellError> {
let data_as_value = input_data.into_value();
// We can only handle a Record or a List of Record's
match data_as_value {
Value::Record { .. } => match format_record(format_operations, &data_as_value) {
Ok(value) => Ok(PipelineData::Value(Value::string(value, Span::unknown()))),
Err(value) => Err(value),
},
Value::List { vals, .. } => {
let mut list = vec![];
for val in vals.iter() {
match val {
Value::Record { .. } => match format_record(format_operations, val) {
Ok(value) => {
list.push(Value::string(value, Span::unknown()));
}
Err(value) => {
return Err(value);
}
},
_ => {
return Err(ShellError::UnsupportedInput(
"Input data is not supported by this command.".to_string(),
Span::unknown(),
))
}
}
}
Ok(PipelineData::Stream(ValueStream::from_stream(
list.into_iter(),
None,
)))
}
_ => Err(ShellError::UnsupportedInput(
"Input data is not supported by this command.".to_string(),
Span::unknown(),
)),
}
}
fn format_record(
format_operations: &[FormatOperation],
data_as_value: &Value,
) -> Result<String, ShellError> {
let mut output = String::new();
for op in format_operations {
match op {
FormatOperation::FixedText(s) => output.push_str(s.as_str()),
// The referenced code suggest to use the correct Span's
// See: https://github.com/nushell/nushell/blob/c4af5df828135159633d4bc3070ce800518a42a2/crates/nu-command/src/commands/strings/format/command.rs#L61
FormatOperation::ValueFromColumn(col_name) => {
match data_as_value
.clone()
.follow_cell_path(&[PathMember::String {
val: col_name.clone(),
span: Span::unknown(),
}]) {
Ok(value_at_column) => {
output.push_str(value_at_column.as_string().unwrap().as_str())
}
Err(se) => return Err(se),
}
}
}
}
Ok(output)
}
#[cfg(test)]
mod test {
#[test]
fn test_examples() {
use super::Format;
use crate::test_examples;
test_examples(Format {})
}
}

View file

@ -0,0 +1,3 @@
pub mod command;
pub use command::Format;

View file

@ -1,7 +1,9 @@
mod build_string;
mod format;
mod size;
mod split;
pub use build_string::BuildString;
pub use format::*;
pub use size::Size;
pub use split::*;

View file

@ -3,7 +3,7 @@ use std::time::Instant;
use nu_engine::eval_block;
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{PipelineData, Signature, SyntaxShape};
use nu_protocol::{IntoPipelineData, PipelineData, Signature, SyntaxShape, Value};
#[derive(Clone)]
pub struct Benchmark;
@ -42,7 +42,12 @@ impl Command for Benchmark {
eval_block(engine_state, &mut stack, block, PipelineData::new())?.into_value();
let end_time = Instant::now();
println!("{} ms", (end_time - start_time).as_millis());
Ok(PipelineData::new())
let output = Value::Duration {
val: (end_time - start_time).as_nanos() as i64,
span: call.head,
};
Ok(output.into_pipeline_data())
}
}

View file

@ -629,27 +629,6 @@ pub fn parse_call(
}
let mut pos = 0;
let mut shorthand = vec![];
while pos < spans.len() {
// Check if there is any environment shorthand
let name = working_set.get_span_contents(spans[pos]);
let split: Vec<_> = name.splitn(2, |x| *x == b'=').collect();
if split.len() == 2 {
shorthand.push(split);
pos += 1;
} else {
break;
}
}
if pos == spans.len() {
return (
garbage(span(spans)),
Some(ParseError::UnknownCommand(spans[0])),
);
}
let cmd_start = pos;
let mut name_spans = vec![];
@ -1683,6 +1662,54 @@ pub fn parse_string(
}
}
pub fn parse_string_strict(
working_set: &mut StateWorkingSet,
span: Span,
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let (bytes, quoted) = if (bytes.starts_with(b"\"") && bytes.ends_with(b"\"") && bytes.len() > 1)
|| (bytes.starts_with(b"\'") && bytes.ends_with(b"\'") && bytes.len() > 1)
{
(&bytes[1..(bytes.len() - 1)], true)
} else {
(bytes, false)
};
if let Ok(token) = String::from_utf8(bytes.into()) {
if quoted {
(
Expression {
expr: Expr::String(token),
span,
ty: Type::String,
custom_completion: None,
},
None,
)
} else if token.contains(' ') {
(
garbage(span),
Some(ParseError::Expected("string".into(), span)),
)
} else {
(
Expression {
expr: Expr::String(token),
span,
ty: Type::String,
custom_completion: None,
},
None,
)
}
} else {
(
garbage(span),
Some(ParseError::Expected("string".into(), span)),
)
}
}
//TODO: Handle error case for unknown shapes
pub fn parse_shape_name(
_working_set: &StateWorkingSet,
@ -2965,12 +2992,126 @@ pub fn parse_expression(
spans: &[Span],
expand_aliases: bool,
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(spans[0]);
let mut pos = 0;
let mut shorthand = vec![];
if is_math_expression_byte(bytes[0]) {
parse_math_expression(working_set, spans, None)
while pos < spans.len() {
// Check if there is any environment shorthand
let name = working_set.get_span_contents(spans[pos]);
let split = name.split(|x| *x == b'=');
let split: Vec<_> = split.collect();
if split.len() == 2 && !split[0].is_empty() {
let point = split[0].len() + 1;
let lhs = parse_string_strict(
working_set,
Span {
start: spans[pos].start,
end: spans[pos].start + point - 1,
},
);
let rhs = if spans[pos].start + point < spans[pos].end {
parse_string_strict(
working_set,
Span {
start: spans[pos].start + point,
end: spans[pos].end,
},
)
} else {
(
Expression {
expr: Expr::String(String::new()),
span: spans[pos],
ty: Type::Nothing,
custom_completion: None,
},
None,
)
};
if lhs.1.is_none() && rhs.1.is_none() {
shorthand.push((lhs.0, rhs.0));
pos += 1;
} else {
break;
}
} else {
break;
}
}
if pos == spans.len() {
return (
garbage(span(spans)),
Some(ParseError::UnknownCommand(spans[0])),
);
}
let bytes = working_set.get_span_contents(spans[pos]);
let (output, err) = if is_math_expression_byte(bytes[0]) {
parse_math_expression(working_set, &spans[pos..], None)
} else {
parse_call(working_set, spans, expand_aliases)
parse_call(working_set, &spans[pos..], expand_aliases)
};
let with_env = working_set.find_decl(b"with-env");
if !shorthand.is_empty() {
if let Some(decl_id) = with_env {
let mut block = Block::default();
let ty = output.ty.clone();
block.stmts = vec![Statement::Pipeline(Pipeline {
expressions: vec![output],
})];
let mut seen = vec![];
let captures = find_captures_in_block(working_set, &block, &mut seen);
block.captures = captures;
let block_id = working_set.add_block(block);
let mut env_vars = vec![];
for sh in shorthand {
env_vars.push(sh.0);
env_vars.push(sh.1);
}
let positional = vec![
Expression {
expr: Expr::List(env_vars),
span: span(&spans[..pos]),
ty: Type::Unknown,
custom_completion: None,
},
Expression {
expr: Expr::Block(block_id),
span: span(&spans[pos..]),
ty,
custom_completion: None,
},
];
(
Expression {
expr: Expr::Call(Box::new(Call {
head: span(spans),
decl_id,
named: vec![],
positional,
})),
custom_completion: None,
span: span(spans),
ty: Type::Unknown,
},
err,
)
} else {
(output, err)
}
} else {
(output, err)
}
}

View file

@ -64,6 +64,9 @@ impl Stack {
}
}
// FIXME: this is probably slow
output.env_vars = self.env_vars.clone();
output
}

View file

@ -14,9 +14,20 @@ use crate::{ast::PathMember, ShellError, Span, Value, ValueStream};
/// Namely, how do you know the difference between a single string and a list of one string. How do you know
/// when to flatten the data given to you from a data source into the stream or to keep it as an unflattened
/// list?
///
/// * We tried putting the stream into Value. This had some interesting properties as now commands "just worked
/// on values", but the inability to pass Value to threads as-is meant a lot of workarounds for dealing with
/// Value's stream case
/// on values", but lead to a few unfortunate issues.
///
/// The first is that you can't easily clone Values in a way that felt largely immutable. For example, if
/// you cloned a Value which contained a stream, and in one variable drained some part of it, then the second
/// variable would see different values based on what you did to the first.
///
/// To make this kind of mutation thread-safe, we would have had to produce a lock for the stream, which in
/// practice would have meant always locking the stream before reading from it. But more fundamentally, it
/// felt wrong in practice that observation of a value at runtime could affect other values which happen to
/// alias the same stream. By separating these, we don't have this effect. Instead, variables could get
/// concrete list values rather than streams, and be able to view them without non-local effects.
///
/// * A balance of the two approaches is what we've landed on: Values are thread-safe to pass, and we can stream
/// them into any sources. Streams are still available to model the infinite streams approach of original
/// Nushell.

View file

@ -4,6 +4,9 @@ use thiserror::Error;
use crate::{ast::Operator, Span, Type};
/// The fundamental error type for the evaluation engine. These cases represent different kinds of errors
/// the evaluator might face, along with helpful spans to label. An error renderer will take this error value
/// and pass it into an error viewer to display to the user.
#[derive(Debug, Clone, Error, Diagnostic, Serialize, Deserialize)]
pub enum ShellError {
#[error("Type mismatch during operation.")]

View file

@ -1,6 +1,7 @@
use miette::SourceSpan;
use serde::{Deserialize, Serialize};
/// A spanned area of interest, generic over what kind of thing is of interest
#[derive(Clone, Debug)]
pub struct Spanned<T>
where
@ -10,6 +11,9 @@ where
pub span: Span,
}
/// Spans are a global offset across all seen files, which are cached in the engine's state. The start and
/// end offset together make the inclusive start/exclusive end pair for where to underline to highlight
/// a given point of interest.
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct Span {
pub start: usize,

View file

@ -199,8 +199,8 @@ impl Value {
Value::Bool { val, .. } => val.to_string(),
Value::Int { val, .. } => val.to_string(),
Value::Float { val, .. } => val.to_string(),
Value::Filesize { val, .. } => format!("{} bytes", val),
Value::Duration { val, .. } => format!("{} ns", val),
Value::Filesize { val, .. } => format_filesize(val),
Value::Duration { val, .. } => format_duration(val),
Value::Date { val, .. } => format!("{:?}", val),
Value::Range { val, .. } => {
format!("{}..{}", val.from.into_string(), val.to.into_string())

View file

@ -130,18 +130,6 @@ impl Range {
}
}
// impl IntoIterator for Range {
// type Item = Value;
// type IntoIter = RangeIterator;
// fn into_iter(self) -> Self::IntoIter {
// let span = self.from.span();
// RangeIterator::new(self, span)
// }
// }
pub struct RangeIterator {
curr: Value,
end: Value,

View file

@ -7,6 +7,12 @@ use std::{
},
};
/// A potentially infinite stream of values, optinally with a mean to send a Ctrl-C signal to stop
/// the stream from continuing.
///
/// In practice, a "stream" here means anything which can be iterated and produce Values as it iterates.
/// Like other iterators in Rust, observing values from this stream will drain the items as you view them
/// and the stream cannot be replayed.
pub struct ValueStream {
pub stream: Box<dyn Iterator<Item = Value> + Send + 'static>,
pub ctrlc: Option<Arc<AtomicBool>>,
@ -60,62 +66,3 @@ impl Iterator for ValueStream {
}
}
}
// impl Serialize for ValueStream {
// fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
// where
// S: serde::Serializer,
// {
// let mut seq = serializer.serialize_seq(None)?;
// for element in self.0.borrow_mut().into_iter() {
// seq.serialize_element(&element)?;
// }
// seq.end()
// }
// }
// impl<'de> Deserialize<'de> for ValueStream {
// fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
// where
// D: serde::Deserializer<'de>,
// {
// deserializer.deserialize_seq(MySeqVisitor)
// }
// }
// struct MySeqVisitor;
// impl<'a> serde::de::Visitor<'a> for MySeqVisitor {
// type Value = ValueStream;
// fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
// formatter.write_str("a value stream")
// }
// fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
// where
// A: serde::de::SeqAccess<'a>,
// {
// let mut output: Vec<Value> = vec![];
// while let Some(value) = seq.next_element()? {
// output.push(value);
// }
// Ok(ValueStream(Rc::new(RefCell::new(output.into_iter()))))
// }
// }
// pub trait IntoValueStream {
// fn into_value_stream(self) -> ValueStream;
// }
// impl<T> IntoValueStream for T
// where
// T: Iterator<Item = Value> + 'static,
// {
// fn into_value_stream(self) -> ValueStream {
// ValueStream::from_stream(self)
// }
// }

View file

@ -804,10 +804,30 @@ fn help_works_with_missing_requirements() -> TestResult {
#[test]
fn scope_variable() -> TestResult {
run_test(r"let x = 3; $scope.vars.0", "$x")
run_test(r#"let x = 3; $scope.vars.0"#, "$x")
}
#[test]
fn zip_ranges() -> TestResult {
run_test(r"1..3 | zip 4..6 | get 2.1", "6")
run_test(r#"1..3 | zip 4..6 | get 2.1"#, "6")
}
#[test]
fn shorthand_env_1() -> TestResult {
run_test(r#"FOO=BAZ $nu.env.FOO"#, "BAZ")
}
#[test]
fn shorthand_env_2() -> TestResult {
run_test(r#"FOO=BAZ FOO=MOO $nu.env.FOO"#, "MOO")
}
#[test]
fn shorthand_env_3() -> TestResult {
run_test(r#"FOO=BAZ BAR=MOO $nu.env.FOO"#, "BAZ")
}
#[test]
fn shorthand_env_4() -> TestResult {
fail_test(r#"FOO=BAZ FOO= $nu.env.FOO"#, "cannot find column")
}