From ac9e8d586f87a5063bbc2f7b95630c21ffde1483 Mon Sep 17 00:00:00 2001 From: amtoine Date: Thu, 16 Nov 2023 18:35:31 +0100 Subject: [PATCH] fix the new record API --- src/nu/value.rs | 10 +++++--- src/ui.rs | 65 ++++++++++++++++++++++++++----------------------- 2 files changed, 41 insertions(+), 34 deletions(-) diff --git a/src/nu/value.rs b/src/nu/value.rs index 7e6b66b..ac766b0 100644 --- a/src/nu/value.rs +++ b/src/nu/value.rs @@ -155,12 +155,14 @@ pub(crate) fn transpose(value: &Value) -> Value { _ => return value.clone(), }; - let full_columns = (1..=(value_rows[0].columns().len())) + let first_row = value_rows[0].as_record().unwrap(); + + let full_columns = (1..=(first_row.len())) .map(|i| format!("{i}")) .collect::>(); - if value_rows[0].columns() == full_columns { - if value_rows[0].columns().len() == 2 { + if first_row.cols == full_columns { + if first_row.len() == 2 { let cols: Vec = value_rows .iter() .map(|row| row.get_data_by_key("1").unwrap().as_string().unwrap()) @@ -179,7 +181,7 @@ pub(crate) fn transpose(value: &Value) -> Value { .map(|v| v.get_data_by_key("1").unwrap().as_string().unwrap()) .collect(); - for i in 0..(value_rows[0].columns().len() - 1) { + for i in 0..(first_row.len() - 1) { rows.push(Value::record( Record { cols: cols.clone(), diff --git a/src/ui.rs b/src/ui.rs index 99cc535..282ffc3 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -5,7 +5,7 @@ use super::config::{repr_keycode, Layout}; use super::{App, Config, Mode}; use crossterm::event::KeyCode; use nu_protocol::ast::PathMember; -use nu_protocol::{Type, Value}; +use nu_protocol::{Record, Type, Value}; use ratatui::prelude::Backend; use ratatui::{ prelude::{Alignment, Constraint, Rect}, @@ -193,16 +193,15 @@ fn repr_data(data: &Value) -> Vec { /// compute the representation of a complete Nushell table /// /// > see the tests for detailed examples -fn repr_table(table: &[Value]) -> (Vec, Vec, Vec>) { - let columns = table[0].columns(); - let mut shapes = vec![Type::Nothing; columns.len()]; +fn repr_table(table: &[Record]) -> (Vec, Vec, Vec>) { + let mut shapes = vec![Type::Nothing; table[0].len()]; let mut rows = vec![vec![]; table.len()]; for (i, row) in table.iter().enumerate() { - for (j, col) in columns.iter().enumerate() { + for (j, col) in table[0].cols.iter().enumerate() { // NOTE: because `table` is a valid table, this should always be a `Some` - let val = row.get_data_by_key(col).unwrap(); + let val = row.get(col).unwrap(); let cell_type = val.get_type(); if !matches!(cell_type, Type::Nothing) { @@ -213,12 +212,12 @@ fn repr_table(table: &[Value]) -> (Vec, Vec, Vec>) { } } - rows[i].push(repr_value(&val).data); + rows[i].push(repr_value(val).data); } } ( - columns.to_vec(), + table[0].cols.clone(), shapes.iter().map(|s| s.to_string()).collect(), rows, ) @@ -274,14 +273,20 @@ fn render_data(frame: &mut Frame<'_, B>, app: &App, config: &Config) let selected = match current { Some(PathMember::Int { val, .. }) => val, Some(PathMember::String { val, .. }) => { - value.columns().iter().position(|x| x == &val).unwrap_or(0) + value.columns().position(|x| x == &val).unwrap_or(0) } None => 0, }; if is_table(&value) { let (columns, shapes, cells) = match value { - Value::List { vals, .. } => repr_table(&vals), + Value::List { vals, .. } => { + let recs = vals + .iter() + .map(|v| v.as_record().unwrap().clone()) + .collect::>(); + repr_table(&recs) + } _ => panic!("value is a table but is not a list"), }; @@ -700,14 +705,14 @@ mod tests { #[test] fn repr_simple_table() { let table = vec![ - Value::test_record(record! { + record! { "a" => Value::test_string("x"), "b" => Value::test_int(1), - }), - Value::test_record(record! { + }, + record! { "a" => Value::test_string("y"), "b" => Value::test_int(2), - }), + }, ]; let expected = ( @@ -722,14 +727,14 @@ mod tests { #[test] fn repr_table_with_empty_column() { let table = vec![ - Value::test_record(record! { + record! { "a" => Value::test_nothing(), "b" => Value::test_int(1), - }), - Value::test_record(record! { + }, + record! { "a" => Value::test_nothing(), "b" => Value::test_int(2), - }), + }, ]; let expected = ( @@ -744,14 +749,14 @@ mod tests { #[test] fn repr_table_with_shuffled_columns() { let table = vec![ - Value::test_record(record! { + record! { "b" => Value::test_int(1), "a" => Value::test_string("x"), - }), - Value::test_record(record! { + }, + record! { "a" => Value::test_string("y"), "b" => Value::test_int(2), - }), + }, ]; let expected = ( @@ -766,14 +771,14 @@ mod tests { #[test] fn repr_table_with_holes() { let table = vec![ - Value::test_record(record! { + record! { "a" => Value::test_string("x"), "b" => Value::test_nothing(), - }), - Value::test_record(record! { + }, + record! { "a" => Value::test_nothing(), "b" => Value::test_int(2), - }), + }, ]; let expected = ( @@ -788,14 +793,14 @@ mod tests { #[test] fn repr_table_with_mixed_numeric_types() { let table = vec![ - Value::test_record(record! { + record! { "a" => Value::test_string("x"), "b" => Value::test_int(1), - }), - Value::test_record(record! { + }, + record! { "a" => Value::test_string("y"), "b" => Value::test_float(2.34), - }), + }, ]; let expected = (