mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
in for, loop, while, auto print final value in each iteration (#7433)
# Description Fixes: #7404 Fixes: #7402 ## About change In `eval_block`, all pipelines(or called statements?) result will be printed except the last one, the last one is returned by `eval_block` function. So if we want to print the last statement in eval block, we just need to print that value. # User-Facing Changes ### for ``` ❯ for _ in 1..2 { echo "a" } a a ``` ### while ``` ❯ mut x = 1; while $x < 3 { $x = $x + 1; echo bb; } bb bb ``` ### loop ``` ❯ mut total = 0; loop { ∙ if $total > 1 { ∙ break ∙ } else { ∙ $total += 1 ∙ } ∙ echo 3 ∙ } 3 3 ``` # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
9009f68e09
commit
585ab56ea4
@ -118,7 +118,7 @@ impl Command for For {
|
||||
return Err(err);
|
||||
}
|
||||
Ok(pipeline) => {
|
||||
pipeline.into_value(head);
|
||||
let _ = pipeline.print(&engine_state, stack, false, false)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -157,7 +157,7 @@ impl Command for For {
|
||||
return Err(err);
|
||||
}
|
||||
Ok(pipeline) => {
|
||||
pipeline.into_value(head);
|
||||
let _ = pipeline.print(&engine_state, stack, false, false)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -69,7 +69,7 @@ impl Command for Loop {
|
||||
return Err(err);
|
||||
}
|
||||
Ok(pipeline) => {
|
||||
pipeline.into_value(call.head);
|
||||
let _ = pipeline.print(engine_state, stack, false, false)?;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -77,7 +77,7 @@ impl Command for While {
|
||||
return Err(err);
|
||||
}
|
||||
Ok(pipeline) => {
|
||||
pipeline.into_value(call.head);
|
||||
let _ = pipeline.print(engine_state, stack, false, false)?;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
16
crates/nu-command/tests/commands/for_.rs
Normal file
16
crates/nu-command/tests/commands/for_.rs
Normal file
@ -0,0 +1,16 @@
|
||||
use nu_test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn for_auto_print_in_each_iteration() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
for i in 1..2 {
|
||||
echo 1
|
||||
}"#
|
||||
);
|
||||
// Note: nu! macro auto repalce "\n" and "\r\n" with ""
|
||||
// so our output will be `11`
|
||||
// that's ok, our main concern is it auto print value in each iteration.
|
||||
assert_eq!(actual.out, "11");
|
||||
}
|
22
crates/nu-command/tests/commands/loop_.rs
Normal file
22
crates/nu-command/tests/commands/loop_.rs
Normal file
@ -0,0 +1,22 @@
|
||||
use nu_test_support::nu;
|
||||
|
||||
#[test]
|
||||
fn loop_auto_print_in_each_iteration() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
r#"
|
||||
mut total = 0;
|
||||
loop {
|
||||
if $total == 3 {
|
||||
break;
|
||||
} else {
|
||||
$total += 1;
|
||||
}
|
||||
echo 1
|
||||
}"#
|
||||
);
|
||||
// Note: nu! macro auto repalce "\n" and "\r\n" with ""
|
||||
// so our output will be `111`
|
||||
// that's ok, our main concern is it auto print value in each iteration.
|
||||
assert_eq!(actual.out, "111");
|
||||
}
|
@ -24,6 +24,7 @@ mod export_def;
|
||||
mod find;
|
||||
mod first;
|
||||
mod flatten;
|
||||
mod for_;
|
||||
mod format;
|
||||
mod g;
|
||||
mod get;
|
||||
@ -41,6 +42,7 @@ mod last;
|
||||
mod length;
|
||||
mod let_;
|
||||
mod lines;
|
||||
mod loop_;
|
||||
mod ls;
|
||||
mod math;
|
||||
mod merge;
|
||||
|
@ -9,3 +9,15 @@ fn while_sum() {
|
||||
|
||||
assert_eq!(actual.out, "55");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn while_auto_print_in_each_iteration() {
|
||||
let actual = nu!(
|
||||
cwd: ".",
|
||||
"mut total = 0; while $total < 2 { $total = $total + 1; echo 1 }"
|
||||
);
|
||||
// Note: nu! macro auto repalce "\n" and "\r\n" with ""
|
||||
// so our output will be `11`
|
||||
// that's ok, our main concern is it auto print value in each iteration.
|
||||
assert_eq!(actual.out, "11");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user