From 04fcbad12f99e79c642936dd511584a56586b176 Mon Sep 17 00:00:00 2001 From: amtoine Date: Sun, 14 Jul 2024 11:39:15 +0200 Subject: [PATCH] allow "loose" detection of tables --- src/nu/value.rs | 16 ++++++++++------ src/ui.rs | 2 +- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/nu/value.rs b/src/nu/value.rs index e1cbdcb..15dcd64 100644 --- a/src/nu/value.rs +++ b/src/nu/value.rs @@ -125,7 +125,7 @@ pub(crate) fn mutate_value_cell( Some(res) } -pub(crate) fn is_table(value: &Value) -> Table { +pub(crate) fn is_table(value: &Value, loose: bool) -> Table { match value { Value::List { vals, .. } => { if vals.is_empty() { @@ -145,6 +145,10 @@ pub(crate) fn is_table(value: &Value) -> Table { }; } + if loose { + return Table::IsValid; + } + // check the number of columns for each row let n = rows[0].keys().len(); for (i, row) in rows.iter().skip(1).enumerate() { @@ -227,7 +231,7 @@ pub(crate) fn is_table(value: &Value) -> Table { /// ``` // WARNING: some _unwraps_ haven't been proven to be safe in this function pub(crate) fn transpose(value: &Value) -> Value { - if matches!(is_table(value), Table::IsValid) { + if matches!(is_table(value, false), Table::IsValid) { let value_rows = match value { Value::List { vals, .. } => vals, _ => return value.clone(), @@ -540,7 +544,7 @@ mod tests { table_with_number_colum, ] { assert_eq!( - is_table(&table), + is_table(&table, false), Table::IsValid, "{} should be a table", default_value_repr(&table) @@ -607,15 +611,15 @@ mod tests { not_a_table_row_invalid_key, ] { assert_eq!( - is_table(¬_a_table), + is_table(¬_a_table, false), expected, "{} should not be a table", default_value_repr(¬_a_table) ); } - assert_eq!(is_table(&Value::test_int(0)), Table::NotAList); - assert_eq!(is_table(&Value::test_list(vec![])), Table::Empty); + assert_eq!(is_table(&Value::test_int(0), false), Table::NotAList); + assert_eq!(is_table(&Value::test_list(vec![]), false), Table::Empty); } #[test] diff --git a/src/ui.rs b/src/ui.rs index 11516ab..3f9acf2 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -238,7 +238,7 @@ fn render_data(frame: &mut Frame, app: &mut App) { let value = app.value_under_cursor(Some(CellPath { members: data_path })); - let table_type = is_table(&value); + let table_type = is_table(&value, false); let is_a_table = matches!(table_type, crate::nu::value::Table::IsValid); let mut data_frame_height = if config.show_cell_path {