From 656e86a7ca8f28e45329ae0525e04bca942de36d Mon Sep 17 00:00:00 2001 From: Michael Angerman Date: Wed, 27 Oct 2021 07:19:33 -0700 Subject: [PATCH] got it working by turning it into a vec --- crates/nu-command/src/filters/last.rs | 77 ++++++++++++++++++--------- 1 file changed, 53 insertions(+), 24 deletions(-) diff --git a/crates/nu-command/src/filters/last.rs b/crates/nu-command/src/filters/last.rs index afc0c5ad28..e2b9eefe97 100644 --- a/crates/nu-command/src/filters/last.rs +++ b/crates/nu-command/src/filters/last.rs @@ -2,7 +2,7 @@ use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value}; +use nu_protocol::{IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape}; use std::convert::TryInto; #[derive(Clone)] @@ -33,33 +33,62 @@ impl Command for Last { input: PipelineData, ) -> Result { let rows: Option = call.opt(engine_state, stack, 0)?; - let iter_count: i64 = input.into_iter().count() as i64; - let beginning_rows_to_skip = rows_to_skip(iter_count, rows); - dbg!(beginning_rows_to_skip); + let v: Vec<_> = input.into_iter().collect(); + let vlen: i64 = v.len() as i64; + let beginning_rows_to_skip = rows_to_skip(vlen, rows); + /* + let end_rows_desired = if let Some(quantity) = rows { + quantity + } else { + 1 + }; + + let beginning_rows_to_skip = if end_rows_desired < v.len() { + v.len() - end_rows_desired + } else { + 0 + }; + */ + + let iter = v + .into_iter() + .skip(beginning_rows_to_skip.try_into().unwrap()); + + //let iter = v.into_iter().skip(beginning_rows_to_skip); + + /* + let iter_count: i64 = input.into_iter().count() as i64; + let beginning_rows_to_skip = rows_to_skip(iter_count, rows); + dbg!(beginning_rows_to_skip); + */ // let beginning_rows_to_skip = 3; - match input { - PipelineData::Stream(stream) => { - dbg!("Stream"); - Ok(stream - .skip(beginning_rows_to_skip.try_into().unwrap()) - .into_pipeline_data()) - } - PipelineData::Value(Value::List { vals, .. }) => { - dbg!("Value"); - Ok(vals - .into_iter() - .skip(beginning_rows_to_skip.try_into().unwrap()) - .into_pipeline_data()) - } - _ => { - dbg!("Fall to the bottom"); - Ok(PipelineData::Value(Value::Nothing { span: call.head })) - } - } + /* + match input { + PipelineData::Stream(stream) => { + dbg!("Stream"); + Ok(stream + .skip(beginning_rows_to_skip.try_into().unwrap()) + .into_pipeline_data()) + } + PipelineData::Value(Value::List { vals, .. }) => { + dbg!("Value"); + Ok(vals + .into_iter() + .skip(beginning_rows_to_skip.try_into().unwrap()) + .into_pipeline_data()) + } + _ => { + dbg!("Fall to the bottom"); + Ok(PipelineData::Value(Value::Nothing { span: call.head })) + } + } + */ - // Ok(PipelineData::Value(Value::Nothing { span: call.head })) + Ok(iter.into_pipeline_data()) + + // Ok(PipelineData::Value(Value::Nothing { span: call.head })) } }