From e9c17daecdc258832985ef5937670735d0972536 Mon Sep 17 00:00:00 2001 From: Darren Schroeder <343840+fdncred@users.noreply.github.com> Date: Thu, 30 Mar 2023 09:54:57 -0500 Subject: [PATCH] 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. --- crates/nu-command/src/debug/inspect_table.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/debug/inspect_table.rs b/crates/nu-command/src/debug/inspect_table.rs index 29004eab2b..f9bf42bca7 100644 --- a/crates/nu-command/src/debug/inspect_table.rs +++ b/crates/nu-command/src/debug/inspect_table.rs @@ -23,7 +23,11 @@ pub fn build_table(value: Value, description: String, termsize: usize) -> String let mut desc_table = Builder::from(desc).build(); 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 .with(Style::rounded().off_bottom()) @@ -134,12 +138,17 @@ mod util { ), Value::List { 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() { columns = vec![String::from("")]; } + // We need something to draw a table with + if data.is_empty() { + data.push(vec!["".to_string()]) + } + (columns, data) } Value::String { val, span } => { @@ -154,7 +163,7 @@ mod util { (vec![String::from("")], lines) } - Value::Nothing { .. } => (vec![], vec![]), + Value::Nothing { .. } => (vec!["".to_string()], vec![vec!["".to_string()]]), value => ( vec![String::from("")], vec![vec![debug_string_without_formatting(&value)]],