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
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())
}
}