improve table for lists

This commit is contained in:
JT 2021-09-26 06:37:25 +13:00
parent a2996abd47
commit 139775dcce
2 changed files with 18 additions and 14 deletions

View File

@ -66,26 +66,30 @@ fn convert_to_table(iter: impl IntoIterator<Item = Value>) -> Option<nu_table::T
if let Some(first) = iter.peek() { if let Some(first) = iter.peek() {
let mut headers = first.columns(); let mut headers = first.columns();
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 = if header == "<value>" { row.push(item.into_string())
Ok(item.clone()) } else {
} else { for header in headers.iter().skip(1) {
item.clone().follow_cell_path(&[PathMember::String { let result = {
val: header.into(), item.clone().follow_cell_path(&[PathMember::String {
span: Span::unknown(), val: header.into(),
}]) span: Span::unknown(),
}; }])
};
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()),
}
} }
} }

View File

@ -290,7 +290,7 @@ impl Value {
pub fn columns(&self) -> Vec<String> { pub fn columns(&self) -> Vec<String> {
match self { match self {
Value::Record { cols, .. } => cols.clone(), Value::Record { cols, .. } => cols.clone(),
_ => vec!["<value>".into()], _ => vec![],
} }
} }
} }