mirror of
https://github.com/nushell/nushell.git
synced 2025-01-28 17:18:59 +01:00
port over from nushell the column flag for the length command (#617)
* port over from nushell the column flag for the length command * fix clippy error * refactor with the get_columns now centrally located
This commit is contained in:
parent
f734995170
commit
5d58f68c59
@ -1,6 +1,10 @@
|
|||||||
|
use nu_engine::column::get_columns;
|
||||||
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::{Category, IntoPipelineData, PipelineData, Signature, Value};
|
use nu_protocol::{
|
||||||
|
Category, IntoInterruptiblePipelineData, IntoPipelineData, PipelineData, ShellError, Signature,
|
||||||
|
Span, Value,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub struct Length;
|
pub struct Length;
|
||||||
@ -15,27 +19,87 @@ impl Command for Length {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("length").category(Category::Filters)
|
Signature::build("length")
|
||||||
|
.switch("column", "Show the number of columns in a table", Some('c'))
|
||||||
|
.category(Category::Filters)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
&self,
|
&self,
|
||||||
_engine_state: &EngineState,
|
engine_state: &EngineState,
|
||||||
_stack: &mut Stack,
|
_stack: &mut Stack,
|
||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
) -> Result<nu_protocol::PipelineData, ShellError> {
|
||||||
match input {
|
let col = call.has_flag("column");
|
||||||
PipelineData::Value(Value::Nothing { .. }, ..) => Ok(Value::Int {
|
if col {
|
||||||
val: 0,
|
length_col(engine_state, call, input)
|
||||||
span: call.head,
|
} else {
|
||||||
}
|
length_row(call, input)
|
||||||
.into_pipeline_data()),
|
}
|
||||||
_ => Ok(Value::Int {
|
}
|
||||||
val: input.into_iter().count() as i64,
|
}
|
||||||
span: call.head,
|
|
||||||
}
|
// this simulates calling input | columns | length
|
||||||
.into_pipeline_data()),
|
fn length_col(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
call: &Call,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
length_row(
|
||||||
|
call,
|
||||||
|
getcol(engine_state, call.head, input)
|
||||||
|
.expect("getcol() should not fail used in column command"),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn length_row(call: &Call, input: PipelineData) -> Result<PipelineData, ShellError> {
|
||||||
|
match input {
|
||||||
|
PipelineData::Value(Value::Nothing { .. }, ..) => Ok(Value::Int {
|
||||||
|
val: 0,
|
||||||
|
span: call.head,
|
||||||
|
}
|
||||||
|
.into_pipeline_data()),
|
||||||
|
_ => Ok(Value::Int {
|
||||||
|
val: input.into_iter().count() as i64,
|
||||||
|
span: call.head,
|
||||||
|
}
|
||||||
|
.into_pipeline_data()),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn getcol(
|
||||||
|
engine_state: &EngineState,
|
||||||
|
span: Span,
|
||||||
|
input: PipelineData,
|
||||||
|
) -> Result<PipelineData, ShellError> {
|
||||||
|
match input {
|
||||||
|
PipelineData::Value(
|
||||||
|
Value::List {
|
||||||
|
vals: input_vals,
|
||||||
|
span,
|
||||||
|
},
|
||||||
|
..,
|
||||||
|
) => {
|
||||||
|
let input_cols = get_columns(&input_vals);
|
||||||
|
Ok(input_cols
|
||||||
|
.into_iter()
|
||||||
|
.map(move |x| Value::String { val: x, span })
|
||||||
|
.into_pipeline_data(engine_state.ctrlc.clone()))
|
||||||
|
}
|
||||||
|
PipelineData::ListStream(stream, ..) => {
|
||||||
|
let v: Vec<_> = stream.into_iter().collect();
|
||||||
|
let input_cols = get_columns(&v);
|
||||||
|
|
||||||
|
Ok(input_cols
|
||||||
|
.into_iter()
|
||||||
|
.map(move |x| Value::String { val: x, span })
|
||||||
|
.into_pipeline_data(engine_state.ctrlc.clone()))
|
||||||
|
}
|
||||||
|
PipelineData::Value(..) | PipelineData::StringStream(..) | PipelineData::ByteStream(..) => {
|
||||||
|
let cols = vec![];
|
||||||
|
let vals = vec![];
|
||||||
|
Ok(Value::Record { cols, vals, span }.into_pipeline_data())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -197,3 +197,16 @@ fn select() -> TestResult {
|
|||||||
fn update_will_insert() -> TestResult {
|
fn update_will_insert() -> TestResult {
|
||||||
run_test(r#"{} | update a b | get a"#, "b")
|
run_test(r#"{} | update a b | get a"#, "b")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn length_for_columns() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
r#"[[name,age,grade]; [bill,20,a] [a b c]] | length -c"#,
|
||||||
|
"3",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn length_for_rows() -> TestResult {
|
||||||
|
run_test(r#"[[name,age,grade]; [bill,20,a] [a b c]] | length"#, "2")
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user