2022-01-12 05:06:56 +01:00
|
|
|
use nu_engine::{eval_block, eval_expression, CallExt};
|
2021-10-09 18:14:02 +02:00
|
|
|
use nu_protocol::ast::Call;
|
2022-01-12 05:06:56 +01:00
|
|
|
use nu_protocol::engine::{CaptureBlock, Command, EngineState, Stack};
|
2021-10-28 06:13:10 +02:00
|
|
|
use nu_protocol::{
|
2021-11-17 05:22:37 +01:00
|
|
|
Category, Example, IntoInterruptiblePipelineData, PipelineData, Signature, Span, SyntaxShape,
|
|
|
|
Value,
|
2021-10-28 06:13:10 +02:00
|
|
|
};
|
2021-10-09 18:14:02 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Clone)]
|
2021-10-09 18:14:02 +02:00
|
|
|
pub struct For;
|
|
|
|
|
|
|
|
impl Command for For {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"for"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Loop over a range"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
|
|
Signature::build("for")
|
|
|
|
.required(
|
|
|
|
"var_name",
|
|
|
|
SyntaxShape::VarWithOptType,
|
|
|
|
"name of the looping variable",
|
|
|
|
)
|
|
|
|
.required(
|
|
|
|
"range",
|
2021-10-09 18:58:33 +02:00
|
|
|
SyntaxShape::Keyword(b"in".to_vec(), Box::new(SyntaxShape::Any)),
|
2021-10-09 18:14:02 +02:00
|
|
|
"range of the loop",
|
|
|
|
)
|
|
|
|
.required(
|
|
|
|
"block",
|
|
|
|
SyntaxShape::Block(Some(vec![])),
|
|
|
|
"the block to run",
|
|
|
|
)
|
2021-12-23 22:41:29 +01:00
|
|
|
.switch(
|
|
|
|
"numbered",
|
|
|
|
"returned a numbered item ($it.index and $it.item)",
|
|
|
|
Some('n'),
|
|
|
|
)
|
2021-10-09 18:14:02 +02:00
|
|
|
.creates_scope()
|
2021-11-17 05:22:37 +01:00
|
|
|
.category(Category::Core)
|
2021-10-09 18:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-09 18:14:02 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
2021-11-06 06:50:33 +01:00
|
|
|
let head = call.head;
|
2021-10-09 18:14:02 +02:00
|
|
|
let var_id = call.positional[0]
|
|
|
|
.as_var()
|
|
|
|
.expect("internal error: missing variable");
|
|
|
|
|
|
|
|
let keyword_expr = call.positional[1]
|
|
|
|
.as_keyword()
|
|
|
|
.expect("internal error: missing keyword");
|
2021-10-25 08:31:39 +02:00
|
|
|
let values = eval_expression(engine_state, stack, keyword_expr)?;
|
2021-10-09 18:14:02 +02:00
|
|
|
|
2022-01-12 05:06:56 +01:00
|
|
|
let capture_block: CaptureBlock = call.req(engine_state, stack, 2)?;
|
2021-10-09 18:58:33 +02:00
|
|
|
|
2021-12-23 22:41:29 +01:00
|
|
|
let numbered = call.has_flag("numbered");
|
|
|
|
|
2021-10-28 06:13:10 +02:00
|
|
|
let ctrlc = engine_state.ctrlc.clone();
|
2021-10-25 08:31:39 +02:00
|
|
|
let engine_state = engine_state.clone();
|
2022-01-12 05:06:56 +01:00
|
|
|
let block = engine_state.get_block(capture_block.block_id).clone();
|
|
|
|
let mut stack = stack.captures_to_stack(&capture_block.captures);
|
2022-01-04 23:30:34 +01:00
|
|
|
let orig_env_vars = stack.env_vars.clone();
|
|
|
|
let orig_env_hidden = stack.env_hidden.clone();
|
2021-10-09 18:14:02 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
match values {
|
2021-10-25 23:14:21 +02:00
|
|
|
Value::List { vals, .. } => Ok(vals
|
2021-10-25 06:01:02 +02:00
|
|
|
.into_iter()
|
2021-12-23 22:41:29 +01:00
|
|
|
.enumerate()
|
|
|
|
.map(move |(idx, x)| {
|
2022-01-04 23:30:34 +01:00
|
|
|
stack.with_env(&orig_env_vars, &orig_env_hidden);
|
|
|
|
|
2021-12-23 22:41:29 +01:00
|
|
|
stack.add_var(
|
|
|
|
var_id,
|
|
|
|
if numbered {
|
|
|
|
Value::Record {
|
|
|
|
cols: vec!["index".into(), "item".into()],
|
|
|
|
vals: vec![
|
|
|
|
Value::Int {
|
|
|
|
val: idx as i64,
|
|
|
|
span: head,
|
|
|
|
},
|
|
|
|
x,
|
|
|
|
],
|
|
|
|
span: head,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
},
|
|
|
|
);
|
2021-10-09 18:58:33 +02:00
|
|
|
|
2021-10-26 05:22:37 +02:00
|
|
|
//let block = engine_state.get_block(block_id);
|
2021-11-06 06:50:33 +01:00
|
|
|
match eval_block(&engine_state, &mut stack, &block, PipelineData::new(head)) {
|
|
|
|
Ok(pipeline_data) => pipeline_data.into_value(head),
|
2021-10-25 06:01:02 +02:00
|
|
|
Err(error) => Value::Error { error },
|
|
|
|
}
|
|
|
|
})
|
2021-10-28 06:13:10 +02:00
|
|
|
.into_pipeline_data(ctrlc)),
|
2021-10-25 23:14:21 +02:00
|
|
|
Value::Range { val, .. } => Ok(val
|
2021-10-25 06:24:10 +02:00
|
|
|
.into_range_iter()?
|
2021-12-23 22:41:29 +01:00
|
|
|
.enumerate()
|
|
|
|
.map(move |(idx, x)| {
|
2022-01-04 23:30:34 +01:00
|
|
|
stack.with_env(&orig_env_vars, &orig_env_hidden);
|
|
|
|
|
2021-12-23 22:41:29 +01:00
|
|
|
stack.add_var(
|
|
|
|
var_id,
|
|
|
|
if numbered {
|
|
|
|
Value::Record {
|
|
|
|
cols: vec!["index".into(), "item".into()],
|
|
|
|
vals: vec![
|
|
|
|
Value::Int {
|
|
|
|
val: idx as i64,
|
|
|
|
span: head,
|
|
|
|
},
|
|
|
|
x,
|
|
|
|
],
|
|
|
|
span: head,
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
x
|
|
|
|
},
|
|
|
|
);
|
2021-10-25 06:24:10 +02:00
|
|
|
|
2021-10-26 05:22:37 +02:00
|
|
|
//let block = engine_state.get_block(block_id);
|
2021-11-06 06:50:33 +01:00
|
|
|
match eval_block(&engine_state, &mut stack, &block, PipelineData::new(head)) {
|
|
|
|
Ok(pipeline_data) => pipeline_data.into_value(head),
|
2021-10-25 06:24:10 +02:00
|
|
|
Err(error) => Value::Error { error },
|
|
|
|
}
|
|
|
|
})
|
2021-10-28 06:13:10 +02:00
|
|
|
.into_pipeline_data(ctrlc)),
|
2021-10-25 06:24:10 +02:00
|
|
|
x => {
|
2021-10-25 08:31:39 +02:00
|
|
|
stack.add_var(var_id, x);
|
2021-10-25 06:24:10 +02:00
|
|
|
|
2021-11-06 06:50:33 +01:00
|
|
|
eval_block(&engine_state, &mut stack, &block, PipelineData::new(head))
|
2021-10-25 06:24:10 +02:00
|
|
|
}
|
2021-10-25 06:01:02 +02:00
|
|
|
}
|
2021-10-09 18:14:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
2021-12-19 08:46:13 +01:00
|
|
|
let span = Span::test_data();
|
2021-10-09 18:14:02 +02:00
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Echo the square of each integer",
|
|
|
|
example: "for x in [1 2 3] { $x * $x }",
|
2021-10-09 15:10:10 +02:00
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::Int { val: 1, span },
|
|
|
|
Value::Int { val: 4, span },
|
|
|
|
Value::Int { val: 9, span },
|
|
|
|
],
|
2021-12-19 08:46:13 +01:00
|
|
|
span,
|
2021-10-09 15:10:10 +02:00
|
|
|
}),
|
2021-10-09 18:14:02 +02:00
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Work with elements of a range",
|
|
|
|
example: "for $x in 1..3 { $x }",
|
2021-10-09 15:10:10 +02:00
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::Int { val: 1, span },
|
|
|
|
Value::Int { val: 2, span },
|
|
|
|
Value::Int { val: 3, span },
|
|
|
|
],
|
2021-12-19 08:46:13 +01:00
|
|
|
span,
|
2021-10-09 15:10:10 +02:00
|
|
|
}),
|
2021-10-09 18:14:02 +02:00
|
|
|
},
|
2021-12-24 01:16:50 +01:00
|
|
|
Example {
|
|
|
|
description: "Number each item and echo a message",
|
|
|
|
example: "for $it in ['bob' 'fred'] --numbered { $\"($it.index) is ($it.item)\" }",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::String {
|
|
|
|
val: "0 is bob".into(),
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
Value::String {
|
|
|
|
val: "1 is fred".into(),
|
|
|
|
span,
|
|
|
|
},
|
|
|
|
],
|
|
|
|
span,
|
|
|
|
}),
|
|
|
|
},
|
2021-10-09 18:14:02 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
2021-10-09 18:58:33 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(For {})
|
|
|
|
}
|
|
|
|
}
|