Skip to content

Commit

Permalink
allow "loose" detection of tables
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Jul 14, 2024
1 parent 9be16d9 commit 04fcbad
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions src/nu/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -607,15 +611,15 @@ mod tests {
not_a_table_row_invalid_key,
] {
assert_eq!(
is_table(&not_a_table),
is_table(&not_a_table, false),
expected,
"{} should not be a table",
default_value_repr(&not_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]
Expand Down
2 changes: 1 addition & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 04fcbad

Please sign in to comment.