mirror of
https://github.com/nushell/nushell
synced 2024-12-28 22:13:10 +00:00
Flatten should flatten embedded table (#534)
This commit is contained in:
parent
caf73c36f2
commit
152467a858
4 changed files with 52 additions and 14 deletions
|
@ -84,12 +84,12 @@ enum TableInside<'a> {
|
||||||
Entries(&'a str, &'a Span, Vec<&'a Value>),
|
Entries(&'a str, &'a Span, Vec<&'a Value>),
|
||||||
}
|
}
|
||||||
|
|
||||||
fn is_table(value: &Value) -> bool {
|
// fn is_table(value: &Value) -> bool {
|
||||||
match value {
|
// match value {
|
||||||
Value::List { vals, span: _ } => vals.iter().all(|f| f.as_record().is_ok()),
|
// Value::List { vals, span: _ } => vals.iter().all(|f| f.as_record().is_ok()),
|
||||||
_ => false,
|
// _ => false,
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value> {
|
fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value> {
|
||||||
let tag = match item.span() {
|
let tag = match item.span() {
|
||||||
|
@ -233,7 +233,7 @@ fn flat_value(columns: &[CellPath], item: &Value, _name_tag: Span) -> Vec<Value>
|
||||||
expanded.push(r);
|
expanded.push(r);
|
||||||
}
|
}
|
||||||
expanded
|
expanded
|
||||||
} else if !is_table(item) {
|
} else if item.as_list().is_ok() {
|
||||||
if let Value::List { vals, span: _ } = item {
|
if let Value::List { vals, span: _ } = item {
|
||||||
vals.to_vec()
|
vals.to_vec()
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -195,6 +195,17 @@ impl Value {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn as_list(&self) -> Result<&[Value], ShellError> {
|
||||||
|
match self {
|
||||||
|
Value::List { vals, .. } => Ok(vals),
|
||||||
|
x => Err(ShellError::CantConvert(
|
||||||
|
"list".into(),
|
||||||
|
x.get_type().to_string(),
|
||||||
|
self.span()?,
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_bool(&self) -> Result<bool, ShellError> {
|
pub fn as_bool(&self) -> Result<bool, ShellError> {
|
||||||
match self {
|
match self {
|
||||||
Value::Bool { val, .. } => Ok(*val),
|
Value::Bool { val, .. } => Ok(*val),
|
||||||
|
|
21
src/main.rs
21
src/main.rs
|
@ -15,7 +15,10 @@ use nu_protocol::{
|
||||||
engine::{EngineState, Stack, StateWorkingSet},
|
engine::{EngineState, Stack, StateWorkingSet},
|
||||||
Config, PipelineData, ShellError, Span, Value, CONFIG_VARIABLE_ID,
|
Config, PipelineData, ShellError, Span, Value, CONFIG_VARIABLE_ID,
|
||||||
};
|
};
|
||||||
use reedline::{Completer, CompletionActionHandler, DefaultPrompt, LineBuffer, Prompt};
|
use reedline::{
|
||||||
|
Completer, CompletionActionHandler, DefaultCompleter, DefaultHinter, DefaultPrompt, LineBuffer,
|
||||||
|
Prompt,
|
||||||
|
};
|
||||||
use std::{
|
use std::{
|
||||||
io::Write,
|
io::Write,
|
||||||
sync::{
|
sync::{
|
||||||
|
@ -353,7 +356,20 @@ fn main() -> Result<()> {
|
||||||
// turn off the hinter but I don't see any way to do that yet.
|
// turn off the hinter but I don't see any way to do that yet.
|
||||||
|
|
||||||
let mut line_editor = if let Some(history_path) = history_path.clone() {
|
let mut line_editor = if let Some(history_path) = history_path.clone() {
|
||||||
|
let history = std::fs::read_to_string(&history_path);
|
||||||
|
if let Ok(history) = history {
|
||||||
|
let history_lines = history.lines().map(|x| x.to_string()).collect::<Vec<_>>();
|
||||||
line_editor
|
line_editor
|
||||||
|
.with_hinter(Box::new(
|
||||||
|
DefaultHinter::default()
|
||||||
|
.with_completer(Box::new(DefaultCompleter::new(history_lines))) // or .with_history()
|
||||||
|
// .with_inside_line()
|
||||||
|
.with_style(
|
||||||
|
nu_ansi_term::Style::new()
|
||||||
|
.italic()
|
||||||
|
.fg(nu_ansi_term::Color::LightGray),
|
||||||
|
),
|
||||||
|
))
|
||||||
.with_history(Box::new(
|
.with_history(Box::new(
|
||||||
FileBackedHistory::with_file(1000, history_path.clone())
|
FileBackedHistory::with_file(1000, history_path.clone())
|
||||||
.into_diagnostic()?,
|
.into_diagnostic()?,
|
||||||
|
@ -361,6 +377,9 @@ fn main() -> Result<()> {
|
||||||
.into_diagnostic()?
|
.into_diagnostic()?
|
||||||
} else {
|
} else {
|
||||||
line_editor
|
line_editor
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
line_editor
|
||||||
};
|
};
|
||||||
|
|
||||||
let prompt = update_prompt(
|
let prompt = update_prompt(
|
||||||
|
|
|
@ -1308,3 +1308,11 @@ fn allow_missing_optional_params() -> TestResult {
|
||||||
"5",
|
"5",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn flatten_should_flatten_inner_table() -> TestResult {
|
||||||
|
run_test(
|
||||||
|
"[[[name, value]; [abc, 123]]] | flatten | get value.0",
|
||||||
|
"123",
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue