Skip to content
This repository has been archived by the owner on Aug 6, 2023. It is now read-only.

Fix for table panic #508

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -2,6 +2,11 @@

## To be released

### Fixes

* Some combinations of `Table` widths and `Rect` width would cause a panic due to accessing area
outside the buffer - (#470)

## v0.15.0 - 2021-05-02

### Features
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "tui"
version = "0.15.0"
version = "0.15.1"
authors = ["Florian Dehau <work@fdehau.com>"]
description = """
A library to build rich terminal user interfaces or dashboards
Expand Down
4 changes: 3 additions & 1 deletion src/buffer.rs
Expand Up @@ -353,8 +353,10 @@ impl Buffer {
}

pub fn set_style(&mut self, area: Rect, style: Style) {
// Ensure styling doesn't overflow buffer - https://github.com/fdehau/tui-rs/issues/470
let right = min(area.right(), self.area.width);
for y in area.top()..area.bottom() {
for x in area.left()..area.right() {
for x in area.left()..right {
self.get_mut(x, y).set_style(style);
}
}
Expand Down
63 changes: 63 additions & 0 deletions tests/widgets_table.rs
Expand Up @@ -715,3 +715,66 @@ fn widgets_table_should_render_even_if_empty() {

terminal.backend().assert_buffer(&expected);
}

/// Testing fix for https://github.com/fdehau/tui-rs/issues/470
#[test]
fn widgets_table_columns_dont_panic() {
let test_case = |state: &mut TableState, table: Table, width: u16| {
let backend = TestBackend::new(width, 8);
let mut terminal = Terminal::new(backend).unwrap();
terminal
.draw(|f| {
let size = f.size();
f.render_stateful_widget(table, size, state);
})
.unwrap();
};

// based on https://github.com/fdehau/tui-rs/issues/470#issuecomment-852562848
let table1_width = 98;
let table1 = Table::new(vec![Row::new(vec!["r1", "r2", "r3", "r4"])])
.header(Row::new(vec!["h1", "h2", "h3", "h4"]))
.block(Block::default().borders(Borders::ALL))
.highlight_symbol(">> ")
.column_spacing(1)
.widths(&[
Constraint::Percentage(15),
Constraint::Percentage(15),
Constraint::Percentage(25),
Constraint::Percentage(45),
]);

// based on https://github.com/fdehau/tui-rs/issues/470#issuecomment-831171722
let table2_width = 189;
let table2 = Table::new(vec![Row::new(vec![
"r1", "r2", "r3", "r4", "r5", "r6", "r7", "r8", "r9", "r10",
])])
.header(Row::new(vec![
"h1", "h2", "h3", "h4", "h5", "h6", "h7", "h8", "h9", "h10",
]))
.block(Block::default().borders(Borders::TOP))
.highlight_style(Style::default().add_modifier(Modifier::REVERSED))
.highlight_symbol("=>")
.widths(&[
Constraint::Percentage(30),
Constraint::Percentage(10),
Constraint::Percentage(15),
Constraint::Percentage(10),
Constraint::Percentage(5),
Constraint::Percentage(5),
Constraint::Percentage(5),
Constraint::Percentage(5),
Constraint::Percentage(5),
Constraint::Percentage(10),
]);

let mut state = TableState::default();
// no selection, no panic
test_case(&mut state, table1.clone(), table1_width);
test_case(&mut state, table2.clone(), table2_width);

// select first, which would cause a panic before fix
state.select(Some(0));
test_case(&mut state, table1.clone(), table1_width);
test_case(&mut state, table2.clone(), table2_width);
}