mirror of
https://github.com/nushell/nushell
synced 2025-01-14 14:14:13 +00:00
move get_columns from the table_viewer to a central location (#628)
* get_columns is working in the columns command * the new location of the get_columns method is nu-protocol/src/column.rs * reference the new location of the get_columns method * move get_columns to nu-engine
This commit is contained in:
parent
44791b5835
commit
f734995170
4 changed files with 23 additions and 26 deletions
|
@ -1,3 +1,4 @@
|
||||||
|
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::{
|
use nu_protocol::{
|
||||||
|
@ -66,7 +67,7 @@ fn getcol(
|
||||||
},
|
},
|
||||||
..,
|
..,
|
||||||
) => {
|
) => {
|
||||||
let input_cols = get_input_cols(input_vals);
|
let input_cols = get_columns(&input_vals);
|
||||||
Ok(input_cols
|
Ok(input_cols
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(move |x| Value::String { val: x, span })
|
.map(move |x| Value::String { val: x, span })
|
||||||
|
@ -74,7 +75,7 @@ fn getcol(
|
||||||
}
|
}
|
||||||
PipelineData::ListStream(stream, ..) => {
|
PipelineData::ListStream(stream, ..) => {
|
||||||
let v: Vec<_> = stream.into_iter().collect();
|
let v: Vec<_> = stream.into_iter().collect();
|
||||||
let input_cols = get_input_cols(v);
|
let input_cols = get_columns(&v);
|
||||||
|
|
||||||
Ok(input_cols
|
Ok(input_cols
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -89,14 +90,6 @@ fn getcol(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_input_cols(input: Vec<Value>) -> Vec<String> {
|
|
||||||
let rec = input.first();
|
|
||||||
match rec {
|
|
||||||
Some(Value::Record { cols, vals: _, .. }) => cols.to_vec(),
|
|
||||||
_ => vec!["".to_string()],
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod test {
|
mod test {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
use lscolors::{LsColors, Style};
|
use lscolors::{LsColors, Style};
|
||||||
use nu_color_config::{get_color_config, style_primitive};
|
use nu_color_config::{get_color_config, style_primitive};
|
||||||
|
use nu_engine::column::get_columns;
|
||||||
use nu_engine::{env_to_string, CallExt};
|
use nu_engine::{env_to_string, CallExt};
|
||||||
use nu_protocol::ast::{Call, PathMember};
|
use nu_protocol::ast::{Call, PathMember};
|
||||||
use nu_protocol::engine::{Command, EngineState, Stack};
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
||||||
|
@ -244,22 +245,6 @@ impl Command for Table {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_columns(input: &[Value]) -> Vec<String> {
|
|
||||||
let mut columns = vec![];
|
|
||||||
|
|
||||||
for item in input {
|
|
||||||
if let Value::Record { cols, vals: _, .. } = item {
|
|
||||||
for col in cols {
|
|
||||||
if !columns.contains(col) {
|
|
||||||
columns.push(col.to_string());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
columns
|
|
||||||
}
|
|
||||||
|
|
||||||
fn convert_to_table(
|
fn convert_to_table(
|
||||||
row_offset: usize,
|
row_offset: usize,
|
||||||
input: &[Value],
|
input: &[Value],
|
||||||
|
|
17
crates/nu-engine/src/column.rs
Normal file
17
crates/nu-engine/src/column.rs
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
use nu_protocol::Value;
|
||||||
|
|
||||||
|
pub fn get_columns(input: &[Value]) -> Vec<String> {
|
||||||
|
let mut columns = vec![];
|
||||||
|
|
||||||
|
for item in input {
|
||||||
|
if let Value::Record { cols, vals: _, .. } = item {
|
||||||
|
for col in cols {
|
||||||
|
if !columns.contains(col) {
|
||||||
|
columns.push(col.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
columns
|
||||||
|
}
|
|
@ -1,9 +1,11 @@
|
||||||
mod call_ext;
|
mod call_ext;
|
||||||
|
pub mod column;
|
||||||
mod documentation;
|
mod documentation;
|
||||||
mod env;
|
mod env;
|
||||||
mod eval;
|
mod eval;
|
||||||
|
|
||||||
pub use call_ext::CallExt;
|
pub use call_ext::CallExt;
|
||||||
|
pub use column::get_columns;
|
||||||
pub use documentation::{generate_docs, get_brief_help, get_documentation, get_full_help};
|
pub use documentation::{generate_docs, get_brief_help, get_documentation, get_full_help};
|
||||||
pub use env::*;
|
pub use env::*;
|
||||||
pub use eval::{eval_block, eval_expression, eval_operator};
|
pub use eval::{eval_block, eval_expression, eval_operator};
|
||||||
|
|
Loading…
Reference in a new issue