Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(table): widths() now accepts AsRef<[Constraint]> #628

Merged
merged 1 commit into from
Nov 15, 2023
Merged

Conversation

joshka
Copy link
Member

@joshka joshka commented Nov 15, 2023

This allows passing an array, slice or Vec of constraints, which is more
ergonomic than requiring this to always be a slice.

The following calls now all succeed:

Table::new(rows).widths([Constraint::Length(5), Constraint::Length(5)]);
Table::new(rows).widths(&[Constraint::Length(5), Constraint::Length(5)]);

// widths could also be computed at runtime
let widths = vec![Constraint::Length(5), Constraint::Length(5)];
Table::new(rows).widths(widths.clone());
Table::new(rows).widths(&widths);

Fixes: https://discord.com/channels/1070692720437383208/1072907135664529508/1174137200909221988


I have a ratatui table widget which I return from a function, but due to the widths function requiring a reference to a value, it yells at me for referencing a temporary value

error[E0515]: cannot return value referencing temporary value
  --> src\ui.rs:89:5
   |
89 | /     Table::new(rows)
90 | |         .header(header)
91 | |         .block(Block::default().borders(Borders::ALL).title("Table"))
92 | |         .highlight_symbol(">> ")
93 | |         .widths(&(widths.clone()))
   | |__________________----------------^ returns a value referencing data owned by the
 current function
   |                    |
   |                    temporary value created here
fn draw_table<'a>(items: &'a project::Items, fields: &'a project::Fields) -> Table<'a> {
    let header_cells = fields.fields
        .iter()
        .map(|h| Cell::from(h.name.clone())
            .style(Style::default().fg(Color::Red)));

    let header = Row::new(header_cells)
        .height(1)
        .bottom_margin(0);

    let empty_cell = Value::String(String::from(""));

    let rows = items.items.iter().map(|item| {
        let cells = fields.fields.iter()
            .map(move |f|
                item.fields
                .get(&*f.name.to_ascii_lowercase()).unwrap_or(&Value::Bool(false))
                .as_str().unwrap_or(""))
            .collect::<Vec<&str>>();

        Row::new(cells).height(1).bottom_margin(1)
    });

    let split = (100 / fields.fields.len()) as u16;
    let widths: Vec<Constraint> = (0..fields.fields.len()).map(
        |n| Constraint::Percentage(split)
    ).collect::<Vec<Constraint>>();

    Table::new(rows)
        .header(header)
        .block(Block::default().borders(Borders::ALL).title("Table"))
        .highlight_symbol(">> ")
        .widths(&(widths.clone()))
}

this is the whole function

Copy link

codecov bot commented Nov 15, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (ec7b387) 90.4% compared to head (e1f6fb8) 90.4%.

Additional details and impacted files
@@          Coverage Diff          @@
##            main    #628   +/-   ##
=====================================
  Coverage   90.4%   90.4%           
=====================================
  Files         41      41           
  Lines      11903   11907    +4     
=====================================
+ Hits       10771   10775    +4     
  Misses      1132    1132           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

This allows passing an array, slice or Vec of constraints, which is more
ergonomic than requiring this to always be a slice.

The following calls now all succeed:

```rust
Table::new(rows).widths([Constraint::Length(5), Constraint::Length(5)]);
Table::new(rows).widths(&[Constraint::Length(5), Constraint::Length(5)]);

// widths could also be computed at runtime
let widths = vec![Constraint::Length(5), Constraint::Length(5)];
Table::new(rows).widths(widths.clone());
Table::new(rows).widths(&widths);
```
@joshka
Copy link
Member Author

joshka commented Nov 15, 2023

(added note to BREAKING-CHANGES.md about clippy lints.

@joshka joshka merged commit 36d8c53 into main Nov 15, 2023
33 checks passed
@joshka joshka deleted the table-widths-refs branch November 15, 2023 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants