mirror of
https://github.com/nushell/nushell.git
synced 2025-04-30 08:04:25 +02:00
Allow echo to iterate ranges (#1905)
This commit is contained in:
parent
e2dabecc0b
commit
98a3d9fff6
@ -1,7 +1,10 @@
|
|||||||
use crate::commands::WholeStreamCommand;
|
use crate::commands::WholeStreamCommand;
|
||||||
use crate::prelude::*;
|
use crate::prelude::*;
|
||||||
use nu_errors::ShellError;
|
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;
|
pub struct Echo;
|
||||||
|
|
||||||
@ -68,6 +71,37 @@ fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, S
|
|||||||
yield Ok(ReturnSuccess::Value(value.clone()));
|
yield Ok(ReturnSuccess::Value(value.clone()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Value {
|
||||||
|
value: UntaggedValue::Primitive(Primitive::Range(range)),
|
||||||
|
tag
|
||||||
|
} => {
|
||||||
|
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()));
|
yield Ok(ReturnSuccess::Value(i.clone()));
|
||||||
}
|
}
|
||||||
|
@ -72,7 +72,7 @@ pub(crate) async fn evaluate_baseline_expr(
|
|||||||
);
|
);
|
||||||
let right = (
|
let right = (
|
||||||
right.as_primitive()?.spanned(right_span),
|
right.as_primitive()?.spanned(right_span),
|
||||||
RangeInclusion::Exclusive,
|
RangeInclusion::Inclusive,
|
||||||
);
|
);
|
||||||
|
|
||||||
Ok(UntaggedValue::range(left, right).into_value(tag))
|
Ok(UntaggedValue::range(left, right).into_value(tag))
|
||||||
|
@ -193,6 +193,18 @@ fn can_process_one_row_from_internal_and_pipes_it_to_stdin_of_external() {
|
|||||||
assert_eq!(actual.out, "nushell");
|
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 {
|
mod parse {
|
||||||
use nu_test_support::nu;
|
use nu_test_support::nu;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user