forked from extern/nushell
Fix 90 degrees tables problem (#4043)
* fix 90 degrees tables problem * linting * clippy * linting
This commit is contained in:
parent
d6df367c6b
commit
32581497ef
@ -99,26 +99,21 @@ fn retrieve_table(mut table: Table, columns: &Value) -> Vec<Value> {
|
||||
cols.push(x.convert_to_string());
|
||||
}
|
||||
}
|
||||
// since cols was empty and headers is not, it means that headers were manually populated
|
||||
// so let's fake the data in order to build a proper table. this situation happens when
|
||||
// there are tables where the first column is actually the headers. kind of like a table
|
||||
// that has been rotated ccw 90 degrees
|
||||
|
||||
if cols.is_empty() && !table.headers().is_empty() {
|
||||
for col in table.headers().keys() {
|
||||
cols.push(col.to_string());
|
||||
}
|
||||
|
||||
let mut data2 = Vec::new();
|
||||
for x in &table.data {
|
||||
data2.push(x.join(", "));
|
||||
}
|
||||
// eprintln!("data2={:?}", data2);
|
||||
table.data = vec![data2];
|
||||
}
|
||||
|
||||
let mut table_out = Vec::new();
|
||||
// sometimes there are tables where the first column is the headers, kind of like
|
||||
// a table has ben rotated ccw 90 degrees, in these cases all columns will be missing
|
||||
// we keep track of this with this variable so we can deal with it later
|
||||
let mut at_least_one_row_filled = false;
|
||||
// if columns are still empty, let's just make a single column table with the data
|
||||
if cols.is_empty() {
|
||||
at_least_one_row_filled = true;
|
||||
let table_with_no_empties: Vec<_> = table.iter().filter(|item| !item.is_empty()).collect();
|
||||
|
||||
for row in &table_with_no_empties {
|
||||
@ -138,12 +133,15 @@ fn retrieve_table(mut table: Table, columns: &Value) -> Vec<Value> {
|
||||
let mut dict = TaggedDictBuilder::new(Tag::unknown());
|
||||
// eprintln!("row={:?}", &row);
|
||||
for col in &cols {
|
||||
// eprintln!("col={:?}", &col);
|
||||
//eprintln!("col={:?}", &col);
|
||||
let key = col.to_string();
|
||||
let val = row
|
||||
.get(col)
|
||||
.unwrap_or(&format!("Missing column: '{}'", &col))
|
||||
.to_string();
|
||||
if !at_least_one_row_filled && val != format!("Missing column: '{}'", &col) {
|
||||
at_least_one_row_filled = true;
|
||||
}
|
||||
dict.insert_value(
|
||||
key,
|
||||
UntaggedValue::Primitive(Primitive::String(val)).into_value(Tag::unknown()),
|
||||
@ -152,6 +150,14 @@ fn retrieve_table(mut table: Table, columns: &Value) -> Vec<Value> {
|
||||
table_out.push(dict.into_value());
|
||||
}
|
||||
}
|
||||
if !at_least_one_row_filled {
|
||||
let mut data2 = Vec::new();
|
||||
for x in &table.data {
|
||||
data2.push(x.join(", "));
|
||||
}
|
||||
table.data = vec![data2];
|
||||
return retrieve_table(table, columns);
|
||||
}
|
||||
table_out
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user