diff --git a/crates/nu-cli/src/commands/echo.rs b/crates/nu-cli/src/commands/echo.rs index 17ad0e8d61..68b446d555 100644 --- a/crates/nu-cli/src/commands/echo.rs +++ b/crates/nu-cli/src/commands/echo.rs @@ -1,7 +1,10 @@ use crate::commands::WholeStreamCommand; use crate::prelude::*; use nu_errors::ShellError; -use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; +use nu_protocol::hir::Operator; +use nu_protocol::{ + Primitive, RangeInclusion, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value, +}; pub struct Echo; @@ -68,6 +71,37 @@ fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result { + let mut current = range.from.0.item; + while current != range.to.0.item { + yield Ok(ReturnSuccess::Value(UntaggedValue::Primitive(current.clone()).into_value(&tag))); + current = match crate::data::value::compute_values(Operator::Plus, &UntaggedValue::Primitive(current), &UntaggedValue::int(1)) { + Ok(result) => match result { + UntaggedValue::Primitive(p) => p, + _ => { + yield Err(ShellError::unimplemented("Internal error: expected a primitive result from increment")); + return; + } + }, + Err((left_type, right_type)) => { + yield Err(ShellError::coerce_error( + left_type.spanned(tag.span), + right_type.spanned(tag.span), + )); + return; + } + } + } + match range.to.1 { + RangeInclusion::Inclusive => { + yield Ok(ReturnSuccess::Value(UntaggedValue::Primitive(current.clone()).into_value(&tag))); + } + _ => {} + } + } _ => { yield Ok(ReturnSuccess::Value(i.clone())); } diff --git a/crates/nu-cli/src/evaluate/evaluator.rs b/crates/nu-cli/src/evaluate/evaluator.rs index c1896c1d19..026e7b4663 100644 --- a/crates/nu-cli/src/evaluate/evaluator.rs +++ b/crates/nu-cli/src/evaluate/evaluator.rs @@ -72,7 +72,7 @@ pub(crate) async fn evaluate_baseline_expr( ); let right = ( right.as_primitive()?.spanned(right_span), - RangeInclusion::Exclusive, + RangeInclusion::Inclusive, ); Ok(UntaggedValue::range(left, right).into_value(tag)) diff --git a/tests/shell/pipeline/commands/internal.rs b/tests/shell/pipeline/commands/internal.rs index 145b809115..d5ebf7da8a 100644 --- a/tests/shell/pipeline/commands/internal.rs +++ b/tests/shell/pipeline/commands/internal.rs @@ -193,6 +193,18 @@ fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() { assert_eq!(actual.out, "nushell"); } +#[test] +fn echoing_ranges() { + let actual = nu!( + cwd: ".", + r#" + echo 1..3 | sum + "# + ); + + assert_eq!(actual.out, "6"); +} + mod parse { use nu_test_support::nu;