From 4bb95a880f967df4ea3102a5d6a06dc4aea76d1d Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Fri, 1 Apr 2022 23:12:31 +1300 Subject: [PATCH] let a simple last be a single value (#5060) --- crates/nu-command/src/filters/last.rs | 46 +++++++++++++------ .../nu-command/tests/commands/path/split.rs | 1 - src/tests/test_table_operations.rs | 2 +- 3 files changed, 33 insertions(+), 16 deletions(-) diff --git a/crates/nu-command/src/filters/last.rs b/crates/nu-command/src/filters/last.rs index 2e4835c854..6642bd2c19 100644 --- a/crates/nu-command/src/filters/last.rs +++ b/crates/nu-command/src/filters/last.rs @@ -3,8 +3,8 @@ use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; use nu_protocol::{ - Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span, - SyntaxShape, Value, + Category, Example, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, + Signature, Span, SyntaxShape, Value, }; #[derive(Clone)] @@ -30,14 +30,21 @@ impl Command for Last { } fn examples(&self) -> Vec { - vec![Example { - example: "[1,2,3] | last 2", - description: "Get the last 2 items", - result: Some(Value::List { - vals: vec![Value::test_int(2), Value::test_int(3)], - span: Span::test_data(), - }), - }] + vec![ + Example { + example: "[1,2,3] | last 2", + description: "Get the last 2 items", + result: Some(Value::List { + vals: vec![Value::test_int(2), Value::test_int(3)], + span: Span::test_data(), + }), + }, + Example { + example: "[1,2,3] | last", + description: "Get the last item", + result: Some(Value::test_int(3)), + }, + ] } fn run( @@ -48,17 +55,28 @@ impl Command for Last { input: PipelineData, ) -> Result { let metadata = input.metadata(); + let span = call.head; let rows: Option = call.opt(engine_state, stack, 0)?; 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 iter = v.into_iter().skip(beginning_rows_to_skip as usize); + if rows.is_some() { + let iter = v.into_iter().skip(beginning_rows_to_skip as usize); - Ok(iter - .into_pipeline_data(engine_state.ctrlc.clone()) - .set_metadata(metadata)) + Ok(iter + .into_pipeline_data(engine_state.ctrlc.clone()) + .set_metadata(metadata)) + } else { + let last = v.into_iter().nth(beginning_rows_to_skip as usize); + + if let Some(last) = last { + Ok(last.into_pipeline_data().set_metadata(metadata)) + } else { + Ok(PipelineData::new(span).set_metadata(metadata)) + } + } } } diff --git a/crates/nu-command/tests/commands/path/split.rs b/crates/nu-command/tests/commands/path/split.rs index 930c0a3783..ffd51cd434 100644 --- a/crates/nu-command/tests/commands/path/split.rs +++ b/crates/nu-command/tests/commands/path/split.rs @@ -20,7 +20,6 @@ fn splits_correctly_single_path() { 'home/viking/spam.txt' | path split | last - | get 0 "# )); diff --git a/src/tests/test_table_operations.rs b/src/tests/test_table_operations.rs index 7eff61068e..bde2e86dbf 100644 --- a/src/tests/test_table_operations.rs +++ b/src/tests/test_table_operations.rs @@ -44,7 +44,7 @@ fn flatten_table_get() -> TestResult { #[test] fn flatten_table_column_get_last() -> TestResult { run_test( - "[[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions | last | get versions.0", + "[[origin, crate, versions]; [World, ([[name]; ['nu-cli']]), ['0.21', '0.22']]] | flatten versions | last | get versions", "0.22", ) }