From 52cb50b93722e98d23d3be5d6b412e3167afceac Mon Sep 17 00:00:00 2001 From: Antonio Natilla Date: Tue, 2 Nov 2021 18:13:06 +0100 Subject: [PATCH 01/17] Base Command implementation for Format Note that run is not implemented yet --- crates/nu-command/src/default_context.rs | 1 + .../nu-command/src/strings/format/format.rs | 53 +++++++++++++++++++ crates/nu-command/src/strings/format/mod.rs | 3 ++ crates/nu-command/src/strings/mod.rs | 2 + 4 files changed, 59 insertions(+) create mode 100644 crates/nu-command/src/strings/format/format.rs create mode 100644 crates/nu-command/src/strings/format/mod.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 3d41f27355..754693c2ca 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -42,6 +42,7 @@ pub fn create_default_context() -> EngineState { External, First, For, + Format, From, FromJson, Get, diff --git a/crates/nu-command/src/strings/format/format.rs b/crates/nu-command/src/strings/format/format.rs new file mode 100644 index 0000000000..5f64661a3a --- /dev/null +++ b/crates/nu-command/src/strings/format/format.rs @@ -0,0 +1,53 @@ +//use nu_engine::CallExt; +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape}; + +#[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 { + todo!() + } + + fn examples(&self) -> Vec { + vec![Example { + description: "Print filenames with their sizes", + example: "ls | format '{name}: {size}'", + result: None, + }] + } +} + +#[cfg(test)] +mod test { + #[test] + fn test_examples() { + use super::Format; + use crate::test_examples; + test_examples(Format {}) + } +} diff --git a/crates/nu-command/src/strings/format/mod.rs b/crates/nu-command/src/strings/format/mod.rs new file mode 100644 index 0000000000..c20d416331 --- /dev/null +++ b/crates/nu-command/src/strings/format/mod.rs @@ -0,0 +1,3 @@ +pub mod format; + +pub use format::Format; diff --git a/crates/nu-command/src/strings/mod.rs b/crates/nu-command/src/strings/mod.rs index bbb78a24d7..a23eba5ff0 100644 --- a/crates/nu-command/src/strings/mod.rs +++ b/crates/nu-command/src/strings/mod.rs @@ -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::*; From 070067b75e333c701fc57afdce598bb382f05909 Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Tue, 2 Nov 2021 20:39:16 +0100 Subject: [PATCH 02/17] Add into string command --- crates/nu-command/Cargo.toml | 4 + crates/nu-command/src/conversions/into/mod.rs | 2 + .../nu-command/src/conversions/into/string.rs | 377 ++++++++++++++++++ crates/nu-command/src/default_context.rs | 1 + 4 files changed, 384 insertions(+) create mode 100644 crates/nu-command/src/conversions/into/string.rs diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index 99d13d33c3..b822e6084e 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -22,6 +22,10 @@ unicode-segmentation = "1.8.0" glob = "0.3.0" thiserror = "1.0.29" sysinfo = "0.20.4" +bigdecimal = { package = "bigdecimal-rs", version = "0.2.1", features = ["serde"] } +num-bigint = { version="0.3.1", features=["serde"] } +num-format = { version="0.4.0", features=["with-num-bigint"] } +num-traits = "0.2.14" chrono = { version = "0.4.19", features = ["serde"] } chrono-humanize = "0.2.1" chrono-tz = "0.6.0" diff --git a/crates/nu-command/src/conversions/into/mod.rs b/crates/nu-command/src/conversions/into/mod.rs index 7563ca614a..d9fb8cb253 100644 --- a/crates/nu-command/src/conversions/into/mod.rs +++ b/crates/nu-command/src/conversions/into/mod.rs @@ -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; diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs new file mode 100644 index 0000000000..a74b630e2a --- /dev/null +++ b/crates/nu-command/src/conversions/into/string.rs @@ -0,0 +1,377 @@ +use nu_protocol::Value::Filesize; +use nu_protocol::{ + ast::Call, + engine::{Command, EngineState, Stack}, + Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value, +}; + +use bigdecimal::{BigDecimal, FromPrimitive}; + +use nu_engine::CallExt; + +use num_bigint::{BigInt, BigUint, ToBigInt}; +use num_format::Locale; +use num_traits::{Pow, Signed}; +use std::iter; +// 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 { + string_helper(engine_state, stack, call, input) + } + + fn examples(&self) -> Vec { + 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 { + let decimals = call.has_flag("decimals"); + let head = call.head; + let decimals_value: Option = 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, + head: Span, + decimals: bool, + digits: Option, + group_digits: bool, +) -> Value { + match input { + Value::Int { val, span } => { + let res = if group_digits { + format_int(val) // int.to_formatted_string(*locale) + } else { + val.to_string() + }; + + Value::String { + val: res, + span: head, + } + } + Value::Float { val, span } => { + if decimals { + let dec = BigDecimal::from_f64(val); + let decimal_value = digits.unwrap() as u64; + match dec { + Some(x) => Value::String { + val: format_decimal(x, Some(decimal_value), group_digits), + span, + }, + None => Value::Error { + error: ShellError::CantConvert( + String::from(format!("cannot convert {}to BigDecimal", val)), + head, + ), + }, + } + } else { + Value::String { + val: val.to_string(), + span: head, + } + } + } + // We do not seem to have BigInt at the moment as a Value Type + // Value::BigInt { val, span } => { + // let res = if group_digits { + // format_bigint(val) // int.to_formatted_string(*locale) + // } else { + // int.to_string() + // }; + + // Value::String { + // val: res, + // span: head, + // } + // .into_pipeline_data() + // } + Value::Bool { val, span } => Value::String { + val: val.to_string(), + span: head, + }, + + Value::Date { val, span } => Value::String { + val: val.format("%c").to_string(), + span: head, + }, + + Value::String { val, span } => Value::String { val, span: head }, + + // 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, span } => { + // let byte_string = InlineShape::format_bytes(*val, None); + // Ok(Value::String { + // val: byte_string.1, + // span, + // } + Value::String { + val: input.into_string(), + span: head, + } + } + Value::Nothing { span } => Value::String { + val: "nothing".to_string(), + span: head, + }, + Value::Record { cols, vals, span } => Value::Error { + error: ShellError::UnsupportedInput( + "Cannot convert Record into string".to_string(), + head, + ), + }, + + _ => Value::Error { + error: ShellError::CantConvert( + String::from(" into string. Probably this type is not supported yet"), + head, + ), + }, + } +} +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), + // } + // } +} + +fn format_bigint(int: &BigInt) -> 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), + // } + // } +} + +fn format_decimal(mut decimal: BigDecimal, digits: Option, group_digits: bool) -> String { + if let Some(n) = digits { + decimal = round_decimal(&decimal, n) + } + + if decimal.is_integer() && (digits.is_none() || digits == Some(0)) { + let int = decimal.as_bigint_and_exponent().0; + // .expect("integer BigDecimal should convert to BigInt"); + return if group_digits { + int.to_string() + } else { + format_bigint(&int) + }; + } + + let (int, exp) = decimal.as_bigint_and_exponent(); + let factor = BigInt::from(10).pow(BigUint::from(exp as u64)); // exp > 0 for non-int decimal + let int_part = &int / &factor; + let dec_part = (&int % &factor) + .abs() + .to_biguint() + .expect("BigInt::abs should always produce positive signed BigInt and thus BigUInt") + .to_str_radix(10); + + let dec_str = if let Some(n) = digits { + dec_part + .chars() + .chain(iter::repeat('0')) + .take(n as usize) + .collect() + } else { + String::from(dec_part.trim_end_matches('0')) + }; + + let format_default_loc = |int_part: BigInt| { + let loc = Locale::en; + //TODO: when num_format is available for recent bigint, replace this with the locale-based format + let (int_str, sep) = (int_part.to_string(), String::from(loc.decimal())); + + format!("{}{}{}", int_str, sep, dec_str) + }; + + format_default_loc(int_part) + + // TODO once platform-specific dependencies are stable (see Cargo.toml) + // #[cfg(windows)] + // { + // format_default_loc(int_part) + // } + // #[cfg(not(windows))] + // { + // match SystemLocale::default() { + // Ok(sys_loc) => { + // let int_str = int_part.to_formatted_string(&sys_loc); + // let sep = String::from(sys_loc.decimal()); + // format!("{}{}{}", int_str, sep, dec_str) + // } + // Err(_) => format_default_loc(int_part), + // } + // } +} + +fn round_decimal(decimal: &BigDecimal, mut digits: u64) -> BigDecimal { + let mut mag = decimal.clone(); + while mag >= BigDecimal::from(1) { + mag = mag / 10; + digits += 1; + } + + decimal.with_prec(digits) +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_examples() { + use crate::test_examples; + + test_examples(SubCommand {}) + } +} diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 3d41f27355..708c581fea 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -53,6 +53,7 @@ pub fn create_default_context() -> EngineState { IntoBinary, IntoFilesize, IntoInt, + IntoString, Last, Length, Let, From 78cc3452df44fb8c80131bf60476bb34ac5d62ec Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Tue, 2 Nov 2021 20:51:03 +0100 Subject: [PATCH 03/17] Fix clippy warnings for into string command --- .../nu-command/src/conversions/into/string.rs | 46 ++++++++----------- 1 file changed, 20 insertions(+), 26 deletions(-) diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index a74b630e2a..1f526726a8 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -1,15 +1,12 @@ -use nu_protocol::Value::Filesize; +use nu_engine::CallExt; use nu_protocol::{ ast::Call, engine::{Command, EngineState, Stack}, - Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value, + Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value, }; use bigdecimal::{BigDecimal, FromPrimitive}; - -use nu_engine::CallExt; - -use num_bigint::{BigInt, BigUint, ToBigInt}; +use num_bigint::{BigInt, BigUint}; use num_format::Locale; use num_traits::{Pow, Signed}; use std::iter; @@ -165,7 +162,7 @@ pub fn action( group_digits: bool, ) -> Value { match input { - Value::Int { val, span } => { + Value::Int { val, span: _ } => { let res = if group_digits { format_int(val) // int.to_formatted_string(*locale) } else { @@ -177,18 +174,18 @@ pub fn action( span: head, } } - Value::Float { val, span } => { + Value::Float { val, span: _ } => { if decimals { let dec = BigDecimal::from_f64(val); let decimal_value = digits.unwrap() as u64; match dec { Some(x) => Value::String { val: format_decimal(x, Some(decimal_value), group_digits), - span, + span: head, }, None => Value::Error { error: ShellError::CantConvert( - String::from(format!("cannot convert {}to BigDecimal", val)), + format!("cannot convert {} to BigDecimal", val), head, ), }, @@ -214,36 +211,33 @@ pub fn action( // } // .into_pipeline_data() // } - Value::Bool { val, span } => Value::String { + Value::Bool { val, span: _ } => Value::String { val: val.to_string(), span: head, }, - Value::Date { val, span } => Value::String { + Value::Date { val, span: _ } => Value::String { val: val.format("%c").to_string(), span: head, }, - Value::String { val, span } => Value::String { val, span: head }, + Value::String { val, span: _ } => Value::String { val, span: head }, // 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, span } => { - // let byte_string = InlineShape::format_bytes(*val, None); - // Ok(Value::String { - // val: byte_string.1, - // span, - // } - Value::String { - val: input.into_string(), - span: head, - } - } - Value::Nothing { span } => Value::String { + Value::Filesize { val: _, span: _ } => Value::String { + val: input.into_string(), + span: head, + }, + Value::Nothing { span: _ } => Value::String { val: "nothing".to_string(), span: head, }, - Value::Record { cols, vals, span } => Value::Error { + Value::Record { + cols: _, + vals: _, + span: _, + } => Value::Error { error: ShellError::UnsupportedInput( "Cannot convert Record into string".to_string(), head, From 86e1092785a09b0a8ef8517c8bc75f42b0423c75 Mon Sep 17 00:00:00 2001 From: JT Date: Wed, 3 Nov 2021 13:26:09 +1300 Subject: [PATCH 04/17] Add more api docs --- crates/nu-protocol/src/pipeline_data.rs | 15 +++++- crates/nu-protocol/src/shell_error.rs | 3 ++ crates/nu-protocol/src/span.rs | 4 ++ crates/nu-protocol/src/value/range.rs | 12 ----- crates/nu-protocol/src/value/stream.rs | 65 +++---------------------- 5 files changed, 26 insertions(+), 73 deletions(-) diff --git a/crates/nu-protocol/src/pipeline_data.rs b/crates/nu-protocol/src/pipeline_data.rs index 47b10b40db..182c253092 100644 --- a/crates/nu-protocol/src/pipeline_data.rs +++ b/crates/nu-protocol/src/pipeline_data.rs @@ -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. diff --git a/crates/nu-protocol/src/shell_error.rs b/crates/nu-protocol/src/shell_error.rs index 425fec37b6..2579856452 100644 --- a/crates/nu-protocol/src/shell_error.rs +++ b/crates/nu-protocol/src/shell_error.rs @@ -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.")] diff --git a/crates/nu-protocol/src/span.rs b/crates/nu-protocol/src/span.rs index b4b32cec64..aa6192c603 100644 --- a/crates/nu-protocol/src/span.rs +++ b/crates/nu-protocol/src/span.rs @@ -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 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, diff --git a/crates/nu-protocol/src/value/range.rs b/crates/nu-protocol/src/value/range.rs index 24e6e6b7b8..63949d98da 100644 --- a/crates/nu-protocol/src/value/range.rs +++ b/crates/nu-protocol/src/value/range.rs @@ -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, diff --git a/crates/nu-protocol/src/value/stream.rs b/crates/nu-protocol/src/value/stream.rs index 3c69cc504e..94cca448dd 100644 --- a/crates/nu-protocol/src/value/stream.rs +++ b/crates/nu-protocol/src/value/stream.rs @@ -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 + Send + 'static>, pub ctrlc: Option>, @@ -60,62 +66,3 @@ impl Iterator for ValueStream { } } } - -// impl Serialize for ValueStream { -// fn serialize(&self, serializer: S) -> Result -// 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(deserializer: D) -> Result -// 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(self, mut seq: A) -> Result -// where -// A: serde::de::SeqAccess<'a>, -// { -// let mut output: Vec = 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 IntoValueStream for T -// where -// T: Iterator + 'static, -// { -// fn into_value_stream(self) -> ValueStream { -// ValueStream::from_stream(self) -// } -// } From 756773a6edb62c5f02aa76d3d9b4ea767f0b1ad5 Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Tue, 2 Nov 2021 21:17:27 -0300 Subject: [PATCH 05/17] MathFloor done and MathMode still left work Math mode final form currently MathMode and MathFloor --- crates/nu-command/src/default_context.rs | 2 + crates/nu-command/src/math/floor.rs | 74 ++++++++++ crates/nu-command/src/math/mod.rs | 4 + crates/nu-command/src/math/mode.rs | 174 +++++++++++++++++++++++ 4 files changed, 254 insertions(+) create mode 100644 crates/nu-command/src/math/floor.rs create mode 100644 crates/nu-command/src/math/mode.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 52dc3f309b..428caa5607 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -62,12 +62,14 @@ pub fn create_default_context() -> EngineState { Math, MathAbs, MathAvg, + MathFloor, MathMax, MathMin, MathProduct, MathRound, MathSqrt, MathSum, + MathMode, Mkdir, Module, Mv, diff --git a/crates/nu-command/src/math/floor.rs b/crates/nu-command/src/math/floor.rs new file mode 100644 index 0000000000..84f4c93886 --- /dev/null +++ b/crates/nu-command/src/math/floor.rs @@ -0,0 +1,74 @@ +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 { + let head = call.head; + input.map( + move |value| operate(value, head), + engine_state.ctrlc.clone(), + ) + } + + fn examples(&self) -> Vec { + 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 tests { + use super::ShellError; + use super::SubCommand; + + #[test] + fn examples_work_as_expected() -> Result<(), ShellError> { + use crate::examples::test as test_examples; + + test_examples(SubCommand {}) + } +} diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs index 66eff72d82..96466658db 100644 --- a/crates/nu-command/src/math/mod.rs +++ b/crates/nu-command/src/math/mod.rs @@ -1,8 +1,10 @@ mod abs; mod avg; pub mod command; +mod floor; mod max; mod min; +mod mode; mod product; mod reducers; mod round; @@ -13,8 +15,10 @@ mod utils; pub use abs::SubCommand as MathAbs; pub use avg::SubCommand as MathAvg; pub use command::MathCommand as Math; +pub use floor::SubCommand as MathFloor; pub use max::SubCommand as MathMax; 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; diff --git a/crates/nu-command/src/math/mode.rs b/crates/nu-command/src/math/mode.rs new file mode 100644 index 0000000000..126d6abc38 --- /dev/null +++ b/crates/nu-command/src/math/mode.rs @@ -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 { + run_with_function(call, input, mode) + } + + fn examples(&self) -> Vec { + 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 { + 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, ShellError> = values + .into_iter() + .map(|val| match val { + Value::Int { val, .. } => Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Int)), + Value::Duration { val, .. } => { + Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Duration)) + } + Value::Float { val, .. } => { + Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Float)) + } + Value::Filesize { val, .. } => { + Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Filesize)) + } + other => Err(ShellError::UnsupportedInput( + "Unable to give a result with this input".to_string(), + other.span().unwrap(), + )), + }) + .collect::, 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::::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_be_bytes(bytes), + span: head, + }, + NumberTypes::Float => Value::Float { + val: f64::from_be_bytes(bytes), + span: head, + }, + NumberTypes::Duration => Value::Duration { + val: i64::from_be_bytes(bytes), + span: head, + }, + NumberTypes::Filesize => Value::Filesize { + val: i64::from_be_bytes(bytes), + span: head, + }, + } +} + +#[cfg(test)] +mod test { + use super::*; + + #[test] + fn test_examples() { + use crate::test_examples; + + test_examples(SubCommand {}) + } +} From bf6c3e53a07527ae60b896f160d0bc5fa93312b3 Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Wed, 3 Nov 2021 08:38:31 +0100 Subject: [PATCH 06/17] Remove BigDecimal and use i64/f64 instead --- .../nu-command/src/conversions/into/string.rs | 159 ++---------------- 1 file changed, 18 insertions(+), 141 deletions(-) diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index 1f526726a8..8392218c1a 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -5,11 +5,6 @@ use nu_protocol::{ Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value, }; -use bigdecimal::{BigDecimal, FromPrimitive}; -use num_bigint::{BigInt, BigUint}; -use num_format::Locale; -use num_traits::{Pow, Signed}; -use std::iter; // TODO num_format::SystemLocale once platform-specific dependencies are stable (see Cargo.toml) #[derive(Clone)] @@ -156,82 +151,54 @@ fn string_helper( pub fn action( input: Value, - head: Span, + span: Span, decimals: bool, digits: Option, group_digits: bool, ) -> Value { match input { - Value::Int { val, span: _ } => { + 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: head, - } + Value::String { val: res, span } } - Value::Float { val, span: _ } => { + Value::Float { val, .. } => { if decimals { - let dec = BigDecimal::from_f64(val); - let decimal_value = digits.unwrap() as u64; - match dec { - Some(x) => Value::String { - val: format_decimal(x, Some(decimal_value), group_digits), - span: head, - }, - None => Value::Error { - error: ShellError::CantConvert( - format!("cannot convert {} to BigDecimal", val), - head, - ), - }, + let decimal_value = digits.unwrap() as usize; + Value::String { + val: format!("{:.*}", decimal_value, val), + span, } } else { Value::String { val: val.to_string(), - span: head, + span, } } } - // We do not seem to have BigInt at the moment as a Value Type - // Value::BigInt { val, span } => { - // let res = if group_digits { - // format_bigint(val) // int.to_formatted_string(*locale) - // } else { - // int.to_string() - // }; - - // Value::String { - // val: res, - // span: head, - // } - // .into_pipeline_data() - // } - Value::Bool { val, span: _ } => Value::String { + Value::Bool { val, .. } => Value::String { val: val.to_string(), - span: head, + span, }, - - Value::Date { val, span: _ } => Value::String { + Value::Date { val, .. } => Value::String { val: val.format("%c").to_string(), - span: head, + span, }, - - Value::String { val, span: _ } => Value::String { val, span: head }, + 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: _, span: _ } => Value::String { + Value::Filesize { val: _, .. } => Value::String { val: input.into_string(), - span: head, + span, }, - Value::Nothing { span: _ } => Value::String { + Value::Nothing { .. } => Value::String { val: "nothing".to_string(), - span: head, + span, }, Value::Record { cols: _, @@ -243,7 +210,6 @@ pub fn action( head, ), }, - _ => Value::Error { error: ShellError::CantConvert( String::from(" into string. Probably this type is not supported yet"), @@ -269,95 +235,6 @@ fn format_int(int: i64) -> String { // } } -fn format_bigint(int: &BigInt) -> 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), - // } - // } -} - -fn format_decimal(mut decimal: BigDecimal, digits: Option, group_digits: bool) -> String { - if let Some(n) = digits { - decimal = round_decimal(&decimal, n) - } - - if decimal.is_integer() && (digits.is_none() || digits == Some(0)) { - let int = decimal.as_bigint_and_exponent().0; - // .expect("integer BigDecimal should convert to BigInt"); - return if group_digits { - int.to_string() - } else { - format_bigint(&int) - }; - } - - let (int, exp) = decimal.as_bigint_and_exponent(); - let factor = BigInt::from(10).pow(BigUint::from(exp as u64)); // exp > 0 for non-int decimal - let int_part = &int / &factor; - let dec_part = (&int % &factor) - .abs() - .to_biguint() - .expect("BigInt::abs should always produce positive signed BigInt and thus BigUInt") - .to_str_radix(10); - - let dec_str = if let Some(n) = digits { - dec_part - .chars() - .chain(iter::repeat('0')) - .take(n as usize) - .collect() - } else { - String::from(dec_part.trim_end_matches('0')) - }; - - let format_default_loc = |int_part: BigInt| { - let loc = Locale::en; - //TODO: when num_format is available for recent bigint, replace this with the locale-based format - let (int_str, sep) = (int_part.to_string(), String::from(loc.decimal())); - - format!("{}{}{}", int_str, sep, dec_str) - }; - - format_default_loc(int_part) - - // TODO once platform-specific dependencies are stable (see Cargo.toml) - // #[cfg(windows)] - // { - // format_default_loc(int_part) - // } - // #[cfg(not(windows))] - // { - // match SystemLocale::default() { - // Ok(sys_loc) => { - // let int_str = int_part.to_formatted_string(&sys_loc); - // let sep = String::from(sys_loc.decimal()); - // format!("{}{}{}", int_str, sep, dec_str) - // } - // Err(_) => format_default_loc(int_part), - // } - // } -} - -fn round_decimal(decimal: &BigDecimal, mut digits: u64) -> BigDecimal { - let mut mag = decimal.clone(); - while mag >= BigDecimal::from(1) { - mag = mag / 10; - digits += 1; - } - - decimal.with_prec(digits) -} - #[cfg(test)] mod test { use super::*; From 6906de7c4893b60d8b4b5463f6a4520613b2294f Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Wed, 3 Nov 2021 08:48:13 +0100 Subject: [PATCH 07/17] Ooops fix the wrong naming --- crates/nu-command/src/conversions/into/string.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/conversions/into/string.rs b/crates/nu-command/src/conversions/into/string.rs index 8392218c1a..31e9fa9c04 100644 --- a/crates/nu-command/src/conversions/into/string.rs +++ b/crates/nu-command/src/conversions/into/string.rs @@ -207,13 +207,13 @@ pub fn action( } => Value::Error { error: ShellError::UnsupportedInput( "Cannot convert Record into string".to_string(), - head, + span, ), }, _ => Value::Error { error: ShellError::CantConvert( String::from(" into string. Probably this type is not supported yet"), - head, + span, ), }, } From 20f3b8b27418ca1c3b1803e9352345e40ec8b171 Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Wed, 3 Nov 2021 10:41:01 +0100 Subject: [PATCH 08/17] Remove unnecessary crate imports --- crates/nu-command/Cargo.toml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/crates/nu-command/Cargo.toml b/crates/nu-command/Cargo.toml index b822e6084e..99d13d33c3 100644 --- a/crates/nu-command/Cargo.toml +++ b/crates/nu-command/Cargo.toml @@ -22,10 +22,6 @@ unicode-segmentation = "1.8.0" glob = "0.3.0" thiserror = "1.0.29" sysinfo = "0.20.4" -bigdecimal = { package = "bigdecimal-rs", version = "0.2.1", features = ["serde"] } -num-bigint = { version="0.3.1", features=["serde"] } -num-format = { version="0.4.0", features=["with-num-bigint"] } -num-traits = "0.2.14" chrono = { version = "0.4.19", features = ["serde"] } chrono-humanize = "0.2.1" chrono-tz = "0.6.0" From 5ae823612fd975f6669143844e3dd0adc7b6a5be Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Wed, 3 Nov 2021 08:59:08 -0300 Subject: [PATCH 09/17] MathCeil, MathFloor and MathMode --- crates/nu-command/src/default_context.rs | 1 + crates/nu-command/src/math/ceil.rs | 73 ++++++++++++++++++++++++ crates/nu-command/src/math/mod.rs | 2 + crates/nu-command/src/math/mode.rs | 2 +- 4 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 crates/nu-command/src/math/ceil.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 428caa5607..5f9f6d4eb4 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -62,6 +62,7 @@ pub fn create_default_context() -> EngineState { Math, MathAbs, MathAvg, + MathCeil, MathFloor, MathMax, MathMin, diff --git a/crates/nu-command/src/math/ceil.rs b/crates/nu-command/src/math/ceil.rs new file mode 100644 index 0000000000..a23720ffbe --- /dev/null +++ b/crates/nu-command/src/math/ceil.rs @@ -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 { + let head = call.head; + input.map( + move |value| operate(value, head), + engine_state.ctrlc.clone(), + ) + } + + fn examples(&self) -> Vec { + 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 {}) + } +} diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs index 96466658db..2ff997c4e3 100644 --- a/crates/nu-command/src/math/mod.rs +++ b/crates/nu-command/src/math/mod.rs @@ -1,5 +1,6 @@ mod abs; mod avg; +mod ceil; pub mod command; mod floor; mod max; @@ -14,6 +15,7 @@ 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; diff --git a/crates/nu-command/src/math/mode.rs b/crates/nu-command/src/math/mode.rs index 126d6abc38..b3eb646b16 100644 --- a/crates/nu-command/src/math/mode.rs +++ b/crates/nu-command/src/math/mode.rs @@ -88,7 +88,7 @@ pub fn mode(values: &[Value], head: &Span) -> Result { // But f64 doesn't implement Hash, so we get the binary representation to use as // key in the HashMap let hashable_values: Result, ShellError> = values - .into_iter() + .iter() .map(|val| match val { Value::Int { val, .. } => Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Int)), Value::Duration { val, .. } => { From d3e5c5a34247fe7d089f236bd0073f647277986f Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Wed, 3 Nov 2021 09:19:28 -0300 Subject: [PATCH 10/17] Fix tests --- crates/nu-command/src/math/floor.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/math/floor.rs b/crates/nu-command/src/math/floor.rs index 84f4c93886..ea1e4cbb43 100644 --- a/crates/nu-command/src/math/floor.rs +++ b/crates/nu-command/src/math/floor.rs @@ -61,13 +61,12 @@ fn operate(value: Value, head: Span) -> Value { } #[cfg(test)] -mod tests { - use super::ShellError; - use super::SubCommand; +mod test { + use super::*; #[test] - fn examples_work_as_expected() -> Result<(), ShellError> { - use crate::examples::test as test_examples; + fn test_examples() { + use crate::test_examples; test_examples(SubCommand {}) } From 806cd4851fe926d0f97dae53065f02b33d350e54 Mon Sep 17 00:00:00 2001 From: Antonio Natilla Date: Wed, 3 Nov 2021 19:46:33 +0100 Subject: [PATCH 11/17] Format implementation, fix on Echo Now, Echo converts multiple values in a ValueStream, but it simply forwards a single Value; if no PipelineData is detected as an input, an empty string is returned as a single Value. --- crates/nu-command/src/core_commands/echo.rs | 26 ++- crates/nu-command/src/example_test.rs | 2 + .../nu-command/src/strings/format/command.rs | 193 ++++++++++++++++++ .../nu-command/src/strings/format/format.rs | 53 ----- crates/nu-command/src/strings/format/mod.rs | 4 +- 5 files changed, 215 insertions(+), 63 deletions(-) create mode 100644 crates/nu-command/src/strings/format/command.rs delete mode 100644 crates/nu-command/src/strings/format/format.rs diff --git a/crates/nu-command/src/core_commands/echo.rs b/crates/nu-command/src/core_commands/echo.rs index af0c994893..064d52bdd2 100644 --- a/crates/nu-command/src/core_commands/echo.rs +++ b/crates/nu-command/src/core_commands/echo.rs @@ -29,10 +29,23 @@ impl Command for Echo { _input: PipelineData, ) -> Result { 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", diff --git a/crates/nu-command/src/example_test.rs b/crates/nu-command/src/example_test.rs index bc3c819a75..635a5ea005 100644 --- a/crates/nu-command/src/example_test.rs +++ b/crates/nu-command/src/example_test.rs @@ -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)); diff --git a/crates/nu-command/src/strings/format/command.rs b/crates/nu-command/src/strings/format/command.rs new file mode 100644 index 0000000000..81e3334e1f --- /dev/null +++ b/crates/nu-command/src/strings/format/command.rs @@ -0,0 +1,193 @@ +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 { + let specified_pattern: Result = call.req(engine_state, stack, 0); + match specified_pattern { + Err(e) => Err(e), + Ok(pattern) => { + // Handle the pattern + let string_pattern = pattern.as_string().unwrap(); + let ops = extract_formatting_operations(string_pattern); + format(input, &ops) + } + } + } + + fn examples(&self) -> Vec { + 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), +} + +fn extract_formatting_operations(input: String) -> Vec { + 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 +} + +fn format( + input_data: PipelineData, + format_operations: &[FormatOperation], +) -> Result { + let data_as_value = input_data.into_value(); + eprintln!("{:#?}", data_as_value); + 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 { + 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 {}) + } +} diff --git a/crates/nu-command/src/strings/format/format.rs b/crates/nu-command/src/strings/format/format.rs deleted file mode 100644 index 5f64661a3a..0000000000 --- a/crates/nu-command/src/strings/format/format.rs +++ /dev/null @@ -1,53 +0,0 @@ -//use nu_engine::CallExt; -use nu_protocol::ast::Call; -use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape}; - -#[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 { - todo!() - } - - fn examples(&self) -> Vec { - vec![Example { - description: "Print filenames with their sizes", - example: "ls | format '{name}: {size}'", - result: None, - }] - } -} - -#[cfg(test)] -mod test { - #[test] - fn test_examples() { - use super::Format; - use crate::test_examples; - test_examples(Format {}) - } -} diff --git a/crates/nu-command/src/strings/format/mod.rs b/crates/nu-command/src/strings/format/mod.rs index c20d416331..71be06ceb0 100644 --- a/crates/nu-command/src/strings/format/mod.rs +++ b/crates/nu-command/src/strings/format/mod.rs @@ -1,3 +1,3 @@ -pub mod format; +pub mod command; -pub use format::Format; +pub use command::Format; From bfae75ca2e8e1baa14f0ce5aed2604dd335b31c2 Mon Sep 17 00:00:00 2001 From: Antonio Natilla Date: Wed, 3 Nov 2021 20:05:24 +0100 Subject: [PATCH 12/17] Clean-up and adding comments --- crates/nu-command/src/strings/format/command.rs | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/crates/nu-command/src/strings/format/command.rs b/crates/nu-command/src/strings/format/command.rs index 81e3334e1f..4c78e5b46f 100644 --- a/crates/nu-command/src/strings/format/command.rs +++ b/crates/nu-command/src/strings/format/command.rs @@ -36,7 +36,6 @@ impl Command for Format { match specified_pattern { Err(e) => Err(e), Ok(pattern) => { - // Handle the pattern let string_pattern = pattern.as_string().unwrap(); let ops = extract_formatting_operations(string_pattern); format(input, &ops) @@ -69,6 +68,12 @@ enum FormatOperation { 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 { let mut output = vec![]; @@ -107,12 +112,14 @@ fn extract_formatting_operations(input: String) -> Vec { output } +/// Format the incoming PipelineData according to the pattern fn format( input_data: PipelineData, format_operations: &[FormatOperation], ) -> Result { let data_as_value = input_data.into_value(); - eprintln!("{:#?}", data_as_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()))), From 47ebde4087ae456830986a04d988173ebbce09e2 Mon Sep 17 00:00:00 2001 From: Luccas Mateus de Medeiros Gomes Date: Wed, 3 Nov 2021 18:24:15 -0300 Subject: [PATCH 13/17] Added MathMedian Added MathMedian Fix tests --- crates/nu-command/src/default_context.rs | 3 +- crates/nu-command/src/math/median.rs | 121 +++++++++++++++++++++++ crates/nu-command/src/math/mod.rs | 2 + 3 files changed, 125 insertions(+), 1 deletion(-) create mode 100644 crates/nu-command/src/math/median.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 5f9f6d4eb4..82e991ca65 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -65,12 +65,13 @@ pub fn create_default_context() -> EngineState { MathCeil, MathFloor, MathMax, + MathMedian, MathMin, + MathMode, MathProduct, MathRound, MathSqrt, MathSum, - MathMode, Mkdir, Module, Mv, diff --git a/crates/nu-command/src/math/median.rs b/crates/nu-command/src/math/median.rs new file mode 100644 index 0000000000..1204485fca --- /dev/null +++ b/crates/nu-command/src/math/median.rs @@ -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 { + run_with_function(call, input, median) + } + + fn examples(&self) -> Vec { + 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 { + 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 {}) + } +} diff --git a/crates/nu-command/src/math/mod.rs b/crates/nu-command/src/math/mod.rs index 2ff997c4e3..bf392f68c1 100644 --- a/crates/nu-command/src/math/mod.rs +++ b/crates/nu-command/src/math/mod.rs @@ -4,6 +4,7 @@ mod ceil; pub mod command; mod floor; mod max; +mod median; mod min; mod mode; mod product; @@ -19,6 +20,7 @@ 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; From 112ebe18425b26037197bf0f16a1281e9558a1ac Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Wed, 3 Nov 2021 22:44:30 +0100 Subject: [PATCH 14/17] Add back binary support for first command --- crates/nu-command/src/filters/first.rs | 85 ++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 13 deletions(-) diff --git a/crates/nu-command/src/filters/first.rs b/crates/nu-command/src/filters/first.rs index 873e07026c..58edcd787d 100644 --- a/crates/nu-command/src/filters/first.rs +++ b/crates/nu-command/src/filters/first.rs @@ -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 { let head = call.head; let rows: Option = 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_if(|val| val.get_type() == Type::Binary) { + 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 = 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::*; From 260838e5eaa362763f3a0b822557f291e27a5d28 Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Wed, 3 Nov 2021 22:48:12 +0100 Subject: [PATCH 15/17] Switch next_if to next as we already know it's of type Binary --- crates/nu-command/src/filters/first.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/nu-command/src/filters/first.rs b/crates/nu-command/src/filters/first.rs index 58edcd787d..1d2275984d 100644 --- a/crates/nu-command/src/filters/first.rs +++ b/crates/nu-command/src/filters/first.rs @@ -71,7 +71,7 @@ fn first_helper( if input_peek.peek().is_some() { match input_peek.peek().unwrap().get_type() { Type::Binary => { - match &mut input_peek.next_if(|val| val.get_type() == Type::Binary) { + match &mut input_peek.next() { Some(v) => match &v { Value::Binary { val, .. } => { let bytes = val; From 1949ba080e3f1f33e04efaff62f33f6eb2bc8b9d Mon Sep 17 00:00:00 2001 From: JT Date: Thu, 4 Nov 2021 15:32:35 +1300 Subject: [PATCH 16/17] Add env shorthand --- crates/nu-command/src/default_context.rs | 1 + crates/nu-command/src/env/mod.rs | 2 + crates/nu-command/src/env/with_env.rs | 180 ++++++++++++++++++++ crates/nu-command/src/system/benchmark.rs | 11 +- crates/nu-parser/src/parser.rs | 191 +++++++++++++++++++--- crates/nu-protocol/src/engine/stack.rs | 3 + crates/nu-protocol/src/value/mod.rs | 4 +- src/tests.rs | 24 ++- 8 files changed, 384 insertions(+), 32 deletions(-) create mode 100644 crates/nu-command/src/env/with_env.rs diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index 95635f093c..116b33b3cf 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -91,6 +91,7 @@ pub fn create_default_context() -> EngineState { Touch, Use, Where, + WithEnv, Wrap, Zip ); diff --git a/crates/nu-command/src/env/mod.rs b/crates/nu-command/src/env/mod.rs index fa42c1b093..48e8293dfd 100644 --- a/crates/nu-command/src/env/mod.rs +++ b/crates/nu-command/src/env/mod.rs @@ -1,3 +1,5 @@ mod let_env; +mod with_env; pub use let_env::LetEnv; +pub use with_env::WithEnv; diff --git a/crates/nu-command/src/env/with_env.rs b/crates/nu-command/src/env/with_env.rs new file mode 100644 index 0000000000..ae3d3a3776 --- /dev/null +++ b/crates/nu-command/src/env/with_env.rs @@ -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 { + with_env(engine_state, stack, call, input) + } + + fn examples(&self) -> Vec { + 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 { + 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 { + // 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 = 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 {}) + } +} diff --git a/crates/nu-command/src/system/benchmark.rs b/crates/nu-command/src/system/benchmark.rs index 7b99c2009a..e786c40885 100644 --- a/crates/nu-command/src/system/benchmark.rs +++ b/crates/nu-command/src/system/benchmark.rs @@ -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()) } } diff --git a/crates/nu-parser/src/parser.rs b/crates/nu-parser/src/parser.rs index 25be7f6d79..3d932b4c89 100644 --- a/crates/nu-parser/src/parser.rs +++ b/crates/nu-parser/src/parser.rs @@ -627,27 +627,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![]; @@ -1681,6 +1660,54 @@ pub fn parse_string( } } +pub fn parse_string_strict( + working_set: &mut StateWorkingSet, + span: Span, +) -> (Expression, Option) { + 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, @@ -2963,12 +2990,126 @@ pub fn parse_expression( spans: &[Span], expand_aliases: bool, ) -> (Expression, Option) { - 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) } } diff --git a/crates/nu-protocol/src/engine/stack.rs b/crates/nu-protocol/src/engine/stack.rs index 45b013de59..f2fc9fc0ac 100644 --- a/crates/nu-protocol/src/engine/stack.rs +++ b/crates/nu-protocol/src/engine/stack.rs @@ -64,6 +64,9 @@ impl Stack { } } + // FIXME: this is probably slow + output.env_vars = self.env_vars.clone(); + output } diff --git a/crates/nu-protocol/src/value/mod.rs b/crates/nu-protocol/src/value/mod.rs index b6ea1e2768..84ff1dd51d 100644 --- a/crates/nu-protocol/src/value/mod.rs +++ b/crates/nu-protocol/src/value/mod.rs @@ -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()) diff --git a/src/tests.rs b/src/tests.rs index 41595e65c2..a9042eb06d 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -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") } From d23929fc8099f25d99420f78f85938f649eadf19 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Fri, 5 Nov 2021 07:04:02 +1300 Subject: [PATCH 17/17] Update mode.rs trying a switch to native endian --- crates/nu-command/src/math/mode.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/crates/nu-command/src/math/mode.rs b/crates/nu-command/src/math/mode.rs index b3eb646b16..baa8f2e739 100644 --- a/crates/nu-command/src/math/mode.rs +++ b/crates/nu-command/src/math/mode.rs @@ -90,15 +90,15 @@ pub fn mode(values: &[Value], head: &Span) -> Result { let hashable_values: Result, ShellError> = values .iter() .map(|val| match val { - Value::Int { val, .. } => Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Int)), + Value::Int { val, .. } => Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Int)), Value::Duration { val, .. } => { - Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Duration)) + Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Duration)) } Value::Float { val, .. } => { - Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Float)) + Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Float)) } Value::Filesize { val, .. } => { - Ok(HashableType::new(val.to_be_bytes(), NumberTypes::Filesize)) + Ok(HashableType::new(val.to_ne_bytes(), NumberTypes::Filesize)) } other => Err(ShellError::UnsupportedInput( "Unable to give a result with this input".to_string(), @@ -143,19 +143,19 @@ 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_be_bytes(bytes), + val: i64::from_ne_bytes(bytes), span: head, }, NumberTypes::Float => Value::Float { - val: f64::from_be_bytes(bytes), + val: f64::from_ne_bytes(bytes), span: head, }, NumberTypes::Duration => Value::Duration { - val: i64::from_be_bytes(bytes), + val: i64::from_ne_bytes(bytes), span: head, }, NumberTypes::Filesize => Value::Filesize { - val: i64::from_be_bytes(bytes), + val: i64::from_ne_bytes(bytes), span: head, }, }