Skip to content

Commit

Permalink
fix(table)!: Add widths parameter to new() (#664)
Browse files Browse the repository at this point in the history
This prevents creating a table that doesn't actually render anything.

Fixes: #537

BREAKING CHANGE: Table::new() now takes an additional widths parameter.
  • Loading branch information
joshka authored Dec 4, 2023
1 parent 91c67eb commit 37c70db
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 226 deletions.
37 changes: 34 additions & 3 deletions BREAKING-CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ github with a [breaking change] label.
This is a quick summary of the sections below:

- Unreleased (0.24.1)
- `Table::widths()` now accepts `AsRef<[Constraint]>`
- `Table::new()` now requires specifying the widths
-`Table::widths()` now accepts `IntoIterator<Item = AsRef<Constraint>>`
- Layout::new() now accepts direction and constraint parameters
- The default `Tabs::highlight_style` is now `Style::new().reversed()`

Expand Down Expand Up @@ -46,9 +47,39 @@ widget in the default configuration would not show any indication of the selecte

[#635]: https://github.com/ratatui-org/ratatui/pull/635

### `Table::widths()` now accepts `AsRef<[Constraint]>` ([#628])
### The default `Tabs::highlight_style` is now `Style::new().reversed()` ([#635])

Previously the default highlight style for tabs was `Style::default()`, which meant that a `Tabs`
widget in the default configuration would not show any indication of the selected tab.


### `Table::new()` now requires specifying the widths of the columrs (#664)

Previously `Table`s could be constructed without widths. In almost all cases this is an error.
A new widths parameter is now manadatory on `Table::new()`. Existing code of the form:

```rust
Table::new(rows).widths(widths)
```

Should be updated to:

```rust
Table::new(rows, widths)
```

For ease of automated replacement in cases where the amount of code broken by this change is large
or complex, it may be convenient to replace `Table::new` with `Table::default().rows`.

```rust
Table::new(rows).block(block).widths(widths);
// becomes
Table::default().rows(rows).widths(widths)
```

### `Table::widths()` now accepts `IntoIterator<Item = AsRef<Constraint>>` ([#663])

[#628]: https://github.com/ratatui-org/ratatui/pull/628
[#663]: https://github.com/ratatui-org/ratatui/pull/663

Previously `Table::widths()` took a slice (`&'a [Constraint]`). This change will introduce clippy
`needless_borrow` warnings for places where slices are passed to this method. To fix these, remove
Expand Down
30 changes: 17 additions & 13 deletions examples/demo/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,18 +289,20 @@ fn draw_second_tab(f: &mut Frame, app: &mut App, area: Rect) {
};
Row::new(vec![s.name, s.location, s.status]).style(style)
});
let table = Table::new(rows)
.header(
Row::new(vec!["Server", "Location", "Status"])
.style(Style::default().fg(Color::Yellow))
.bottom_margin(1),
)
.block(Block::default().title("Servers").borders(Borders::ALL))
.widths([
let table = Table::new(
rows,
[
Constraint::Length(15),
Constraint::Length(15),
Constraint::Length(10),
]);
],
)
.header(
Row::new(vec!["Server", "Location", "Status"])
.style(Style::default().fg(Color::Yellow))
.bottom_margin(1),
)
.block(Block::default().title("Servers").borders(Borders::ALL));
f.render_widget(table, chunks[0]);

let map = Canvas::default()
Expand Down Expand Up @@ -393,12 +395,14 @@ fn draw_third_tab(f: &mut Frame, _app: &mut App, area: Rect) {
Row::new(cells)
})
.collect();
let table = Table::new(items)
.block(Block::default().title("Colors").borders(Borders::ALL))
.widths([
let table = Table::new(
items,
[
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
Constraint::Ratio(1, 3),
]);
],
)
.block(Block::default().title("Colors").borders(Borders::ALL));
f.render_widget(table, chunks[0]);
}
3 changes: 1 addition & 2 deletions examples/demo2/tabs/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,9 @@ fn render_ingredients(selected_row: usize, area: Rect, buf: &mut Buffer) {
let rows = INGREDIENTS.iter().map(|&i| i.into()).collect_vec();
let theme = THEME.recipe;
StatefulWidget::render(
Table::new(rows)
Table::new(rows, [Constraint::Length(7), Constraint::Length(30)])
.block(Block::new().style(theme.ingredients))
.header(Row::new(vec!["Qty", "Ingredient"]).style(theme.ingredients_header))
.widths([Constraint::Length(7), Constraint::Length(30)])
.highlight_style(Style::new().light_yellow()),
area,
buf,
Expand Down
3 changes: 1 addition & 2 deletions examples/demo2/tabs/traceroute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ fn render_hops(selected_row: usize, area: Rect, buf: &mut Buffer) {
.title_alignment(Alignment::Center)
.padding(Padding::new(1, 1, 1, 1));
StatefulWidget::render(
Table::new(rows)
Table::new(rows, [Constraint::Max(100), Constraint::Length(15)])
.header(Row::new(vec!["Host", "Address"]).set_style(THEME.traceroute.header))
.widths([Constraint::Max(100), Constraint::Length(15)])
.highlight_style(THEME.traceroute.selected)
.block(block),
area,
Expand Down
16 changes: 9 additions & 7 deletions examples/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,15 +137,17 @@ fn ui(f: &mut Frame, app: &mut App) {
let cells = item.iter().map(|c| Cell::from(*c));
Row::new(cells).height(height as u16).bottom_margin(1)
});
let t = Table::new(rows)
.header(header)
.block(Block::default().borders(Borders::ALL).title("Table"))
.highlight_style(selected_style)
.highlight_symbol(">> ")
.widths([
let t = Table::new(
rows,
[
Constraint::Percentage(50),
Constraint::Max(30),
Constraint::Min(10),
]);
],
)
.header(header)
.block(Block::default().borders(Borders::ALL).title("Table"))
.highlight_style(selected_style)
.highlight_symbol(">> ");
f.render_stateful_widget(t, rects[0], &mut app.state);
}
Loading

0 comments on commit 37c70db

Please sign in to comment.