Hi there,

The case which was presented must be addressed.

But I did not test it properly...
I'd encourage you to do so.

Take care.

---------

Signed-off-by: Maxim Zhiburt <zhiburt@gmail.com>
This commit is contained in:
Maxim Zhiburt 2023-02-08 05:01:31 +03:00 committed by GitHub
parent a7fdca05c6
commit 6e6ef862c5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 118 additions and 25 deletions

View File

@ -1333,24 +1333,31 @@ fn convert_to_table2_entry(
return value_to_styled_string(item, config, style_computer); return value_to_styled_string(item, config, style_computer);
} }
let table = match &item { match &item {
Value::Record { span, cols, vals } => { Value::Record { span, cols, vals } => {
if cols.is_empty() && vals.is_empty() { if cols.is_empty() && vals.is_empty() {
return value_to_styled_string(item, config, style_computer); return value_to_styled_string(item, config, style_computer);
} }
convert_to_table2( // we verify what is the structure of a Record cause it might represent
0,
std::iter::once(item), let table = build_expanded_table(
cols.clone(),
vals.clone(),
*span,
ctrlc.clone(), ctrlc.clone(),
config, config,
*span,
style_computer, style_computer,
width,
deep.map(|i| i - 1), deep.map(|i| i - 1),
flatten, flatten,
flatten_sep, flatten_sep,
width, );
)
match table {
Ok(Some(table)) => (table, TextStyle::default()),
_ => value_to_styled_string(item, config, style_computer),
}
} }
Value::List { vals, span } => { Value::List { vals, span } => {
if flatten { if flatten {
@ -1363,7 +1370,7 @@ fn convert_to_table2_entry(
} }
} }
convert_to_table2( let table = convert_to_table2(
0, 0,
vals.iter(), vals.iter(),
ctrlc.clone(), ctrlc.clone(),
@ -1374,10 +1381,7 @@ fn convert_to_table2_entry(
flatten, flatten,
flatten_sep, flatten_sep,
width, width,
) );
}
_ => return value_to_styled_string(item, config, style_computer), // unknown type.
};
let (table, whead, windex) = match table { let (table, whead, windex) = match table {
Ok(Some(out)) => out, Ok(Some(out)) => out,
@ -1394,6 +1398,9 @@ fn convert_to_table2_entry(
None => value_to_styled_string(item, config, style_computer), None => value_to_styled_string(item, config, style_computer),
} }
} }
_ => value_to_styled_string(item, config, style_computer), // unknown type.
}
}
fn convert_value_list_to_string( fn convert_value_list_to_string(
vals: &[Value], vals: &[Value],

View File

@ -180,6 +180,92 @@ fn table_expand_flatten_and_deep_1() {
); );
} }
#[test]
fn table_expand_record_0() {
let actual = nu!(r#"[{c: {d: 1}}] | table --expand"#);
assert_eq!(
actual.out,
"╭───┬───────────╮\
# c \
\
0 \
d 1 \
\
"
);
}
#[test]
fn table_expand_record_1() {
let actual =
nu!(r#"[[a b, c]; [1 2 3] [4 5 [1 2 {a: 123, b: 234, c: 345}]]] | table --expand"#);
assert_eq!(
actual.out,
"╭───┬───┬───┬─────────────────────╮\
# a b c \
\
0 1 2 3 \
1 4 5 \
0 1 \
1 2 \
2 \
a 123 \
b 234 \
c 345 \
\
\
"
);
}
#[test]
fn table_expand_record_2() {
let structure = "{\
field1: [ a, b, c ],\
field2: [ 123, 234, 345 ],\
field3: [ [ head1, head2, head3 ]; [ 1 2 3 ] [ 79 79 79 ] [ { f1: 'a string', f2: 1000 }, 1, 2 ] ],\
field4: { f1: 1, f2: 3, f3: { f1: f1, f2: f2, f3: f3 } }\
}";
let actual = nu!(format!("{} | table --expand", structure));
assert_eq!(
actual.out,
"╭────────┬───────────────────────────────────────────╮\
\
field1 0 a \
1 b \
2 c \
\
\
field2 0 123 \
1 234 \
2 345 \
\
\
field3 # head1 head2 head3 \
\
0 1 2 3 \
1 79 79 79 \
2 1 2 \
f1 a string \
f2 1000 \
\
\
\
field4 f1 1 \
f2 3 \
\
f3 f1 f1 \
f2 f2 \
f3 f3 \
\
\
"
);
}
#[test] #[test]
#[cfg(not(windows))] #[cfg(not(windows))]
fn external_with_too_much_stdout_should_not_hang_nu() { fn external_with_too_much_stdout_should_not_hang_nu() {