2022-01-16 13:52:41 +00:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
|
|
|
engine::{Command, EngineState, Stack},
|
|
|
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, Spanned,
|
2022-11-09 21:55:05 +00:00
|
|
|
SyntaxShape, Type, Value,
|
2022-01-16 13:52:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Seq;
|
|
|
|
|
|
|
|
impl Command for Seq {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"seq"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("seq")
|
2022-11-09 23:24:57 +00:00
|
|
|
.input_output_types(vec![(Type::Nothing, Type::List(Box::new(Type::Number)))])
|
2022-01-16 13:52:41 +00:00
|
|
|
.rest("rest", SyntaxShape::Number, "sequence values")
|
|
|
|
.category(Category::Generators)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2022-11-09 22:19:02 +00:00
|
|
|
"Output sequences of numbers."
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
seq(engine_state, stack, call)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
2022-11-09 22:19:02 +00:00
|
|
|
description: "sequence 1 to 10",
|
2022-01-16 13:52:41 +00:00
|
|
|
example: "seq 1 10",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_int(1),
|
|
|
|
Value::test_int(2),
|
|
|
|
Value::test_int(3),
|
|
|
|
Value::test_int(4),
|
|
|
|
Value::test_int(5),
|
|
|
|
Value::test_int(6),
|
|
|
|
Value::test_int(7),
|
|
|
|
Value::test_int(8),
|
|
|
|
Value::test_int(9),
|
|
|
|
Value::test_int(10),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
2022-11-09 22:19:02 +00:00
|
|
|
description: "sequence 1.0 to 2.0 by 0.1s",
|
2022-01-16 13:52:41 +00:00
|
|
|
example: "seq 1.0 0.1 2.0",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_float(1.0000),
|
|
|
|
Value::test_float(1.1000),
|
|
|
|
Value::test_float(1.2000),
|
|
|
|
Value::test_float(1.3000),
|
|
|
|
Value::test_float(1.4000),
|
|
|
|
Value::test_float(1.5000),
|
|
|
|
Value::test_float(1.6000),
|
|
|
|
Value::test_float(1.7000),
|
|
|
|
Value::test_float(1.8000),
|
|
|
|
Value::test_float(1.9000),
|
|
|
|
Value::test_float(2.0000),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
2022-11-09 22:19:02 +00:00
|
|
|
description: "sequence 1 to 5, then convert to a string with a pipe separator",
|
|
|
|
example: "seq 1 5 | str join '|'",
|
2022-11-09 23:24:57 +00:00
|
|
|
result: None,
|
2022-01-16 13:52:41 +00:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn seq(
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let span = call.head;
|
|
|
|
let rest_nums: Vec<Spanned<f64>> = call.rest(engine_state, stack, 0)?;
|
|
|
|
|
|
|
|
if rest_nums.is_empty() {
|
2022-04-18 12:34:10 +00:00
|
|
|
return Err(ShellError::GenericError(
|
2022-01-16 13:52:41 +00:00
|
|
|
"seq requires some parameters".into(),
|
|
|
|
"needs parameter".into(),
|
2022-04-18 12:34:10 +00:00
|
|
|
Some(call.head),
|
|
|
|
None,
|
|
|
|
Vec::new(),
|
2022-01-16 13:52:41 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
let rest_nums: Vec<f64> = rest_nums.iter().map(|n| n.item).collect();
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
run_seq(rest_nums, span)
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(Seq {})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
pub fn run_seq(free: Vec<f64>, span: Span) -> Result<PipelineData, ShellError> {
|
|
|
|
let first = free[0];
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
let step: f64 = if free.len() > 2 { free[1] } else { 1.0 };
|
|
|
|
let last = { free[free.len() - 1] };
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
Ok(print_seq(first, step, last, span))
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn done_printing(next: f64, step: f64, last: f64) -> bool {
|
|
|
|
if step >= 0f64 {
|
|
|
|
next > last
|
|
|
|
} else {
|
|
|
|
next < last
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
fn print_seq(first: f64, step: f64, last: f64, span: Span) -> PipelineData {
|
2022-01-16 13:52:41 +00:00
|
|
|
let mut i = 0isize;
|
|
|
|
let mut value = first + i as f64 * step;
|
|
|
|
let mut ret_num = vec![];
|
|
|
|
|
|
|
|
while !done_printing(value, step, last) {
|
2022-11-09 22:19:02 +00:00
|
|
|
ret_num.push(value);
|
2022-01-16 13:52:41 +00:00
|
|
|
i += 1;
|
|
|
|
value = first + i as f64 * step;
|
|
|
|
}
|
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
// we'd like to keep the datatype the same for the output, so check
|
|
|
|
// and see if any of the output contains values after the decimal point,
|
|
|
|
// and if so we'll make the entire output floats
|
|
|
|
let contains_decimals = vec_contains_decimals(&ret_num);
|
|
|
|
let rows: Vec<Value> = ret_num
|
|
|
|
.iter()
|
|
|
|
.map(|v| {
|
|
|
|
if contains_decimals {
|
|
|
|
Value::float(*v, span)
|
|
|
|
} else {
|
|
|
|
Value::int(*v as i64, span)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2022-01-16 13:52:41 +00:00
|
|
|
|
2022-11-09 22:19:02 +00:00
|
|
|
Value::List { vals: rows, span }.into_pipeline_data()
|
2022-01-16 13:52:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn vec_contains_decimals(array: &[f64]) -> bool {
|
|
|
|
let mut found_decimal = false;
|
|
|
|
for x in array {
|
|
|
|
if x.fract() != 0.0 {
|
|
|
|
found_decimal = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
found_decimal
|
|
|
|
}
|