From 6e49d0f84bd0495f1be3073a6c836aa47be523e3 Mon Sep 17 00:00:00 2001 From: Stefan Stanciulescu Date: Tue, 2 Nov 2021 09:05:03 +0100 Subject: [PATCH] Fix first command to display the first item not as a table --- crates/nu-command/src/filters/first.rs | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/crates/nu-command/src/filters/first.rs b/crates/nu-command/src/filters/first.rs index 68e121adb2..10b778068c 100644 --- a/crates/nu-command/src/filters/first.rs +++ b/crates/nu-command/src/filters/first.rs @@ -1,7 +1,9 @@ use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Example, IntoPipelineData, PipelineData, Signature, Span, SyntaxShape, Value}; +use nu_protocol::{ + Example, IntoPipelineData, PipelineData, ShellError, Signature, Span, SyntaxShape, Value, +}; #[derive(Clone)] pub struct First; @@ -61,17 +63,26 @@ fn first_helper( call: &Call, input: PipelineData, ) -> Result { + let head = call.head; let rows: Option = call.opt(engine_state, stack, 0)?; let rows_desired: usize = match rows { Some(x) => x as usize, None => 1, }; - Ok(Value::List { - vals: input.into_iter().take(rows_desired).collect(), - span: call.head, + if rows_desired == 1 { + let mut input_peek = input.into_iter().peekable(); + match input_peek.next() { + Some(val) => Ok(val.into_pipeline_data()), + None => Err(ShellError::AccessBeyondEndOfStream(head)), + } + } else { + Ok(Value::List { + vals: input.into_iter().take(rows_desired).collect(), + span: head, + } + .into_pipeline_data()) } - .into_pipeline_data()) } #[cfg(test)]