Merge pull request #68 from nushell/list_table2

improve table for lists
This commit is contained in:
JT 2021-09-26 07:37:49 +13:00 committed by GitHub
commit 1015ea814c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -65,30 +65,35 @@ fn convert_to_table(iter: impl IntoIterator<Item = Value>) -> Option<nu_table::T
let mut iter = iter.into_iter().peekable(); let mut iter = iter.into_iter().peekable();
if let Some(first) = iter.peek() { if let Some(first) = iter.peek() {
let mut headers = match first { let mut headers = first.columns();
Value::Record { .. } => first.columns(),
_ => ["Column_0".to_string()].to_vec(),
};
headers.insert(0, "#".into()); if !headers.is_empty() {
headers.insert(0, "#".into());
}
let mut data = vec![]; let mut data = vec![];
for (row_num, item) in iter.enumerate() { for (row_num, item) in iter.enumerate() {
let mut row = vec![row_num.to_string()]; let mut row = vec![row_num.to_string()];
for header in headers.iter().skip(1) { if headers.is_empty() {
let result = match item { row.push(item.into_string())
Value::Record { .. } => item.clone().follow_cell_path(&[PathMember::String { } else {
val: header.into(), for header in headers.iter().skip(1) {
span: Span::unknown(), let result = match item {
}]), Value::Record { .. } => {
_ => Ok(item.clone()), item.clone().follow_cell_path(&[PathMember::String {
}; val: header.into(),
span: Span::unknown(),
}])
}
_ => Ok(item.clone()),
};
match result { match result {
Ok(value) => row.push(value.into_string()), Ok(value) => row.push(value.into_string()),
Err(_) => row.push(String::new()), Err(_) => row.push(String::new()),
}
} }
} }