diff --git a/crates/nu-command/src/platform/input/list.rs b/crates/nu-command/src/platform/input/list.rs index 1b54f2d2e7b1..47311ba38ce9 100644 --- a/crates/nu-command/src/platform/input/list.rs +++ b/crates/nu-command/src/platform/input/list.rs @@ -80,36 +80,78 @@ impl Command for InputList { PipelineData::Value(Value::Range { .. }, ..) | PipelineData::Value(Value::List { .. }, ..) | PipelineData::ListStream { .. } - | PipelineData::Value(Value::Record { .. }, ..) => input - .into_iter() - .map_while(move |x| { - if let Ok(val) = x.as_string() { - Some(Options { - name: val, - value: x, - }) - } else if let Ok(record) = x.as_record() { - let mut options = Vec::new(); - for (col, val) in record.0.iter().zip(record.1.iter()) { + | PipelineData::Value(Value::Record { .. }, ..) => { + let mut lentable = Vec::::new(); + let rows = input.into_iter().collect::>(); + rows.iter().for_each(|row| { + if let Ok(record) = row.as_record() { + let columns = record.1.len(); + for (i, (col, val)) in record.0.iter().zip(record.1.iter()).enumerate() { + if i == columns - 1 { + break; + } + if let Ok(val) = val.as_string() { - options.push(format!( - " {}{}{}: {} |\t", - Color::Cyan.prefix(), - col, - Color::Cyan.suffix(), - &val - )); + let len = nu_utils::strip_ansi_likely(&val).len() + + nu_utils::strip_ansi_likely(col).len(); + if let Some(max_len) = lentable.get(i) { + lentable[i] = (*max_len).max(len); + } else { + lentable.push(len); + } } } - Some(Options { - name: options.join(""), - value: x, - }) - } else { - None } - }) - .collect(), + }); + + rows.into_iter() + .map_while(move |x| { + if let Ok(val) = x.as_string() { + Some(Options { + name: val, + value: x, + }) + } else if let Ok(record) = x.as_record() { + let mut options = Vec::new(); + let columns = record.1.len(); + for (i, (col, val)) in record.0.iter().zip(record.1.iter()).enumerate() + { + if let Ok(val) = val.as_string() { + let len = nu_utils::strip_ansi_likely(&val).len() + + nu_utils::strip_ansi_likely(col).len(); + options.push(format!( + " {}{}{}: {}{}", + Color::Cyan.prefix(), + col, + Color::Cyan.suffix(), + &val, + if i == columns - 1 { + String::from("") + } else { + format!( + "{} |", + " ".repeat( + lentable + .get(i) + .cloned() + .unwrap_or_default() + .saturating_sub(len) + ) + ) + } + )); + } + } + Some(Options { + name: options.join(""), + value: x, + }) + } else { + None + } + }) + .collect() + } _ => { return Err(ShellError::TypeMismatch {