From d23a3737c07e7c06f802e5ada62c5addf4f99d3e Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Fri, 26 Apr 2024 19:33:00 +0800 Subject: [PATCH] make `grid` throw an error when not enough columns (#12672) # Description Resolves #12654. # User-Facing Changes `grid` can now throw an error. # Tests + Formatting Added relevant test. --- crates/nu-command/src/viewers/griddle.rs | 19 +++++++++++-------- crates/nu-command/tests/commands/griddle.rs | 8 ++++++++ crates/nu-command/tests/commands/mod.rs | 1 + 3 files changed, 20 insertions(+), 8 deletions(-) create mode 100644 crates/nu-command/tests/commands/griddle.rs diff --git a/crates/nu-command/src/viewers/griddle.rs b/crates/nu-command/src/viewers/griddle.rs index 2795ce646d..e0856e0a70 100644 --- a/crates/nu-command/src/viewers/griddle.rs +++ b/crates/nu-command/src/viewers/griddle.rs @@ -233,14 +233,17 @@ fn create_grid_output( } } - Ok( - if let Some(grid_display) = grid.fit_into_width(cols as usize) { - Value::string(grid_display.to_string(), call.head) - } else { - Value::string(format!("Couldn't fit grid into {cols} columns!"), call.head) - } - .into_pipeline_data(), - ) + if let Some(grid_display) = grid.fit_into_width(cols as usize) { + Ok(Value::string(grid_display.to_string(), call.head).into_pipeline_data()) + } else { + Err(ShellError::GenericError { + error: format!("Couldn't fit grid into {cols} columns"), + msg: "too few columns to fit the grid into".into(), + span: Some(call.head), + help: Some("try rerunning with a different --width".into()), + inner: Vec::new(), + }) + } } #[allow(clippy::type_complexity)] diff --git a/crates/nu-command/tests/commands/griddle.rs b/crates/nu-command/tests/commands/griddle.rs new file mode 100644 index 0000000000..f69db7e3d6 --- /dev/null +++ b/crates/nu-command/tests/commands/griddle.rs @@ -0,0 +1,8 @@ +use nu_test_support::nu; + +#[test] +fn grid_errors_with_few_columns() { + let actual = nu!("[1 2 3 4 5] | grid --width 5"); + + assert!(actual.err.contains("Couldn't fit grid into 5 columns")); +} diff --git a/crates/nu-command/tests/commands/mod.rs b/crates/nu-command/tests/commands/mod.rs index af1f2079d2..d7215e002b 100644 --- a/crates/nu-command/tests/commands/mod.rs +++ b/crates/nu-command/tests/commands/mod.rs @@ -39,6 +39,7 @@ mod format; mod generate; mod get; mod glob; +mod griddle; mod group_by; mod hash_; mod headers;