From e6be167797f31f4702934d7951801d331dbe5498 Mon Sep 17 00:00:00 2001 From: Carter Reeb Date: Wed, 31 May 2023 12:31:46 -0400 Subject: [PATCH] fix padding when running `input list` on tables (#9316) # Description Improves the output when running `input list` on tabular data by aligning each column. # User-Facing Changes ## Before ![before](https://github.com/nushell/nushell/assets/39879966/b6a93568-f37c-4bd3-93eb-efa41cac1baf) ## After ![after](https://github.com/nushell/nushell/assets/39879966/35d74bc7-6f72-42c4-89e7-f54692ccd3ff) # Tests + Formatting # After Submitting --- crates/nu-command/src/platform/input/list.rs | 94 ++++++++++++++------ 1 file changed, 68 insertions(+), 26 deletions(-) diff --git a/crates/nu-command/src/platform/input/list.rs b/crates/nu-command/src/platform/input/list.rs index 1b54f2d2e7..47311ba38c 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 {