fix inspect panic with large tables (#8673)

# Description

This PR fixes a small bug where `inspect` was panicking because the data
returned was larger than that terminal size.

Closes #8671
Closes #8674

# User-Facing Changes

No more panic

# 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` to check that you're using the standard code
style
- `cargo test --workspace` to check that all tests pass

> **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:
Darren Schroeder 2023-03-30 09:54:57 -05:00 committed by GitHub
parent 53beba7acc
commit e9c17daecd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -23,7 +23,11 @@ pub fn build_table(value: Value, description: String, termsize: usize) -> String
let mut desc_table = Builder::from(desc).build(); let mut desc_table = Builder::from(desc).build();
let desc_table_width = desc_table.total_width(); let desc_table_width = desc_table.total_width();
let width = val_table_width.clamp(desc_table_width, termsize); let width = if desc_table_width > termsize {
val_table_width.clamp(termsize, desc_table_width)
} else {
val_table_width.clamp(desc_table_width, termsize)
};
desc_table desc_table
.with(Style::rounded().off_bottom()) .with(Style::rounded().off_bottom())
@ -134,12 +138,17 @@ mod util {
), ),
Value::List { vals, .. } => { Value::List { vals, .. } => {
let mut columns = get_columns(&vals); let mut columns = get_columns(&vals);
let data = convert_records_to_dataset(&columns, vals); let mut data = convert_records_to_dataset(&columns, vals);
if columns.is_empty() && !data.is_empty() { if columns.is_empty() && !data.is_empty() {
columns = vec![String::from("")]; columns = vec![String::from("")];
} }
// We need something to draw a table with
if data.is_empty() {
data.push(vec!["<empty data>".to_string()])
}
(columns, data) (columns, data)
} }
Value::String { val, span } => { Value::String { val, span } => {
@ -154,7 +163,7 @@ mod util {
(vec![String::from("")], lines) (vec![String::from("")], lines)
} }
Value::Nothing { .. } => (vec![], vec![]), Value::Nothing { .. } => (vec!["".to_string()], vec![vec!["<empty data>".to_string()]]),
value => ( value => (
vec![String::from("")], vec![String::from("")],
vec![vec![debug_string_without_formatting(&value)]], vec![vec![debug_string_without_formatting(&value)]],