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)

<!-- List of all changes that impact the user experience here. This
helps us keep track of breaking changes. -->

# Tests + Formatting
<!--
Don't forget to add tests that cover your changes.

Make sure you've run and fixed any issues with these commands:

- `cargo fmt --all -- --check` to check standard code formatting (`cargo
fmt --all` applies these changes)
- `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A
clippy::needless_collect -A clippy::result_large_err` to check that
you're using the standard code style
- `cargo test --workspace` to check that all tests pass
- `cargo run -- crates/nu-std/tests/run.nu` to run the tests for the
standard library

> **Note**
> from `nushell` you can also use the `toolkit` as follows
> ```bash
> use toolkit.nu # or use an `env_change` hook to activate it
automatically
> toolkit check pr
> ```
-->

# After Submitting
<!-- If your PR had any user-facing changes, update [the
documentation](https://github.com/nushell/nushell.github.io) after the
PR is merged, if necessary. This will help us keep the docs up to date.
-->
This commit is contained in:
Carter Reeb 2023-05-31 12:31:46 -04:00 committed by GitHub
parent bfe7133e7c
commit e6be167797
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -80,8 +80,31 @@ impl Command for InputList {
PipelineData::Value(Value::Range { .. }, ..) PipelineData::Value(Value::Range { .. }, ..)
| PipelineData::Value(Value::List { .. }, ..) | PipelineData::Value(Value::List { .. }, ..)
| PipelineData::ListStream { .. } | PipelineData::ListStream { .. }
| PipelineData::Value(Value::Record { .. }, ..) => input | PipelineData::Value(Value::Record { .. }, ..) => {
.into_iter() let mut lentable = Vec::<usize>::new();
let rows = input.into_iter().collect::<Vec<_>>();
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() {
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);
}
}
}
}
});
rows.into_iter()
.map_while(move |x| { .map_while(move |x| {
if let Ok(val) = x.as_string() { if let Ok(val) = x.as_string() {
Some(Options { Some(Options {
@ -90,14 +113,32 @@ impl Command for InputList {
}) })
} else if let Ok(record) = x.as_record() { } else if let Ok(record) = x.as_record() {
let mut options = Vec::new(); let mut options = Vec::new();
for (col, val) in record.0.iter().zip(record.1.iter()) { 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() { 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!( options.push(format!(
" {}{}{}: {} |\t", " {}{}{}: {}{}",
Color::Cyan.prefix(), Color::Cyan.prefix(),
col, col,
Color::Cyan.suffix(), Color::Cyan.suffix(),
&val &val,
if i == columns - 1 {
String::from("")
} else {
format!(
"{} |",
" ".repeat(
lentable
.get(i)
.cloned()
.unwrap_or_default()
.saturating_sub(len)
)
)
}
)); ));
} }
} }
@ -109,7 +150,8 @@ impl Command for InputList {
None None
} }
}) })
.collect(), .collect()
}
_ => { _ => {
return Err(ShellError::TypeMismatch { return Err(ShellError::TypeMismatch {