Skip to content

Commit

Permalink
limit the "repr" of data with rows to frame
Browse files Browse the repository at this point in the history
  • Loading branch information
amtoine committed Jul 14, 2024
1 parent 5ce75b8 commit 9467739
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn repr_value(value: &Value) -> DataRowRepr {
/// compute the row / item representation of a complete Nushell Value
///
/// > see the tests for detailed examples
fn repr_data(data: &Value) -> Vec<DataRowRepr> {
fn repr_data(data: &Value, start: usize, length: usize) -> Vec<DataRowRepr> {
match data {
Value::List { vals, .. } => {
if vals.is_empty() {
Expand All @@ -163,7 +163,11 @@ fn repr_data(data: &Value) -> Vec<DataRowRepr> {
data: "[]".into(),
}]
} else {
vals.iter().map(repr_value).collect::<Vec<DataRowRepr>>()
vals.iter()
.skip(start)
.take(length)
.map(repr_value)
.collect::<Vec<DataRowRepr>>()
}
}
Value::Record { val: rec, .. } => {
Expand All @@ -175,6 +179,8 @@ fn repr_data(data: &Value) -> Vec<DataRowRepr> {
}]
} else {
rec.iter()
.skip(start)
.take(length)
.map(|(col, val)| {
let mut repr = repr_value(val);
repr.name = Some(col.to_string());
Expand Down Expand Up @@ -441,7 +447,7 @@ fn render_data(frame: &mut Frame, app: &mut App) {

match config.layout {
Layout::Compact => {
let items: Vec<ListItem> = repr_data(&value)
let items: Vec<ListItem> = repr_data(&value, margin_offset, height as usize)
.iter()
.cloned()
.map(|row| {
Expand All @@ -466,15 +472,13 @@ fn render_data(frame: &mut Frame, app: &mut App) {
let selected = if app.is_at_bottom() {
None
} else {
Some(selected)
Some(selected - margin_offset)
};

frame.render_stateful_widget(
items,
rect_without_bottom_bar,
&mut ListState::default()
.with_selected(selected)
.with_offset(margin_offset),
&mut ListState::default().with_selected(selected),
)
}
Layout::Table => {
Expand All @@ -486,7 +490,7 @@ fn render_data(frame: &mut Frame, app: &mut App) {
Cell::from("shape")
.style(normal_shape_style.add_modifier(Modifier::REVERSED)),
]);
let rows: Vec<Row> = repr_data(&value)
let rows: Vec<Row> = repr_data(&value, margin_offset, height as usize)
.iter()
.cloned()
.map(|row| {
Expand Down Expand Up @@ -515,7 +519,7 @@ fn render_data(frame: &mut Frame, app: &mut App) {
.style(normal_shape_style.add_modifier(Modifier::REVERSED)),
]);

let rows: Vec<Row> = repr_data(&value)
let rows: Vec<Row> = repr_data(&value, margin_offset, height as usize)
.iter()
.cloned()
.map(|row| {
Expand Down Expand Up @@ -570,9 +574,7 @@ fn render_data(frame: &mut Frame, app: &mut App) {
frame.render_stateful_widget(
table,
rect_without_bottom_bar,
&mut TableState::default()
.with_selected(Some(selected))
.with_offset(margin_offset),
&mut TableState::default().with_selected(Some(selected - margin_offset)),
)
}
}
Expand Down Expand Up @@ -819,7 +821,7 @@ mod tests {
"i" => Value::test_int(123),
});

let result = repr_data(&data);
let result = repr_data(&data, 0, 4);
let expected: Vec<DataRowRepr> = vec![
DataRowRepr::named("l", "[3 items]", "list"),
DataRowRepr::named("r", "{2 fields}", "record"),
Expand Down

0 comments on commit 9467739

Please sign in to comment.