mirror of
https://github.com/nushell/nushell.git
synced 2025-02-19 12:01:42 +01:00
Fix first command to display the first item not as a table
This commit is contained in:
parent
e1ea0d42a9
commit
6e49d0f84b
@ -1,7 +1,9 @@
|
|||||||
use nu_engine::CallExt;
|
use nu_engine::CallExt;
|
||||||
use nu_protocol::ast::Call;
|
use nu_protocol::ast::Call;
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
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)]
|
#[derive(Clone)]
|
||||||
pub struct First;
|
pub struct First;
|
||||||
@ -61,17 +63,26 @@ fn first_helper(
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
||||||
|
let head = call.head;
|
||||||
let rows: Option<i64> = call.opt(engine_state, stack, 0)?;
|
let rows: Option<i64> = call.opt(engine_state, stack, 0)?;
|
||||||
let rows_desired: usize = match rows {
|
let rows_desired: usize = match rows {
|
||||||
Some(x) => x as usize,
|
Some(x) => x as usize,
|
||||||
None => 1,
|
None => 1,
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok(Value::List {
|
if rows_desired == 1 {
|
||||||
vals: input.into_iter().take(rows_desired).collect(),
|
let mut input_peek = input.into_iter().peekable();
|
||||||
span: call.head,
|
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)]
|
#[cfg(test)]
|
||||||
|
Loading…
Reference in New Issue
Block a user